-
Notifications
You must be signed in to change notification settings - Fork 54
/
keywords_object.go
692 lines (590 loc) · 19.9 KB
/
keywords_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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package jsonschema
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strconv"
jptr "github.com/qri-io/jsonpointer"
)
// Properties defines the properties JSON Schema keyword
type Properties map[string]*Schema
// NewProperties allocates a new Properties keyword
func NewProperties() Keyword {
return &Properties{}
}
// Register implements the Keyword interface for Properties
func (p *Properties) Register(uri string, registry *SchemaRegistry) {
for _, v := range *p {
v.Register(uri, registry)
}
}
// Resolve implements the Keyword interface for Properties
func (p *Properties) Resolve(pointer jptr.Pointer, uri string) *Schema {
if pointer == nil {
return nil
}
current := pointer.Head()
if current == nil {
return nil
}
if schema, ok := (*p)[*current]; ok {
return schema.Resolve(pointer.Tail(), uri)
}
return nil
}
// ValidateKeyword implements the Keyword interface for Properties
func (p Properties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[Properties] Validating")
if obj, ok := data.(map[string]interface{}); ok {
subState := currentState.NewSubState()
for key := range p {
if _, ok := obj[key]; ok {
currentState.SetEvaluatedKey(key)
subState.ClearState()
subState.DescendBaseFromState(currentState, "properties", key)
subState.DescendRelativeFromState(currentState, "properties", key)
subState.DescendInstanceFromState(currentState, key)
subState.Errs = &[]KeyError{}
p[key].ValidateKeyword(ctx, subState, obj[key])
currentState.AddSubErrors(*subState.Errs...)
if subState.IsValid() {
currentState.UpdateEvaluatedPropsAndItems(subState)
}
}
}
}
}
// JSONProp implements the JSONPather for Properties
func (p Properties) JSONProp(name string) interface{} {
return p[name]
}
// JSONChildren implements the JSONContainer interface for Properties
func (p Properties) JSONChildren() (res map[string]JSONPather) {
res = map[string]JSONPather{}
for key, sch := range p {
res[key] = sch
}
return
}
// Required defines the required JSON Schema keyword
type Required []string
// NewRequired allocates a new Required keyword
func NewRequired() Keyword {
return &Required{}
}
// Register implements the Keyword interface for Required
func (r *Required) Register(uri string, registry *SchemaRegistry) {}
// Resolve implements the Keyword interface for Required
func (r *Required) Resolve(pointer jptr.Pointer, uri string) *Schema {
return nil
}
// ValidateKeyword implements the Keyword interface for Required
func (r Required) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[Required] Validating")
if obj, ok := data.(map[string]interface{}); ok {
for _, key := range r {
if _, ok := obj[key]; !ok {
currentState.AddError(data, fmt.Sprintf(`"%s" value is required`, key))
}
}
}
}
// JSONProp implements the JSONPather for Required
func (r Required) JSONProp(name string) interface{} {
idx, err := strconv.Atoi(name)
if err != nil {
return nil
}
if idx > len(r) || idx < 0 {
return nil
}
return r[idx]
}
// MaxProperties defines the maxProperties JSON Schema keyword
type MaxProperties int
// NewMaxProperties allocates a new MaxProperties keyword
func NewMaxProperties() Keyword {
return new(MaxProperties)
}
// Register implements the Keyword interface for MaxProperties
func (m *MaxProperties) Register(uri string, registry *SchemaRegistry) {}
// Resolve implements the Keyword interface for MaxProperties
func (m *MaxProperties) Resolve(pointer jptr.Pointer, uri string) *Schema {
return nil
}
// ValidateKeyword implements the Keyword interface for MaxProperties
func (m MaxProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[MaxProperties] Validating")
if obj, ok := data.(map[string]interface{}); ok {
if len(obj) > int(m) {
currentState.AddError(data, fmt.Sprintf("%d object Properties exceed %d maximum", len(obj), m))
}
}
}
// MinProperties defines the minProperties JSON Schema keyword
type MinProperties int
// NewMinProperties allocates a new MinProperties keyword
func NewMinProperties() Keyword {
return new(MinProperties)
}
// Register implements the Keyword interface for MinProperties
func (m *MinProperties) Register(uri string, registry *SchemaRegistry) {}
// Resolve implements the Keyword interface for MinProperties
func (m *MinProperties) Resolve(pointer jptr.Pointer, uri string) *Schema {
return nil
}
// ValidateKeyword implements the Keyword interface for MinProperties
func (m MinProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[MinProperties] Validating")
if obj, ok := data.(map[string]interface{}); ok {
if len(obj) < int(m) {
currentState.AddError(data, fmt.Sprintf("%d object Properties below %d minimum", len(obj), m))
}
}
}
// PatternProperties defines the patternProperties JSON Schema keyword
type PatternProperties []patternSchema
// NewPatternProperties allocates a new PatternProperties keyword
func NewPatternProperties() Keyword {
return &PatternProperties{}
}
type patternSchema struct {
key string
re *regexp.Regexp
schema *Schema
}
// Register implements the Keyword interface for PatternProperties
func (p *PatternProperties) Register(uri string, registry *SchemaRegistry) {
for _, v := range *p {
v.schema.Register(uri, registry)
}
}
// Resolve implements the Keyword interface for PatternProperties
func (p *PatternProperties) Resolve(pointer jptr.Pointer, uri string) *Schema {
if pointer == nil {
return nil
}
current := pointer.Head()
if current == nil {
return nil
}
patProp := &patternSchema{}
for _, v := range *p {
if v.key == *current {
patProp = &v
break
}
}
return patProp.schema.Resolve(pointer.Tail(), uri)
}
// ValidateKeyword implements the Keyword interface for PatternProperties
func (p PatternProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[PatternProperties] Validating")
if obj, ok := data.(map[string]interface{}); ok {
for key, val := range obj {
for _, ptn := range p {
if ptn.re.Match([]byte(key)) {
currentState.SetEvaluatedKey(key)
subState := currentState.NewSubState()
subState.DescendBase("patternProperties", key)
subState.DescendRelative("patternProperties", key)
subState.DescendInstance(key)
subState.Errs = &[]KeyError{}
ptn.schema.ValidateKeyword(ctx, subState, val)
currentState.AddSubErrors(*subState.Errs...)
if subState.IsValid() {
currentState.UpdateEvaluatedPropsAndItems(subState)
}
}
}
}
}
}
// JSONProp implements the JSONPather for PatternProperties
func (p PatternProperties) JSONProp(name string) interface{} {
for _, pp := range p {
if pp.key == name {
return pp.schema
}
}
return nil
}
// JSONChildren implements the JSONContainer interface for PatternProperties
func (p PatternProperties) JSONChildren() (res map[string]JSONPather) {
res = map[string]JSONPather{}
for i, pp := range p {
res[strconv.Itoa(i)] = pp.schema
}
return
}
// UnmarshalJSON implements the json.Unmarshaler interface for PatternProperties
func (p *PatternProperties) UnmarshalJSON(data []byte) error {
var props map[string]*Schema
if err := json.Unmarshal(data, &props); err != nil {
return err
}
ptn := make(PatternProperties, len(props))
i := 0
for key, sch := range props {
re, err := regexp.Compile(key)
if err != nil {
return fmt.Errorf("invalid pattern: %s: %s", key, err.Error())
}
ptn[i] = patternSchema{
key: key,
re: re,
schema: sch,
}
i++
}
*p = ptn
return nil
}
// MarshalJSON implements the json.Marshaler interface for PatternProperties
func (p PatternProperties) MarshalJSON() ([]byte, error) {
obj := map[string]interface{}{}
for _, prop := range p {
obj[prop.key] = prop.schema
}
return json.Marshal(obj)
}
// AdditionalProperties defines the additionalProperties JSON Schema keyword
type AdditionalProperties Schema
// NewAdditionalProperties allocates a new AdditionalProperties keyword
func NewAdditionalProperties() Keyword {
return &AdditionalProperties{}
}
// Register implements the Keyword interface for AdditionalProperties
func (ap *AdditionalProperties) Register(uri string, registry *SchemaRegistry) {
(*Schema)(ap).Register(uri, registry)
}
// Resolve implements the Keyword interface for AdditionalProperties
func (ap *AdditionalProperties) Resolve(pointer jptr.Pointer, uri string) *Schema {
return (*Schema)(ap).Resolve(pointer, uri)
}
// ValidateKeyword implements the Keyword interface for AdditionalProperties
func (ap *AdditionalProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[AdditionalProperties] Validating")
if obj, ok := data.(map[string]interface{}); ok {
subState := currentState.NewSubState()
subState.ClearState()
subState.DescendBase("additionalProperties")
subState.DescendRelative("additionalProperties")
for key := range obj {
if currentState.IsLocallyEvaluatedKey(key) {
continue
}
currentState.SetEvaluatedKey(key)
subState.ClearState()
subState.DescendInstanceFromState(currentState, key)
if ap.schemaType == schemaTypeFalse {
subState.AddError(data, "additional properties are not allowed")
return
}
(*Schema)(ap).ValidateKeyword(ctx, subState, obj[key])
currentState.UpdateEvaluatedPropsAndItems(subState)
}
}
}
// UnmarshalJSON implements the json.Unmarshaler interface for AdditionalProperties
func (ap *AdditionalProperties) UnmarshalJSON(data []byte) error {
sch := &Schema{}
if err := json.Unmarshal(data, sch); err != nil {
return err
}
*ap = (AdditionalProperties)(*sch)
return nil
}
// PropertyNames defines the propertyNames JSON Schema keyword
type PropertyNames Schema
// NewPropertyNames allocates a new PropertyNames keyword
func NewPropertyNames() Keyword {
return &PropertyNames{}
}
// Register implements the Keyword interface for PropertyNames
func (p *PropertyNames) Register(uri string, registry *SchemaRegistry) {
(*Schema)(p).Register(uri, registry)
}
// Resolve implements the Keyword interface for PropertyNames
func (p *PropertyNames) Resolve(pointer jptr.Pointer, uri string) *Schema {
return (*Schema)(p).Resolve(pointer, uri)
}
// ValidateKeyword implements the Keyword interface for PropertyNames
func (p *PropertyNames) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[PropertyNames] Validating")
if obj, ok := data.(map[string]interface{}); ok {
for key := range obj {
subState := currentState.NewSubState()
subState.DescendBase("propertyNames")
subState.DescendRelative("propertyNames")
subState.DescendInstance(key)
(*Schema)(p).ValidateKeyword(ctx, subState, key)
}
}
}
// JSONProp implements the JSONPather for PropertyNames
func (p PropertyNames) JSONProp(name string) interface{} {
return Schema(p).JSONProp(name)
}
// JSONChildren implements the JSONContainer interface for PropertyNames
func (p PropertyNames) JSONChildren() (res map[string]JSONPather) {
return Schema(p).JSONChildren()
}
// UnmarshalJSON implements the json.Unmarshaler interface for PropertyNames
func (p *PropertyNames) UnmarshalJSON(data []byte) error {
var sch Schema
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
*p = PropertyNames(sch)
return nil
}
// MarshalJSON implements the json.Marshaler interface for PropertyNames
func (p PropertyNames) MarshalJSON() ([]byte, error) {
return json.Marshal(Schema(p))
}
// DependentSchemas defines the dependentSchemas JSON Schema keyword
type DependentSchemas map[string]SchemaDependency
// NewDependentSchemas allocates a new DependentSchemas keyword
func NewDependentSchemas() Keyword {
return &DependentSchemas{}
}
// Register implements the Keyword interface for DependentSchemas
func (d *DependentSchemas) Register(uri string, registry *SchemaRegistry) {
for _, v := range *d {
v.schema.Register(uri, registry)
}
}
// Resolve implements the Keyword interface for DependentSchemas
func (d *DependentSchemas) Resolve(pointer jptr.Pointer, uri string) *Schema {
if pointer == nil {
return nil
}
current := pointer.Head()
if current == nil {
return nil
}
if schema, ok := (*d)[*current]; ok {
return schema.Resolve(pointer.Tail(), uri)
}
return nil
}
// ValidateKeyword implements the Keyword interface for DependentSchemas
func (d *DependentSchemas) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[DependentSchemas] Validating")
for _, v := range *d {
subState := currentState.NewSubState()
subState.DescendBase("dependentSchemas")
subState.DescendRelative("dependentSchemas")
subState.Misc["dependencyParent"] = "dependentSchemas"
v.ValidateKeyword(ctx, subState, data)
}
}
type _dependentSchemas map[string]Schema
// UnmarshalJSON implements the json.Unmarshaler interface for DependentSchemas
func (d *DependentSchemas) UnmarshalJSON(data []byte) error {
_d := _dependentSchemas{}
if err := json.Unmarshal(data, &_d); err != nil {
return err
}
ds := DependentSchemas{}
for k, v := range _d {
sch := Schema(v)
ds[k] = SchemaDependency{
schema: &sch,
prop: k,
}
}
*d = ds
return nil
}
// JSONProp implements the JSONPather for DependentSchemas
func (d DependentSchemas) JSONProp(name string) interface{} {
return d[name]
}
// JSONChildren implements the JSONContainer interface for DependentSchemas
func (d DependentSchemas) JSONChildren() (r map[string]JSONPather) {
r = map[string]JSONPather{}
for key, val := range d {
r[key] = val
}
return
}
// SchemaDependency is the internal representation of a dependent schema
type SchemaDependency struct {
schema *Schema
prop string
}
// Register implements the Keyword interface for SchemaDependency
func (d *SchemaDependency) Register(uri string, registry *SchemaRegistry) {
d.schema.Register(uri, registry)
}
// Resolve implements the Keyword interface for SchemaDependency
func (d *SchemaDependency) Resolve(pointer jptr.Pointer, uri string) *Schema {
return d.schema.Resolve(pointer, uri)
}
// ValidateKeyword implements the Keyword interface for SchemaDependency
func (d *SchemaDependency) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[SchemaDependency] Validating")
depsData := map[string]interface{}{}
ok := false
if depsData, ok = data.(map[string]interface{}); !ok {
return
}
if _, okProp := depsData[d.prop]; !okProp {
return
}
subState := currentState.NewSubState()
subState.DescendBase(d.prop)
subState.DescendRelative(d.prop)
d.schema.ValidateKeyword(ctx, subState, data)
}
// MarshalJSON implements the json.Marshaler interface for SchemaDependency
func (d SchemaDependency) MarshalJSON() ([]byte, error) {
return json.Marshal(d.schema)
}
// JSONProp implements the JSONPather for SchemaDependency
func (d SchemaDependency) JSONProp(name string) interface{} {
return d.schema.JSONProp(name)
}
// DependentRequired defines the dependentRequired JSON Schema keyword
type DependentRequired map[string]PropertyDependency
// NewDependentRequired allocates a new DependentRequired keyword
func NewDependentRequired() Keyword {
return &DependentRequired{}
}
// Register implements the Keyword interface for DependentRequired
func (d *DependentRequired) Register(uri string, registry *SchemaRegistry) {}
// Resolve implements the Keyword interface for DependentRequired
func (d *DependentRequired) Resolve(pointer jptr.Pointer, uri string) *Schema {
return nil
}
// ValidateKeyword implements the Keyword interface for DependentRequired
func (d *DependentRequired) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[DependentRequired] Validating")
for _, prop := range *d {
subState := currentState.NewSubState()
subState.DescendBase("dependentRequired")
subState.DescendRelative("dependentRequired")
subState.Misc["dependencyParent"] = "dependentRequired"
prop.ValidateKeyword(ctx, subState, data)
}
}
type _dependentRequired map[string][]string
// UnmarshalJSON implements the json.Unmarshaler interface for DependentRequired
func (d *DependentRequired) UnmarshalJSON(data []byte) error {
_d := _dependentRequired{}
if err := json.Unmarshal(data, &_d); err != nil {
return err
}
dr := DependentRequired{}
for k, v := range _d {
dr[k] = PropertyDependency{
dependencies: v,
prop: k,
}
}
*d = dr
return nil
}
// MarshalJSON implements the json.Marshaler interface for DependentRequired
func (d DependentRequired) MarshalJSON() ([]byte, error) {
obj := map[string]interface{}{}
for key, prop := range d {
obj[key] = prop.dependencies
}
return json.Marshal(obj)
}
// JSONProp implements the JSONPather for DependentRequired
func (d DependentRequired) JSONProp(name string) interface{} {
return d[name]
}
// JSONChildren implements the JSONContainer interface for DependentRequired
func (d DependentRequired) JSONChildren() (r map[string]JSONPather) {
r = map[string]JSONPather{}
for key, val := range d {
r[key] = val
}
return
}
// PropertyDependency is the internal representation of a dependent property
type PropertyDependency struct {
dependencies []string
prop string
}
// Register implements the Keyword interface for PropertyDependency
func (p *PropertyDependency) Register(uri string, registry *SchemaRegistry) {}
// Resolve implements the Keyword interface for PropertyDependency
func (p *PropertyDependency) Resolve(pointer jptr.Pointer, uri string) *Schema {
return nil
}
// ValidateKeyword implements the Keyword interface for PropertyDependency
func (p *PropertyDependency) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[PropertyDependency] Validating")
if obj, ok := data.(map[string]interface{}); ok {
if obj[p.prop] == nil {
return
}
for _, dep := range p.dependencies {
if obj[dep] == nil {
currentState.AddError(data, fmt.Sprintf(`"%s" property is required`, dep))
}
}
}
}
// JSONProp implements the JSONPather for PropertyDependency
func (p PropertyDependency) JSONProp(name string) interface{} {
idx, err := strconv.Atoi(name)
if err != nil {
return nil
}
if idx > len(p.dependencies) || idx < 0 {
return nil
}
return p.dependencies[idx]
}
// UnevaluatedProperties defines the unevaluatedProperties JSON Schema keyword
type UnevaluatedProperties Schema
// NewUnevaluatedProperties allocates a new UnevaluatedProperties keyword
func NewUnevaluatedProperties() Keyword {
return &UnevaluatedProperties{}
}
// Register implements the Keyword interface for UnevaluatedProperties
func (up *UnevaluatedProperties) Register(uri string, registry *SchemaRegistry) {
(*Schema)(up).Register(uri, registry)
}
// Resolve implements the Keyword interface for UnevaluatedProperties
func (up *UnevaluatedProperties) Resolve(pointer jptr.Pointer, uri string) *Schema {
return (*Schema)(up).Resolve(pointer, uri)
}
// ValidateKeyword implements the Keyword interface for UnevaluatedProperties
func (up *UnevaluatedProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[UnevaluatedProperties] Validating")
if obj, ok := data.(map[string]interface{}); ok {
subState := currentState.NewSubState()
subState.ClearState()
subState.DescendBase("unevaluatedProperties")
subState.DescendRelative("unevaluatedProperties")
for key := range obj {
if currentState.IsEvaluatedKey(key) {
continue
}
if up.schemaType == schemaTypeFalse {
currentState.AddError(data, "unevaluated properties are not allowed")
return
}
subState.DescendInstanceFromState(currentState, key)
(*Schema)(up).ValidateKeyword(ctx, subState, obj[key])
}
}
}
// UnmarshalJSON implements the json.Unmarshaler interface for UnevaluatedProperties
func (up *UnevaluatedProperties) UnmarshalJSON(data []byte) error {
sch := &Schema{}
if err := json.Unmarshal(data, sch); err != nil {
return err
}
*up = (UnevaluatedProperties)(*sch)
return nil
}