/
json_schema.go
813 lines (678 loc) · 21 KB
/
json_schema.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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
package core
import (
"encoding/json"
"errors"
"fmt"
"math"
"slices"
"strings"
"github.com/inoxlang/inox/internal/utils"
"github.com/santhosh-tekuri/jsonschema/v5"
)
var (
ErrInvalidOrUnsupportedJsonSchema = errors.New("invalid or unsupported JSON Schema")
ErrRecursiveJSONSchemaNotSupported = errors.New("recursive JSON schema are not supported")
ErrJSONSchemaMixingIntFloatNotSupported = errors.New("JSON schemas mixing integers and floats are not supported")
JSON_SCHEMA_TYPE_TO_PATTERN = map[string]Pattern{
"string": STR_PATTERN,
"number": FLOAT_PATTERN,
"integer": INT_PATTERN,
"object": OBJECT_PATTERN,
"array": LIST_PATTERN,
"boolean": BOOL_PATTERN,
"null": NIL_PATTERN,
}
)
func init() {
//remove loader for file: URIs
clear(jsonschema.Loaders)
}
// ConvertJsonSchemaToPattern converts a JSON schema definition to an Inox pattern,
// all schemas are not supported and the resulting pattern might be stricter.
func ConvertJsonSchemaToPattern(schemaBytes string) (Pattern, error) {
compiler := jsonschema.NewCompiler()
url := "schema.json"
if err := compiler.AddResource(url, strings.NewReader(schemaBytes)); err != nil {
return nil, err
}
compiler.Draft = jsonschema.Draft7
schema, err := compiler.Compile(url)
if err != nil {
return nil, err
}
return convertJsonSchemaToPattern(schema, nil, false, false, 0)
}
func convertJsonSchemaToPattern(schema *jsonschema.Schema, baseSchema *jsonschema.Schema, ignoreSpecial bool, removeIgnore bool, depth int) (Pattern, error) {
//baseSchema is the parent schema of children of not, allOf, anyOf, oneOf, it is nil in other cases.
//We choose to ignore
if depth > 10 {
return nil, ErrRecursiveJSONSchemaNotSupported
}
if len(schema.PatternProperties) > 0 {
return nil, errors.New("pattern properties not supported yet")
}
// any or never
if schema.Always != nil {
if *schema.Always {
return ANYVAL_PATTERN, nil
}
return NEVER_PATTERN, nil
}
if schema.Ref != nil {
return convertJsonSchemaToPattern(schema.Ref, nil, false, removeIgnore, depth+1)
}
if !ignoreSpecial {
if schema.Not != nil {
basePattern, err := convertJsonSchemaToPattern(schema, nil, true, false, depth+1)
if err != nil {
return nil, err
}
negation, err := convertJsonSchemaToPattern(schema.Not, schema, false, false, depth+1)
if err != nil {
return nil, err
}
return NewDifferencePattern(basePattern, negation), nil
}
if len(schema.AllOf) > 0 {
basePattern, err := convertJsonSchemaToPattern(schema, nil, true, false, depth+1)
if err != nil {
return nil, err
}
var intersectionCases []Pattern
if basePattern != ANYVAL_PATTERN {
intersectionCases = append(intersectionCases, basePattern)
}
hasIntPattern := false
hasFloatPattern := false
for _, t := range schema.AllOf {
case_, err := convertJsonSchemaToPattern(t, schema, false, true, depth+1)
if err != nil {
return nil, err
}
isFloatPattern := isFloatPattern(case_)
isIntPattern := isIntPattern(case_)
if hasIntPattern && isFloatPattern {
return nil, ErrJSONSchemaMixingIntFloatNotSupported
} else if hasFloatPattern && isIntPattern {
return nil, ErrJSONSchemaMixingIntFloatNotSupported
}
hasIntPattern = isIntPattern
hasFloatPattern = isFloatPattern
intersectionCases = append(intersectionCases, case_)
}
if len(intersectionCases) == 1 {
return intersectionCases[0], nil
}
return NewIntersectionPattern(intersectionCases, nil), nil
}
if len(schema.AnyOf) > 0 {
basePattern, err := convertJsonSchemaToPattern(schema, nil, true, false, depth+1)
if err != nil {
return nil, err
}
var unionCases []Pattern
hasIntPattern := false
hasFloatPattern := false
for _, t := range schema.AnyOf {
case_, err := convertJsonSchemaToPattern(t, schema, false, true, depth+1)
if err != nil {
return nil, err
}
isFloatPattern := isFloatPattern(case_)
isIntPattern := isIntPattern(case_)
if hasIntPattern && isFloatPattern {
return nil, ErrJSONSchemaMixingIntFloatNotSupported
} else if hasFloatPattern && isIntPattern {
return nil, ErrJSONSchemaMixingIntFloatNotSupported
}
hasIntPattern = isIntPattern
hasFloatPattern = isFloatPattern
unionCases = append(unionCases, case_)
}
unionPattern := NewUnionPattern(unionCases, nil)
if basePattern == ANYVAL_PATTERN {
if len(unionCases) == 1 {
return unionCases[0], nil
}
return unionPattern, nil
}
return NewIntersectionPattern([]Pattern{basePattern, unionPattern}, nil), nil
}
if len(schema.OneOf) > 0 {
basePattern, err := convertJsonSchemaToPattern(schema, nil, true, false, depth+1)
if err != nil {
return nil, err
}
var disjointUnionCases []Pattern
hasIntPattern := false
hasFloatPattern := false
for _, t := range schema.OneOf {
case_, err := convertJsonSchemaToPattern(t, schema, false, true, depth+1)
if err != nil {
return nil, err
}
isFloatPattern := isFloatPattern(case_)
isIntPattern := isIntPattern(case_)
if hasIntPattern && isFloatPattern {
return nil, ErrJSONSchemaMixingIntFloatNotSupported
} else if hasFloatPattern && isIntPattern {
return nil, ErrJSONSchemaMixingIntFloatNotSupported
}
hasIntPattern = isIntPattern
hasFloatPattern = isFloatPattern
disjointUnionCases = append(disjointUnionCases, case_)
}
disjointUnionPattern := NewDisjointUnionPattern(disjointUnionCases, nil)
if basePattern == ANYVAL_PATTERN {
return disjointUnionPattern, nil
}
return simplifyIntersection([]Pattern{basePattern, disjointUnionPattern}), nil
}
if schema.If != nil || schema.Then != nil || schema.Else != nil {
return nil, errors.New("conditional schemas are not supported yet")
}
}
if len(schema.Constant) > 0 {
constant := schema.Constant[0]
return convertConstSchemaValueToPattern(constant)
}
if len(schema.Enum) > 0 {
var err error
unionCases := make([]Pattern, len(schema.Enum))
for i, constant := range schema.Enum {
unionCases[i], err = convertConstSchemaValueToPattern(constant)
if err != nil {
return nil, err
}
}
return NewUnionPattern(unionCases, nil), nil
}
allowNumber := false
allowInteger := false
allowNull := false
allowBoolean := false
allowString := false
allowObject := false
allowArray := false
ignoreNonArray := false
ignoreNonNumber := false
ignoreNonString := false
ignoreNonObject := false
var unionCases []Pattern
switch len(schema.Types) {
case 0:
if schema.Contains != nil {
ignoreNonArray = true
allowArray = schema.Contains.Always == nil || *schema.Contains.Always
break
}
if schema.MinItems >= 0 || schema.MaxItems >= 0 {
ignoreNonArray = true
allowArray = true
}
if schema.Maximum != nil || schema.ExclusiveMaximum != nil || schema.Minimum != nil ||
schema.ExclusiveMinimum != nil || schema.MultipleOf != nil {
ignoreNonNumber = true
allowNumber = true
}
if schema.Pattern != nil || schema.Format != "" || schema.MinLength != -1 || schema.MaxLength != -1 {
ignoreNonString = true
allowString = true
}
//note: schema.ContainsEval true for objects, why ?
if schema.AdditionalItems != nil || schema.Items != nil ||
schema.MaxItems >= 0 || schema.PrefixItems != nil || schema.Contains != nil ||
schema.MinContains != 1 || schema.MaxContains >= 0 ||
schema.UnevaluatedItems != nil {
allowArray = true
}
if schema.Dependencies != nil {
allowNumber = true
allowInteger = true
allowNull = true
allowBoolean = true
allowString = true
allowObject = true
allowArray = true
break
}
if schema.MinProperties >= 0 || schema.MaxProperties >= 0 || schema.Required != nil ||
schema.Properties != nil || schema.PropertyNames != nil || schema.RegexProperties ||
schema.PatternProperties != nil || schema.AdditionalProperties != nil ||
schema.DependentRequired != nil {
ignoreNonObject = true
allowObject = true
}
default:
for _, typename := range schema.Types {
switch typename {
case "number":
allowNumber = true
case "integer":
allowInteger = true
case "null":
allowNull = true
case "boolean":
allowBoolean = true
case "string":
allowString = true
case "array":
allowArray = true
case "object":
allowObject = true
}
}
}
if !(allowBoolean || allowNumber || allowInteger || allowNull || allowString || allowArray || allowObject) {
return ANYVAL_PATTERN, nil
}
if allowNumber {
hasSpecifiedRange := false
floatRange := FloatRange{
start: math.Inf(-1),
end: math.Inf(1),
}
if schema.Minimum != nil {
hasSpecifiedRange = true
min, _ := schema.Minimum.Float64()
floatRange.start = min
} else if schema.ExclusiveMinimum != nil {
hasSpecifiedRange = true
exclusiveMinimum, _ := schema.ExclusiveMinimum.Float64()
floatRange.start = math.Nextafter(exclusiveMinimum, math.Inf(1))
}
if schema.Maximum != nil {
hasSpecifiedRange = true
max, _ := schema.Maximum.Float64()
floatRange.end = max
floatRange.inclusiveEnd = true
} else if schema.ExclusiveMaximum != nil {
hasSpecifiedRange = true
exclusiveMaximum, _ := schema.ExclusiveMaximum.Float64()
if math.IsInf(exclusiveMaximum, 1) {
floatRange.end = exclusiveMaximum
} else {
floatRange.end = math.Nextafter(exclusiveMaximum, math.Inf(-1))
}
}
var multipleOf float64 = -1
if schema.MultipleOf != nil {
multipleOf, _ = schema.MultipleOf.Float64()
}
if multipleOf == -1 && !hasSpecifiedRange {
unionCases = append(unionCases, FLOAT_PATTERN)
} else {
pattern := NewFloatRangePattern(floatRange, multipleOf)
unionCases = append(unionCases, pattern)
}
} else if allowInteger {
hasRange := false
intRange := IntRange{
start: math.MinInt64,
end: math.MaxInt64,
step: 1,
}
if schema.Minimum != nil {
min, _ := schema.Minimum.Float64()
intRange.start = int64(math.Floor(min))
hasRange = true
} else if schema.ExclusiveMinimum != nil {
exclusiveMinimum, _ := schema.Minimum.Float64()
min := math.Nextafter(exclusiveMinimum, math.Inf(1))
intRange.start = int64(math.Floor(min))
hasRange = true
}
if schema.Maximum != nil {
exclusiveMax, _ := schema.Maximum.Float64()
intRange.end = int64(math.Ceil(exclusiveMax))
hasRange = true
} else if schema.ExclusiveMaximum != nil {
max, _ := schema.Maximum.Float64()
intRange.end = int64(math.Ceil(max))
if intRange.end == math.MinInt64 {
return nil, errors.New("invalid 'exclusiveMaximum'")
}
intRange.end--
hasRange = true
}
var multipleOf int64 = -1
var multipleOfFloat Float = -1
if schema.MultipleOf != nil {
n, _ := schema.MultipleOf.Float64()
if utils.IsWholeInt64(n) {
multipleOf = int64(n)
} else {
multipleOfFloat = Float(n)
}
}
var pattern Pattern
if !hasRange {
pattern = INT_PATTERN
} else if multipleOfFloat != -1 {
pattern = NewIntRangePatternFloatMultiple(intRange, multipleOfFloat)
} else {
pattern = NewIntRangePattern(intRange, multipleOf)
}
unionCases = append(unionCases, pattern)
}
if allowNull {
unionCases = append(unionCases, NIL_PATTERN)
}
if allowBoolean {
unionCases = append(unionCases, BOOL_PATTERN)
}
if allowString {
var pattern Pattern
var lengthRange = IntRange{}
var hasLengthRange bool
if schema.MinLength >= 0 {
lengthRange.start = int64(schema.MinLength)
hasLengthRange = true
if schema.MaxLength < 0 {
lengthRange.end = math.MaxInt64
}
}
if schema.MaxLength >= 0 {
lengthRange.end = int64(schema.MaxLength)
hasLengthRange = true
if schema.MinLength < 0 {
lengthRange.start = 0
}
}
if schema.Pattern != nil {
regexPattern := NewRegexPattern(schema.Pattern.String())
if hasLengthRange {
regexPattern = regexPattern.WithLengthRange(lengthRange)
}
pattern = regexPattern
} else if schema.Format != "" {
// if hasLengthRange {
// return nil, errors.New("'minLength' & 'maxLength' are not supported yet when 'format' is set")
// }
// switch schema.Format {
// case "time":
// //note: some informations are lost (length, ...)
// pattern = DATE_PATTERN
// case "duration":
// //note: some informations are lost (length, ...)
// pattern = DURATION_PATTERN
// case "email-address":
// //note: some informations are lost (length, ...)
// pattern = EMAIL_ADDR_PATTERN
// case "uri":
// pattern = NewUnionPattern([]Pattern{URL_PATTERN, STRLIKE_PATTERN}, nil)
// }
}
if pattern == nil {
if hasLengthRange {
pattern = NewLengthCheckingStringPattern(lengthRange.start, lengthRange.InclusiveEnd())
} else {
pattern = STR_PATTERN
}
}
unionCases = append(unionCases, pattern)
}
if allowObject {
anyObject := true
if schema.MinProperties >= 0 || schema.MaxProperties >= 0 {
anyObject = false
return nil, errors.New("'minProperties' & 'maxProperties' are not supported")
}
if schema.RegexProperties {
anyObject = false
return nil, errors.New("'regexProperties' is not supported")
}
if len(schema.PatternProperties) > 0 {
anyObject = false
return nil, errors.New("'patternProperties' is not supported yet")
}
if schema.UnevaluatedProperties != nil {
anyObject = false
return nil, errors.New("'unevaluatedProperties' is not supported yet")
}
if len(schema.DependentRequired) > 0 || len(schema.DependentSchemas) > 0 {
anyObject = false
return nil, errors.New("'dependentRequired' & 'dependentSchemas' are not supported yet")
}
exact := !schema.RegexProperties && len(schema.PatternProperties) == 0
switch v := schema.AdditionalProperties.(type) {
case *jsonschema.Schema:
anyObject = false
return nil, errors.New("'additionalProperties' is not fully supported yet, only 'additionalProperties': <bool> is allowed")
case bool:
anyObject = false
if v {
exact = false
}
case nil:
exact = false
}
var entries []ObjectPatternEntry
for name, propSchema := range schema.Properties {
anyObject = false
propPattern, err := convertJsonSchemaToPattern(propSchema, nil, false, false, depth+1)
if err != nil {
return nil, err
}
entry := ObjectPatternEntry{
Name: name,
Pattern: propPattern,
}
required := false
for _, requiredPropName := range schema.Required {
if requiredPropName == name {
required = true
}
}
if !required {
entry.IsOptional = true
}
entries = append(entries, entry)
}
for _, requiredPropName := range schema.Required {
anyObject = false
//Add entry if not present.
if !slices.ContainsFunc(entries, func(entry ObjectPatternEntry) bool { return entry.Name == requiredPropName }) {
entries = append(entries, ObjectPatternEntry{
Name: requiredPropName,
Pattern: SERIALIZABLE_PATTERN,
})
}
}
if len(schema.Dependencies) > 0 {
anyObject = false
for dependentKey, deps := range schema.Dependencies {
//Make sure the dependent key is present in the entries.
if !slices.ContainsFunc(entries, func(entry ObjectPatternEntry) bool { return entry.Name == dependentKey }) {
entries = append(entries, ObjectPatternEntry{
Name: dependentKey,
Pattern: SERIALIZABLE_PATTERN,
IsOptional: true,
})
}
switch d := deps.(type) {
case []string:
for i := range entries {
//Add dependencies to the entry.
if entries[i].Name == dependentKey {
entries[i].Dependencies = PropertyDependencies{RequiredKeys: d}
break
}
}
case *jsonschema.Schema:
if d.Always != nil {
return nil, errors.New("'dependencies' with boolean schemas are not supported")
}
var propDependencies PropertyDependencies
if d.Properties == nil || d.AdditionalProperties != nil ||
d.RegexProperties || d.UnevaluatedProperties != nil ||
d.PatternProperties != nil || d.MaxProperties != -1 ||
d.MinProperties != -1 || d.DependentRequired != nil {
return nil, errors.New("'dependencies' with schemas are not fully supported")
}
dependenciesPattern, err := convertJsonSchemaToPattern(d, nil, false, true, depth+1)
if err != nil {
return nil, fmt.Errorf("failed to convert dependency pattern for property %q", dependentKey)
}
propDependencies.Pattern = dependenciesPattern
for i := range entries {
//Add dependencies to the entry.
if entries[i].Name == dependentKey {
entries[i].Dependencies = propDependencies
break
}
}
default:
}
}
}
if anyObject {
unionCases = append(unionCases, OBJECT_PATTERN)
} else {
objectPattern := NewObjectPattern(!exact, entries)
unionCases = append(unionCases, objectPattern)
}
}
if allowArray {
//TODO: support schema.Items2020
if schema.UniqueItems {
return nil, errors.New("'uniqueItems' is not supported yet")
}
if schema.UnevaluatedItems != nil {
return nil, errors.New("'UnevaluatedItems' is not supported yet")
}
//TODO: schema.ContainsEval ?
switch schema.AdditionalItems.(type) {
case nil:
default:
return nil, errors.New("'additionalItems' is not supported yet")
}
var generalElementPattern Pattern = SERIALIZABLE_PATTERN
var elementPatterns []Pattern
switch items := schema.Items.(type) {
case *jsonschema.Schema:
var err error
generalElementPattern, err = convertJsonSchemaToPattern(items, nil, false, false, depth+1)
if err != nil {
return nil, err
}
case []*jsonschema.Schema:
for _, item := range items {
elementPattern, err := convertJsonSchemaToPattern(item, nil, false, false, depth+1)
if err != nil {
return nil, err
}
elementPatterns = append(elementPatterns, elementPattern)
}
case nil:
generalElementPattern = SERIALIZABLE_PATTERN
}
var listPattern *ListPattern
if elementPatterns != nil {
listPattern = NewListPattern(elementPatterns)
} else {
listPattern = NewListPatternOf(generalElementPattern)
}
if schema.MinItems != -1 || schema.MaxItems != -1 {
minItems := schema.MinItems
if minItems < 0 {
minItems = 0
}
maxItems := schema.MaxItems
if maxItems < 0 {
maxItems = math.MaxInt64
}
listPattern = listPattern.WithMinMaxElements(minItems, maxItems)
}
if schema.Contains == nil {
unionCases = append(unionCases, listPattern)
} else if schema.Contains.Always == nil {
containedElementPattern, err := convertJsonSchemaToPattern(schema.Contains, nil, false, false, depth+1)
if err != nil {
return nil, err
}
listPattern = listPattern.WithElement(containedElementPattern)
unionCases = append(unionCases, listPattern)
} else if *schema.Contains.Always {
if listPattern.MinElementCount() == 0 {
unionCases = append(unionCases, listPattern.WithMinElements(1))
} else {
unionCases = append(unionCases, listPattern)
}
} else {
panic(ErrUnreachable)
}
}
if len(unionCases) == 1 {
if !removeIgnore {
if ignoreNonArray {
if !allowArray {
return NewDifferencePattern(ANYVAL_PATTERN, LIST_PATTERN), nil
}
unionCases = append(unionCases, NewDifferencePattern(ANYVAL_PATTERN, LIST_PATTERN))
return NewDisjointUnionPattern(unionCases, nil), nil
} else if ignoreNonObject {
unionCases = append(unionCases, NewDifferencePattern(ANYVAL_PATTERN, OBJECT_PATTERN))
return NewDisjointUnionPattern(unionCases, nil), nil
} else if ignoreNonNumber {
unionCases = append(unionCases, NewDifferencePattern(ANYVAL_PATTERN, FLOAT_PATTERN))
return NewDisjointUnionPattern(unionCases, nil), nil
} else if ignoreNonString {
unionCases = append(unionCases, NewDifferencePattern(ANYVAL_PATTERN, STR_PATTERN))
return NewDisjointUnionPattern(unionCases, nil), nil
}
}
return unionCases[0], nil
}
return NewUnionPattern(unionCases, nil), nil
}
func convertJsonSchemaTypeToPattern(typename string) (Pattern, error) {
pattern, ok := JSON_SCHEMA_TYPE_TO_PATTERN[typename]
if ok {
return pattern, nil
}
return nil, ErrInvalidOrUnsupportedJsonSchema
}
func convertConstSchemaValueToPattern(jsonValue any) (_ Pattern, err error) {
switch c := jsonValue.(type) {
case map[string]any:
var entries []ObjectPatternEntry
for k, v := range c {
if k == "$id" {
return nil, errors.New("const values with an $id property are not supported yet")
}
pattern, err := convertConstSchemaValueToPattern(v)
if err != nil {
return nil, err
}
entries = append(entries, ObjectPatternEntry{
Name: k,
Pattern: pattern,
})
}
return NewExactObjectPattern(entries), nil
case []any:
var elements []Pattern
for _, e := range c {
p, err := convertConstSchemaValueToPattern(e)
if err != nil {
return nil, err
}
elements = append(elements, p)
}
return NewListPattern(elements), nil
case json.Number:
float, _ := c.Float64()
return NewExactValuePattern(Float(float)), nil
case float64:
return NewExactValuePattern(Float(c)), nil
case nil:
return NIL_PATTERN, nil
case bool:
return NewExactValuePattern(Bool(c)), nil
case string:
return NewExactValuePattern(String(c)), nil
default:
return nil, fmt.Errorf("cannot convert value of type %T to Inox Value", c)
}
}