This repository has been archived by the owner on Jul 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
val_number.go
163 lines (147 loc) · 3.61 KB
/
val_number.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package jsonschema
import (
"encoding/json"
"fmt"
"strings"
)
type maximum struct {
json.Number
exclusive bool
}
func (m maximum) isLargerThanInt(n int64) (bool, error) {
if !strings.Contains(m.String(), ".") {
max, err := m.Int64()
if err != nil {
return false, err
}
return max > n || !m.exclusive && max == n, nil
} else {
return m.isLargerThanFloat(float64(n))
}
}
func (m maximum) isLargerThanFloat(n float64) (isLarger bool, err error) {
max, err := m.Float64()
if err != nil {
return
}
return max > n || !m.exclusive && max == n, nil
}
func (m *maximum) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &m.Number)
}
func (m *maximum) SetSchema(v map[string]json.RawMessage) error {
value, ok := v["exclusiveMaximum"]
if ok {
// Ignore errors from Unmarshal. If exclusiveMaximum is a non boolean JSON
// value we leave it as false.
json.Unmarshal(value, &m.exclusive)
}
return nil
}
func (m maximum) Validate(keypath []string, v interface{}) []ValidationError {
normalized, err := normalizeNumber(v)
if err != nil {
return []ValidationError{{keypath, err.Error()}}
}
var isLarger bool
switch n := normalized.(type) {
case int64:
isLarger, err = m.isLargerThanInt(n)
case float64:
isLarger, err = m.isLargerThanFloat(n)
default:
return nil
}
if err != nil {
return nil
}
if !isLarger {
maxErr := fmt.Sprintf("Value must be smaller than %s.", m)
return []ValidationError{{keypath, maxErr}}
}
return nil
}
type minimum struct {
json.Number
exclusive bool
}
func (m minimum) isLargerThanInt(n int64) (bool, error) {
if !strings.Contains(m.String(), ".") {
min, err := m.Int64()
if err != nil {
return false, nil
}
return min > n || !m.exclusive && min == n, nil
} else {
return m.isLargerThanFloat(float64(n))
}
}
func (m minimum) isLargerThanFloat(n float64) (isLarger bool, err error) {
min, err := m.Float64()
if err != nil {
return
}
return min > n || !m.exclusive && min == n, nil
}
func (m *minimum) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &m.Number)
}
func (m *minimum) SetSchema(v map[string]json.RawMessage) error {
value, ok := v["exclusiveminimum"]
if ok {
// Ignore errors from Unmarshal. If exclusiveminimum is a non boolean JSON
// value we leave it as false.
json.Unmarshal(value, &m.exclusive)
}
return nil
}
func (m minimum) Validate(keypath []string, v interface{}) []ValidationError {
normalized, err := normalizeNumber(v)
if err != nil {
return []ValidationError{{keypath, err.Error()}}
}
var isLarger bool
switch n := normalized.(type) {
case int64:
isLarger, err = m.isLargerThanInt(n)
case float64:
isLarger, err = m.isLargerThanFloat(n)
default:
return nil
}
if err != nil {
return nil
}
if isLarger {
minErr := fmt.Sprintf("Value must be larger than %s.", m)
return []ValidationError{{keypath, minErr}}
}
return nil
}
type multipleOf int64
func (m *multipleOf) UnmarshalJSON(b []byte) error {
var n int64
if err := json.Unmarshal(b, &n); err != nil {
return err
}
*m = multipleOf(n)
return nil
}
// Contrary to the spec, validation doesn't support floats in the schema
// or the data being validated. This is because of issues with math.Mod,
// e.g. math.Mod(0.0075, 0.0001) != 0.
func (m multipleOf) Validate(keypath []string, v interface{}) []ValidationError {
normalized, err := normalizeNumber(v)
if err != nil {
return []ValidationError{{keypath, err.Error()}}
}
n, ok := normalized.(int64)
if !ok {
return nil
}
if n%int64(m) != 0 {
mulErr := ValidationError{keypath, fmt.Sprintf("Value must be a multiple of %d.", m)}
return []ValidationError{mulErr}
}
return nil
}