-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate.go
412 lines (368 loc) · 10.7 KB
/
validate.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
package jtd
import (
"errors"
"math"
"strconv"
"time"
)
// ValidateSettings are settings that configure ValidateWithSettings.
type ValidateSettings struct {
// The maximum number of refs to recursively follow before returning
// ErrMaxDepthExceeded. Zero disables a max depth altogether.
MaxDepth int
// The maximum number of validation errors to return. Zero disables a max
// number of errors altogether.
MaxErrors int
}
// ValidateOption is an option you can pass to Validate.
type ValidateOption func(*ValidateSettings)
// WithMaxDepth sets the the MaxDepth option of ValidateSettings.
func WithMaxDepth(maxDepth int) ValidateOption {
return func(settings *ValidateSettings) {
settings.MaxDepth = maxDepth
}
}
// WithMaxErrors sets the the MaxErrors option of ValidateSettings.
func WithMaxErrors(maxErrors int) ValidateOption {
return func(settings *ValidateSettings) {
settings.MaxErrors = maxErrors
}
}
// ValidateError is a validation error returned from Validate.
//
// This corresponds to a standard error indicator from the JSON Typedef
// specification.
type ValidateError struct {
// Path to the part of the instance that was invalid.
InstancePath []string
// Path to the part of the schema that rejected the instance.
SchemaPath []string
}
// ErrMaxDepthExceeded is the error returned from Validate if too many refs are
// recursively followed.
//
// The maximum depth of refs to follow is controlled by MaxErrors in
// ValidateSettings.
var ErrMaxDepthExceeded = errors.New("jtd: max depth exceeded")
// Validate validates a schema against an instance (or "input").
//
// Returns ErrMaxDepthExceeded if too many refs are recursively followed while
// validating. Otherwise, returns a set of ValidateError, in conformance with
// the JSON Typedef specification.
func Validate(schema Schema, instance interface{}, opts ...ValidateOption) ([]ValidateError, error) {
settings := ValidateSettings{}
for _, opt := range opts {
opt(&settings)
}
return ValidateWithSettings(settings, schema, instance)
}
// ValidateWithSettings validates a schema against an instance, using a set of
// settings.
//
// Returns ErrMaxDepthExceeded if too many refs are recursively followed while
// validating. Otherwise, returns a set of ValidateError, in conformance with
// the JSON Typedef specification.
func ValidateWithSettings(settings ValidateSettings, schema Schema, instance interface{}) ([]ValidateError, error) {
state := validateState{
Errors: []ValidateError{},
InstanceTokens: []string{},
SchemaTokens: [][]string{[]string{}},
Root: schema,
Settings: settings,
}
// errMaxErrorsReached is just an internal error used to quickly abort further
// validation. It is not an actual error for the end user, just a
// circuit-breaker used by validate internally.
if err := validate(&state, schema, instance, nil); err != nil && err != errMaxErrorsReached {
return nil, err
}
return state.Errors, nil
}
func validate(state *validateState, schema Schema, instance interface{}, parentTag *string) error {
if schema.Nullable && instance == nil {
return nil
}
switch schema.Form() {
case FormEmpty:
return nil
case FormRef:
if len(state.SchemaTokens) == state.Settings.MaxDepth {
return ErrMaxDepthExceeded
}
state.SchemaTokens = append(state.SchemaTokens, []string{"definitions", *schema.Ref})
if err := validate(state, state.Root.Definitions[*schema.Ref], instance, nil); err != nil {
return err
}
state.SchemaTokens = state.SchemaTokens[:len(state.SchemaTokens)-1]
case FormType:
state.pushSchemaToken("type")
switch schema.Type {
case TypeBoolean:
if _, ok := instance.(bool); !ok {
if err := state.pushError(); err != nil {
return err
}
}
case TypeFloat32, TypeFloat64:
if _, ok := instance.(float64); !ok {
if err := state.pushError(); err != nil {
return err
}
}
case TypeInt8:
if err := validateInt(state, instance, -128.0, 127.0); err != nil {
return err
}
case TypeUint8:
if err := validateInt(state, instance, 0.0, 255.0); err != nil {
return err
}
case TypeInt16:
if err := validateInt(state, instance, -32768.0, 32767.0); err != nil {
return err
}
case TypeUint16:
if err := validateInt(state, instance, 0.0, 65535.0); err != nil {
return err
}
case TypeInt32:
if err := validateInt(state, instance, -2147483648.0, 2147483647.0); err != nil {
return err
}
case TypeUint32:
if err := validateInt(state, instance, 0.0, 4294967295.0); err != nil {
return err
}
case TypeString:
if _, ok := instance.(string); !ok {
if err := state.pushError(); err != nil {
return err
}
}
case TypeTimestamp:
if s, ok := instance.(string); ok {
if _, err := time.Parse(time.RFC3339, s); err != nil {
if err := state.pushError(); err != nil {
return err
}
}
} else {
if err := state.pushError(); err != nil {
return err
}
}
}
state.popSchemaToken()
case FormEnum:
state.pushSchemaToken("enum")
if s, ok := instance.(string); ok {
ok := false
for _, value := range schema.Enum {
if s == value {
ok = true
}
}
if !ok {
if err := state.pushError(); err != nil {
return err
}
}
} else {
if err := state.pushError(); err != nil {
return err
}
}
state.popSchemaToken()
case FormElements:
state.pushSchemaToken("elements")
if arr, ok := instance.([]interface{}); ok {
for i, subInstance := range arr {
state.pushInstanceToken(strconv.Itoa(i))
if err := validate(state, *schema.Elements, subInstance, nil); err != nil {
return err
}
state.popInstanceToken()
}
} else {
if err := state.pushError(); err != nil {
return err
}
}
state.popSchemaToken()
case FormProperties:
if obj, ok := instance.(map[string]interface{}); ok {
state.pushSchemaToken("properties")
for key, subSchema := range schema.Properties {
state.pushSchemaToken(key)
if subInstance, ok := obj[key]; ok {
state.pushInstanceToken(key)
if err := validate(state, subSchema, subInstance, nil); err != nil {
return err
}
state.popInstanceToken()
} else {
if err := state.pushError(); err != nil {
return err
}
}
state.popSchemaToken()
}
state.popSchemaToken()
state.pushSchemaToken("optionalProperties")
for key, subSchema := range schema.OptionalProperties {
state.pushSchemaToken(key)
if subInstance, ok := obj[key]; ok {
state.pushInstanceToken(key)
if err := validate(state, subSchema, subInstance, nil); err != nil {
return err
}
state.popInstanceToken()
}
state.popSchemaToken()
}
state.popSchemaToken()
if !schema.AdditionalProperties {
for key := range obj {
if parentTag != nil && key == *parentTag {
continue
}
requiredOk := false
optionalOk := false
if schema.Properties != nil {
_, requiredOk = schema.Properties[key]
}
if schema.OptionalProperties != nil {
_, optionalOk = schema.OptionalProperties[key]
}
if !requiredOk && !optionalOk {
state.pushInstanceToken(key)
if err := state.pushError(); err != nil {
return err
}
state.popInstanceToken()
}
}
}
} else {
if schema.Properties != nil {
state.pushSchemaToken("properties")
} else {
state.pushSchemaToken("optionalProperties")
}
if err := state.pushError(); err != nil {
return err
}
state.popSchemaToken()
}
case FormValues:
state.pushSchemaToken("values")
if obj, ok := instance.(map[string]interface{}); ok {
for key, subInstance := range obj {
state.pushInstanceToken(key)
if err := validate(state, *schema.Values, subInstance, nil); err != nil {
return err
}
state.popInstanceToken()
}
} else {
if err := state.pushError(); err != nil {
return err
}
}
state.popSchemaToken()
case FormDiscriminator:
if obj, ok := instance.(map[string]interface{}); ok {
if tag, ok := obj[schema.Discriminator]; ok {
if tagStr, ok := tag.(string); ok {
if mapping, ok := schema.Mapping[tagStr]; ok {
state.pushSchemaToken("mapping")
state.pushSchemaToken(tagStr)
if err := validate(state, mapping, instance, &schema.Discriminator); err != nil {
return err
}
state.popSchemaToken()
state.popSchemaToken()
} else {
state.pushSchemaToken("mapping")
state.pushInstanceToken(schema.Discriminator)
if err := state.pushError(); err != nil {
return err
}
state.popInstanceToken()
state.popSchemaToken()
}
} else {
state.pushSchemaToken("discriminator")
state.pushInstanceToken(schema.Discriminator)
if err := state.pushError(); err != nil {
return err
}
state.popInstanceToken()
state.popSchemaToken()
}
} else {
state.pushSchemaToken("discriminator")
if err := state.pushError(); err != nil {
return err
}
state.popSchemaToken()
}
} else {
state.pushSchemaToken("discriminator")
if err := state.pushError(); err != nil {
return err
}
state.popSchemaToken()
}
}
return nil
}
func validateInt(state *validateState, instance interface{}, min, max float64) error {
if n, ok := instance.(float64); ok {
if i, f := math.Modf(n); f != 0.0 || i < min || i > max {
if err := state.pushError(); err != nil {
return err
}
}
} else {
if err := state.pushError(); err != nil {
return err
}
}
return nil
}
var errMaxErrorsReached = errors.New("jtd internal: max errors reached")
type validateState struct {
Errors []ValidateError
InstanceTokens []string
SchemaTokens [][]string
Root Schema
Settings ValidateSettings
}
func (vs *validateState) pushInstanceToken(token string) {
vs.InstanceTokens = append(vs.InstanceTokens, token)
}
func (vs *validateState) popInstanceToken() {
vs.InstanceTokens = vs.InstanceTokens[:len(vs.InstanceTokens)-1]
}
func (vs *validateState) pushSchemaToken(token string) {
vs.SchemaTokens[len(vs.SchemaTokens)-1] = append(vs.SchemaTokens[len(vs.SchemaTokens)-1], token)
}
func (vs *validateState) popSchemaToken() {
last := vs.SchemaTokens[len(vs.SchemaTokens)-1]
vs.SchemaTokens[len(vs.SchemaTokens)-1] = last[:len(last)-1]
}
func (vs *validateState) pushError() error {
instanceTokens := make([]string, len(vs.InstanceTokens))
copy(instanceTokens, vs.InstanceTokens)
schemaTokens := make([]string, len(vs.SchemaTokens[len(vs.SchemaTokens)-1]))
copy(schemaTokens, vs.SchemaTokens[len(vs.SchemaTokens)-1])
vs.Errors = append(vs.Errors, ValidateError{
InstancePath: instanceTokens,
SchemaPath: schemaTokens,
})
if len(vs.Errors) == vs.Settings.MaxErrors {
return errMaxErrorsReached
}
return nil
}