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_object.go
330 lines (299 loc) · 7.41 KB
/
val_object.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package jsonschema
import (
"encoding/json"
"errors"
"fmt"
"regexp"
)
type additionalProperties struct {
EmbeddedSchemas
isTrue bool
propertiesIsNeighbor bool
}
func (a *additionalProperties) UnmarshalJSON(b []byte) error {
a.isTrue = true
if err := json.Unmarshal(b, &a.isTrue); err == nil {
return nil
}
return json.Unmarshal(b, &a.EmbeddedSchemas)
}
func (a *additionalProperties) CheckNeighbors(m map[string]Node) {
v, ok := m["properties"]
if !ok {
return
}
if _, ok := v.Validator.(*properties); !ok {
return
}
a.propertiesIsNeighbor = true
return
}
func (a additionalProperties) Validate(keypath []string, v interface{}) []ValidationError {
// In this case validation will be handled by the "properties" validator.
if a.propertiesIsNeighbor {
return nil
}
var valErrs []ValidationError
dataMap, ok := v.(map[string]interface{})
if !ok {
return nil
}
s, ok := a.EmbeddedSchemas[""]
if !ok {
return nil
}
for _, dataVal := range dataMap {
valErrs = append(valErrs, s.Validate(keypath, dataVal)...)
}
return valErrs
}
type dependencies struct {
EmbeddedSchemas
propertyDeps map[string]propertySet
}
type propertySet map[string]struct{}
func (d *dependencies) UnmarshalJSON(b []byte) error {
json.Unmarshal(b, &d.EmbeddedSchemas)
var c map[string]json.RawMessage
if err := json.Unmarshal(b, &c); err != nil {
return err
}
d.propertyDeps = make(map[string]propertySet, len(c))
for k, v := range c {
var props []string
if err := json.Unmarshal(v, &props); err != nil {
continue
}
set := make(propertySet, len(props))
for _, p := range props {
set[p] = struct{}{}
}
d.propertyDeps[k] = set
}
if len(d.propertyDeps) == 0 && len(d.EmbeddedSchemas) == 0 {
return errors.New("no valid schema or property dependency validators")
}
return nil
}
func (d dependencies) Validate(keypath []string, v interface{}) []ValidationError {
var valErrs []ValidationError
val, ok := v.(map[string]interface{})
if !ok {
return nil
}
// Handle schema dependencies.
for key, schema := range d.EmbeddedSchemas {
if _, ok := val[key]; !ok {
continue
}
valErrs = append(valErrs, schema.Validate(keypath, v)...)
}
// Handle property dependencies.
for key, set := range d.propertyDeps {
if _, ok := val[key]; !ok {
continue
}
for a := range set {
if _, ok := val[a]; !ok {
valErrs = append(valErrs, ValidationError{keypath,
fmt.Sprintf("instance does not have a property with the name %s", a)})
}
}
}
return valErrs
}
type maxProperties int
func (m *maxProperties) UnmarshalJSON(b []byte) error {
var n int
if err := json.Unmarshal(b, &n); err != nil {
return err
}
if n < 0 {
return errors.New("maxProperties cannot be smaller than zero")
}
*m = maxProperties(n)
return nil
}
func (m maxProperties) Validate(keypath []string, v interface{}) []ValidationError {
val, ok := v.(map[string]interface{})
if !ok {
return nil
}
if len(val) > int(m) {
return []ValidationError{{keypath,
fmt.Sprintf("Object has more properties than maxProperties (%d > %d)", len(val), m)}}
}
return nil
}
type minProperties int
func (m *minProperties) UnmarshalJSON(b []byte) error {
var n int
if err := json.Unmarshal(b, &n); err != nil {
return err
}
if n < 0 {
return errors.New("minProperties cannot be smaller than zero")
}
*m = minProperties(n)
return nil
}
func (m minProperties) Validate(keypath []string, v interface{}) []ValidationError {
val, ok := v.(map[string]interface{})
if !ok {
return nil
}
if len(val) < int(m) {
return []ValidationError{{keypath,
fmt.Sprintf("Object has fewer properties than minProperties (%d < %d)", len(val), m)}}
}
return nil
}
type patternProperties struct {
EmbeddedSchemas
object []regexpToSchema
disabled bool
}
type regexpToSchema struct {
regexp regexp.Regexp
schema *Schema
}
func (p *patternProperties) UnmarshalJSON(b []byte) error {
var m map[string]*Schema
if err := json.Unmarshal(b, &m); err != nil {
return err
}
p.EmbeddedSchemas = make(EmbeddedSchemas, len(m))
for k, v := range m {
p.EmbeddedSchemas[k] = v
r, err := regexp.Compile(k)
if err != nil {
continue
}
p.object = append(p.object, regexpToSchema{*r, v})
}
return nil
}
func (p *patternProperties) CheckNeighbors(m map[string]Node) {
v, ok := m["properties"]
if !ok {
return
}
if _, ok := v.Validator.(*properties); !ok {
return
}
p.disabled = true
return
}
func (p patternProperties) Validate(keypath []string, v interface{}) []ValidationError {
if p.disabled {
return nil
}
var valErrs []ValidationError
data, ok := v.(map[string]interface{})
if !ok {
return nil
}
for dataKey, dataVal := range data {
for _, val := range p.object {
if val.regexp.MatchString(dataKey) {
valErrs = append(valErrs, val.schema.Validate(append(keypath, dataKey), dataVal)...)
}
}
}
return valErrs
}
type properties struct {
EmbeddedSchemas
patternProperties *patternProperties
additionalPropertiesBool bool
additionalPropertiesObject *Schema
}
func (p *properties) CheckNeighbors(m map[string]Node) {
p.additionalPropertiesBool = true
v, ok := m["patternProperties"]
if ok {
pat, ok := v.Validator.(*patternProperties)
if ok {
// Use a copy of pattern properties with 'disable' set to false.
//
// Since 'properties' is one of it's neighbors, the independant
// 'patternProperties' validator will set its 'disable' to true,
// so only this one will be run.
p.patternProperties = &patternProperties{pat.EmbeddedSchemas, pat.object, false}
}
}
v, ok = m["additionalProperties"]
if !ok {
return
}
a, ok := v.Validator.(*additionalProperties)
if !ok {
return
}
p.additionalPropertiesBool = a.isTrue
s, ok := a.EmbeddedSchemas[""]
if !ok {
return
}
p.additionalPropertiesObject = s
return
}
func (p properties) Validate(keypath []string, v interface{}) []ValidationError {
var valErrs []ValidationError
dataMap, ok := v.(map[string]interface{})
if !ok {
return nil
}
for dataKey, dataVal := range dataMap {
var match = false
schema, ok := p.EmbeddedSchemas[dataKey]
if ok {
valErrs = append(valErrs, schema.Validate(append(keypath, dataKey), dataVal)...)
match = true
}
if p.patternProperties != nil {
for _, val := range p.patternProperties.object {
if val.regexp.MatchString(dataKey) {
valErrs = append(valErrs, val.schema.Validate(append(keypath, dataKey), dataVal)...)
match = true
}
}
}
if match {
continue
}
if p.additionalPropertiesObject != nil {
valErrs = append(valErrs, p.additionalPropertiesObject.Validate(append(keypath, dataKey), dataVal)...)
continue
}
if !p.additionalPropertiesBool {
valErrs = append([]ValidationError{{keypath, fmt.Sprintf("Additional properties aren't allowed, found \"%v\" as one of its keys", dataKey)}})
}
}
return valErrs
}
type required map[string]struct{}
func (r *required) UnmarshalJSON(b []byte) error {
var l []string
if err := json.Unmarshal(b, &l); err != nil {
return err
}
*r = make(required)
for _, val := range l {
(*r)[val] = struct{}{}
}
return nil
}
func (r required) Validate(keypath []string, v interface{}) []ValidationError {
var valErrs []ValidationError
data, ok := v.(map[string]interface{})
if !ok {
return nil
}
for key := range r {
if _, ok := data[key]; !ok {
valErrs = append(valErrs, ValidationError{keypath, fmt.Sprintf("Required error. The data must be an object with \"%v\" as one of its keys", key)})
}
}
return valErrs
}