-
Notifications
You must be signed in to change notification settings - Fork 54
/
keywords_boolean.go
313 lines (270 loc) · 7.46 KB
/
keywords_boolean.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
package jsonschema
import (
"context"
"encoding/json"
"strconv"
jptr "github.com/qri-io/jsonpointer"
)
// AllOf defines the allOf JSON Schema keyword
type AllOf []*Schema
// NewAllOf allocates a new AllOf keyword
func NewAllOf() Keyword {
return &AllOf{}
}
// Register implements the Keyword interface for AllOf
func (a *AllOf) Register(uri string, registry *SchemaRegistry) {
for _, sch := range *a {
sch.Register(uri, registry)
}
}
// Resolve implements the Keyword interface for AllOf
func (a *AllOf) Resolve(pointer jptr.Pointer, uri string) *Schema {
if pointer == nil {
return nil
}
current := pointer.Head()
if current == nil {
return nil
}
pos, err := strconv.Atoi(*current)
if err != nil {
return nil
}
if pos < 0 || pos >= len(*a) {
return nil
}
return (*a)[pos].Resolve(pointer.Tail(), uri)
}
// ValidateKeyword implements the Keyword interface for AllOf
func (a *AllOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[AllOf] Validating")
stateCopy := currentState.NewSubState()
stateCopy.ClearState()
invalid := false
for i, sch := range *a {
subState := currentState.NewSubState()
subState.ClearState()
subState.DescendBase("allOf", strconv.Itoa(i))
subState.DescendRelative("allOf", strconv.Itoa(i))
subState.Errs = &[]KeyError{}
sch.ValidateKeyword(ctx, subState, data)
currentState.AddSubErrors(*subState.Errs...)
stateCopy.UpdateEvaluatedPropsAndItems(subState)
if !subState.IsValid() {
invalid = true
}
}
if !invalid {
currentState.UpdateEvaluatedPropsAndItems(stateCopy)
}
}
// JSONProp implements the JSONPather for AllOf
func (a AllOf) JSONProp(name string) interface{} {
idx, err := strconv.Atoi(name)
if err != nil {
return nil
}
if idx > len(a) || idx < 0 {
return nil
}
return a[idx]
}
// JSONChildren implements the JSONContainer interface for AllOf
func (a AllOf) JSONChildren() (res map[string]JSONPather) {
res = map[string]JSONPather{}
for i, sch := range a {
res[strconv.Itoa(i)] = sch
}
return
}
// AnyOf defines the anyOf JSON Schema keyword
type AnyOf []*Schema
// NewAnyOf allocates a new AnyOf keyword
func NewAnyOf() Keyword {
return &AnyOf{}
}
// Register implements the Keyword interface for AnyOf
func (a *AnyOf) Register(uri string, registry *SchemaRegistry) {
for _, sch := range *a {
sch.Register(uri, registry)
}
}
// Resolve implements the Keyword interface for AnyOf
func (a *AnyOf) Resolve(pointer jptr.Pointer, uri string) *Schema {
if pointer == nil {
return nil
}
current := pointer.Head()
if current == nil {
return nil
}
pos, err := strconv.Atoi(*current)
if err != nil {
return nil
}
if pos < 0 || pos >= len(*a) {
return nil
}
return (*a)[pos].Resolve(pointer.Tail(), uri)
}
// ValidateKeyword implements the Keyword interface for AnyOf
func (a *AnyOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[AnyOf] Validating")
for i, sch := range *a {
subState := currentState.NewSubState()
subState.ClearState()
subState.DescendBase("anyOf", strconv.Itoa(i))
subState.DescendRelative("anyOf", strconv.Itoa(i))
subState.Errs = &[]KeyError{}
sch.ValidateKeyword(ctx, subState, data)
if subState.IsValid() {
currentState.UpdateEvaluatedPropsAndItems(subState)
return
}
}
currentState.AddError(data, "did Not match any specified AnyOf schemas")
}
// JSONProp implements the JSONPather for AnyOf
func (a AnyOf) JSONProp(name string) interface{} {
idx, err := strconv.Atoi(name)
if err != nil {
return nil
}
if idx > len(a) || idx < 0 {
return nil
}
return a[idx]
}
// JSONChildren implements the JSONContainer interface for AnyOf
func (a AnyOf) JSONChildren() (res map[string]JSONPather) {
res = map[string]JSONPather{}
for i, sch := range a {
res[strconv.Itoa(i)] = sch
}
return
}
// OneOf defines the oneOf JSON Schema keyword
type OneOf []*Schema
// NewOneOf allocates a new OneOf keyword
func NewOneOf() Keyword {
return &OneOf{}
}
// Register implements the Keyword interface for OneOf
func (o *OneOf) Register(uri string, registry *SchemaRegistry) {
for _, sch := range *o {
sch.Register(uri, registry)
}
}
// Resolve implements the Keyword interface for OneOf
func (o *OneOf) Resolve(pointer jptr.Pointer, uri string) *Schema {
if pointer == nil {
return nil
}
current := pointer.Head()
if current == nil {
return nil
}
pos, err := strconv.Atoi(*current)
if err != nil {
return nil
}
if pos < 0 || pos >= len(*o) {
return nil
}
return (*o)[pos].Resolve(pointer.Tail(), uri)
}
// ValidateKeyword implements the Keyword interface for OneOf
func (o *OneOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[OneOf] Validating")
matched := false
stateCopy := currentState.NewSubState()
stateCopy.ClearState()
for i, sch := range *o {
subState := currentState.NewSubState()
subState.ClearState()
subState.DescendBase("oneOf", strconv.Itoa(i))
subState.DescendRelative("oneOf", strconv.Itoa(i))
subState.Errs = &[]KeyError{}
sch.ValidateKeyword(ctx, subState, data)
stateCopy.UpdateEvaluatedPropsAndItems(subState)
if subState.IsValid() {
if matched {
currentState.AddError(data, "matched more than one specified OneOf schemas")
return
}
matched = true
}
}
if !matched {
currentState.AddError(data, "did not match any of the specified OneOf schemas")
} else {
currentState.UpdateEvaluatedPropsAndItems(stateCopy)
}
}
// JSONProp implements the JSONPather for OneOf
func (o OneOf) JSONProp(name string) interface{} {
idx, err := strconv.Atoi(name)
if err != nil {
return nil
}
if idx > len(o) || idx < 0 {
return nil
}
return o[idx]
}
// JSONChildren implements the JSONContainer interface for OneOf
func (o OneOf) JSONChildren() (res map[string]JSONPather) {
res = map[string]JSONPather{}
for i, sch := range o {
res[strconv.Itoa(i)] = sch
}
return
}
// Not defines the not JSON Schema keyword
type Not Schema
// NewNot allocates a new Not keyword
func NewNot() Keyword {
return &Not{}
}
// Register implements the Keyword interface for Not
func (n *Not) Register(uri string, registry *SchemaRegistry) {
(*Schema)(n).Register(uri, registry)
}
// Resolve implements the Keyword interface for Not
func (n *Not) Resolve(pointer jptr.Pointer, uri string) *Schema {
return (*Schema)(n).Resolve(pointer, uri)
}
// ValidateKeyword implements the Keyword interface for Not
func (n *Not) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[Not] Validating")
subState := currentState.NewSubState()
subState.DescendBase("not")
subState.DescendRelative("not")
subState.Errs = &[]KeyError{}
sch := Schema(*n)
sch.ValidateKeyword(ctx, subState, data)
if subState.IsValid() {
currentState.AddError(data, "result was valid, ('not') expected invalid")
}
}
// JSONProp implements the JSONPather for Not
func (n Not) JSONProp(name string) interface{} {
return Schema(n).JSONProp(name)
}
// JSONChildren implements the JSONContainer interface for Not
func (n Not) JSONChildren() (res map[string]JSONPather) {
return Schema(n).JSONChildren()
}
// UnmarshalJSON implements the json.Unmarshaler interface for Not
func (n *Not) UnmarshalJSON(data []byte) error {
var sch Schema
if err := json.Unmarshal(data, &sch); err != nil {
return err
}
*n = Not(sch)
return nil
}
// MarshalJSON implements the json.Marshaler interface for Not
func (n Not) MarshalJSON() ([]byte, error) {
return json.Marshal(Schema(n))
}