-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_of.go
88 lines (70 loc) · 1.69 KB
/
all_of.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
package jsonschema
import (
"fmt"
"reflect"
"github.com/aacebo/jsonschema/coerce"
)
// https://json-schema.org/understanding-json-schema/reference/combining#allOf
func allOf(key string) Keyword {
return Keyword{
Compile: func(ns *Namespace, ctx Context, config reflect.Value) []SchemaError {
errs := []SchemaError{}
if config.Kind() != reflect.Slice {
errs = append(errs, SchemaError{
Path: ctx.Path,
Keyword: key,
Message: `should be a "[]Schema"`,
})
return errs
}
for i := 0; i < config.Len(); i++ {
index := coerce.Map(config.Index(i))
path := fmt.Sprintf("%s/%s/%d", ctx.Path, key, i)
if index.Kind() != reflect.Map {
errs = append(errs, SchemaError{
Path: path,
Keyword: key,
Message: `should be a "Schema"`,
})
continue
}
_errs := ns.compile(
ctx.ID,
path,
index.Interface().(map[string]any),
)
if len(_errs) > 0 {
errs = append(errs, _errs...)
}
}
return errs
},
Validate: func(ns *Namespace, ctx Context, config reflect.Value, value reflect.Value) []SchemaError {
errs := []SchemaError{}
if config.Kind() != reflect.Slice && config.Kind() != reflect.Array {
return errs
}
for i := 0; i < config.Len(); i++ {
index := coerce.Map(config.Index(i))
if index.Kind() != reflect.Map {
continue
}
_errs := ns.validate(
ctx.ID,
ctx.Path,
index.Interface().(map[string]any),
value.Interface(),
)
if len(_errs) > 0 {
errs = append(errs, SchemaError{
Path: ctx.Path,
Keyword: key,
Message: "must match all schemas",
})
return errs
}
}
return errs
},
}
}