-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
validate_test.go
732 lines (717 loc) · 17.6 KB
/
validate_test.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
package huma
import (
"encoding/json"
"reflect"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var validateTests = []struct {
name string
typ reflect.Type
input any
mode ValidateMode
errs []string
panic string
}{
{
name: "bool success",
typ: reflect.TypeOf(true),
input: true,
},
{
name: "expected bool",
typ: reflect.TypeOf(true),
input: 1.23,
errs: []string{"expected boolean"},
},
{
name: "int success",
typ: reflect.TypeOf(0),
input: 0,
},
{
name: "float64 success",
typ: reflect.TypeOf(0),
input: float64(0),
},
{
name: "int64 success",
typ: reflect.TypeOf(0),
input: int64(0),
},
{
name: "expected number int",
typ: reflect.TypeOf(0),
input: "",
errs: []string{"expected number"},
},
{
name: "expected number float64",
typ: reflect.TypeOf(float64(0)),
input: "",
errs: []string{"expected number"},
},
{
name: "minimum success",
typ: reflect.TypeOf(struct {
Value int `json:"value" minimum:"1"`
}{}),
input: map[string]any{"value": 1},
},
{
name: "minimum fail",
typ: reflect.TypeOf(struct {
Value int `json:"value" minimum:"1"`
}{}),
input: map[string]any{"value": 0},
errs: []string{"expected number >= 1"},
},
{
name: "exclusive minimum success",
typ: reflect.TypeOf(struct {
Value int `json:"value" exclusiveMinimum:"1"`
}{}),
input: map[string]any{"value": 2},
},
{
name: "exclusive minimum fail",
typ: reflect.TypeOf(struct {
Value int `json:"value" exclusiveMinimum:"1"`
}{}),
input: map[string]any{"value": 1},
errs: []string{"expected number > 1"},
},
{
name: "maximum success",
typ: reflect.TypeOf(struct {
Value int `json:"value" maximum:"1"`
}{}),
input: map[string]any{"value": 1},
},
{
name: "maximum fail",
typ: reflect.TypeOf(struct {
Value int `json:"value" maximum:"1"`
}{}),
input: map[string]any{"value": 2},
errs: []string{"expected number <= 1"},
},
{
name: "exclusive maximum success",
typ: reflect.TypeOf(struct {
Value int `json:"value" exclusiveMaximum:"1"`
}{}),
input: map[string]any{"value": 0},
},
{
name: "exclusive maximum fail",
typ: reflect.TypeOf(struct {
Value int `json:"value" exclusiveMaximum:"1"`
}{}),
input: map[string]any{"value": 1},
errs: []string{"expected number < 1"},
},
{
name: "multiple of success",
typ: reflect.TypeOf(struct {
Value int `json:"value" multipleOf:"5"`
}{}),
input: map[string]any{"value": 10},
},
{
name: "multiple of fail",
typ: reflect.TypeOf(struct {
Value int `json:"value" multipleOf:"5"`
}{}),
input: map[string]any{"value": 2},
errs: []string{"expected number to be a multiple of 5"},
},
{
name: "string success",
typ: reflect.TypeOf(""),
input: "",
},
{
name: "expected string",
typ: reflect.TypeOf(""),
input: 1,
errs: []string{"expected string"},
},
{
name: "min length success",
typ: reflect.TypeOf(struct {
Value string `json:"value" minLength:"1"`
}{}),
input: map[string]any{"value": "a"},
},
{
name: "min length fail",
typ: reflect.TypeOf(struct {
Value string `json:"value" minLength:"1"`
}{}),
input: map[string]any{"value": ""},
errs: []string{"expected length >= 1"},
},
{
name: "max length success",
typ: reflect.TypeOf(struct {
Value string `json:"value" maxLength:"1"`
}{}),
input: map[string]any{"value": "a"},
},
{
name: "max length fail",
typ: reflect.TypeOf(struct {
Value string `json:"value" maxLength:"1"`
}{}),
input: map[string]any{"value": "ab"},
errs: []string{"expected length <= 1"},
},
{
name: "pattern success",
typ: reflect.TypeOf(struct {
Value string `json:"value" pattern:"^[a-z]+$"`
}{}),
input: map[string]any{"value": "a"},
},
{
name: "pattern fail",
typ: reflect.TypeOf(struct {
Value string `json:"value" pattern:"^[a-z]+$"`
}{}),
input: map[string]any{"value": "a1"},
errs: []string{"expected string to match pattern ^[a-z]+$"},
},
{
name: "pattern invalid",
typ: reflect.TypeOf(struct {
Value string `json:"value" pattern:"^[a-"`
}{}),
input: map[string]any{"value": "a1"},
panic: "error parsing regexp",
},
{
name: "datetime success",
typ: reflect.TypeOf(struct {
Value time.Time `json:"value"`
}{}),
input: map[string]any{"value": []byte("2020-03-07T22:22:06-08:00")},
},
{
name: "datetime string success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"date-time"`
}{}),
input: map[string]any{"value": []byte("2020-03-07T22:22:06-08:00")},
},
{
name: "expected datetime",
typ: reflect.TypeOf(struct {
Value time.Time `json:"value"`
}{}),
input: map[string]any{"value": "bad"},
errs: []string{"expected string to be RFC 3339 date-time"},
},
{
name: "date success",
typ: reflect.TypeOf(struct {
Value time.Time `json:"value" format:"date"`
}{}),
input: map[string]any{"value": "2020-03-07"},
},
{
name: "expected date",
typ: reflect.TypeOf(struct {
Value time.Time `json:"value" format:"date"`
}{}),
input: map[string]any{"value": "bad"},
errs: []string{"expected string to be RFC 3339 date"},
},
{
name: "time success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"time"`
}{}),
input: map[string]any{"value": "22:22:06-08:00"},
},
{
name: "expected time",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"time"`
}{}),
input: map[string]any{"value": "bad"},
errs: []string{"expected string to be RFC 3339 time"},
},
{
name: "email success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"email"`
}{}),
input: map[string]any{"value": "alice@example.com"},
},
{
name: "expected email",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"email"`
}{}),
input: map[string]any{"value": "alice"},
errs: []string{"expected string to be RFC 5322 email: mail: missing '@' or angle-addr"},
},
{
name: "hostname success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"hostname"`
}{}),
input: map[string]any{"value": "example.com"},
},
{
name: "expected hostname",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"hostname"`
}{}),
input: map[string]any{"value": "%$^"},
errs: []string{"expected string to be RFC 5890 hostname"},
},
{
name: "idn-hostname success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"idn-hostname"`
}{}),
input: map[string]any{"value": "ëxample.com"},
},
// {
// name: "expected idn-hostname",
// typ: reflect.TypeOf(struct {
// Value string `json:"value" format:"idn-hostname"`
// }{}),
// input: map[string]any{"value": "\\"},
// errs: []string{"expected string to be RFC 5890 hostname"},
// },
{
name: "ipv4 success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"ipv4"`
}{}),
input: map[string]any{"value": "127.0.0.1"},
},
{
name: "expected ipv4",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"ipv4"`
}{}),
input: map[string]any{"value": "1234"},
errs: []string{"expected string to be RFC 2673 ipv4"},
},
{
name: "ipv6 success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"ipv6"`
}{}),
input: map[string]any{"value": "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
},
{
name: "expected ipv6",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"ipv6"`
}{}),
input: map[string]any{"value": "1234"},
errs: []string{"expected string to be RFC 2373 ipv6"},
},
{
name: "uri success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"uri"`
}{}),
input: map[string]any{"value": "http://example.com"},
},
{
name: "expected uri",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"uri"`
}{}),
input: map[string]any{"value": ":"},
errs: []string{"expected string to be RFC 3986 uri: parse \":\": missing protocol scheme"},
},
{
name: "uuid success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"uuid"`
}{}),
input: map[string]any{"value": "123e4567-e89b-12d3-a456-426655440000"},
},
{
name: "expected uuid",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"uuid"`
}{}),
input: map[string]any{"value": "bad"},
errs: []string{"expected string to be RFC 4122 uuid: invalid UUID length: 3"},
},
{
name: "uritemplate success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"uri-template"`
}{}),
input: map[string]any{"value": "/items/{item-id}/history"},
},
{
name: "expected uritemplate bad url",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"uri-template"`
}{}),
input: map[string]any{"value": ":"},
errs: []string{"expected string to be RFC 3986 uri: parse \":\": missing protocol scheme"},
},
{
name: "expected uritemplate",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"uri-template"`
}{}),
input: map[string]any{"value": "missing{"},
errs: []string{"expected string to be RFC 6570 uri-template"},
},
{
name: "jsonpointer success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"json-pointer"`
}{}),
input: map[string]any{"value": "/foo/bar"},
},
{
name: "expected jsonpointer",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"json-pointer"`
}{}),
input: map[string]any{"value": "bad"},
errs: []string{"expected string to be RFC 6901 json-pointer"},
},
{
name: "rel jsonpointer success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"relative-json-pointer"`
}{}),
input: map[string]any{"value": "0"},
},
{
name: "expected rel jsonpointer",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"relative-json-pointer"`
}{}),
input: map[string]any{"value": "/bad"},
errs: []string{"expected string to be RFC 6901 relative-json-pointer"},
},
{
name: "regex success",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"regex"`
}{}),
input: map[string]any{"value": "^[0-9a-f]+$"},
},
{
name: "expected regex",
typ: reflect.TypeOf(struct {
Value string `json:"value" format:"regex"`
}{}),
input: map[string]any{"value": "^[bad"},
errs: []string{"expected string to be regex: error parsing regexp: missing closing ]: `[bad`"},
},
{
name: "base64 byte success",
typ: reflect.TypeOf(struct {
Value []byte `json:"value"`
}{}),
input: map[string]any{"value": []byte("ABCD")},
},
{
name: "base64 string success",
typ: reflect.TypeOf(struct {
Value string `json:"value" encoding:"base64"`
}{}),
input: map[string]any{"value": "ABCD"},
},
{
name: "base64 fail",
typ: reflect.TypeOf(struct {
Value []byte `json:"value"`
}{}),
input: map[string]any{"value": []byte("!")},
errs: []string{"expected string to be base64 encoded"},
},
{
name: "array success",
typ: reflect.TypeOf([]any{}),
input: []any{1, 2, 3},
},
{
name: "expected array",
typ: reflect.TypeOf([]any{}),
input: 1,
errs: []string{"expected array"},
},
{
name: "min items success",
typ: reflect.TypeOf(struct {
Value []any `json:"value" minItems:"1"`
}{}),
input: map[string]any{"value": []any{1}},
},
{
name: "expected min items",
typ: reflect.TypeOf(struct {
Value []any `json:"value" minItems:"1"`
}{}),
input: map[string]any{"value": []any{}},
errs: []string{"expected array length >= 1"},
},
{
name: "max items success",
typ: reflect.TypeOf(struct {
Value []any `json:"value" maxItems:"1"`
}{}),
input: map[string]any{"value": []any{1}},
},
{
name: "expected max items",
typ: reflect.TypeOf(struct {
Value []any `json:"value" maxItems:"1"`
}{}),
input: map[string]any{"value": []any{1, 2}},
errs: []string{"expected array length <= 1"},
},
{
name: "unique success",
typ: reflect.TypeOf(struct {
Value []any `json:"value" uniqueItems:"true"`
}{}),
input: map[string]any{"value": []any{1, 2, 3, 4, 5}},
},
{
name: "expected unique",
typ: reflect.TypeOf(struct {
Value []any `json:"value" uniqueItems:"true"`
}{}),
input: map[string]any{"value": []any{1, 2, 1, 3}},
errs: []string{"expected array items to be unique"},
},
{
name: "map success",
typ: reflect.TypeOf(map[string]int{}),
input: map[string]any{"one": 1, "two": 2},
},
{
name: "expected map item",
typ: reflect.TypeOf(map[string]int{}),
input: map[string]any{"one": 1, "two": true},
errs: []string{"expected number"},
},
{
name: "map minProps success",
typ: reflect.TypeOf(struct {
Value map[string]int `json:"value" minProperties:"1"`
}{}),
input: map[string]any{
"value": map[string]any{"one": 1},
},
},
{
name: "expected map minProps",
typ: reflect.TypeOf(struct {
Value map[string]int `json:"value" minProperties:"1"`
}{}),
input: map[string]any{
"value": map[string]any{},
},
errs: []string{"expected object with at least 1 properties"},
},
{
name: "map maxProps success",
typ: reflect.TypeOf(struct {
Value map[string]int `json:"value" maxProperties:"1"`
}{}),
input: map[string]any{
"value": map[string]any{"one": 1},
},
},
{
name: "expected map maxProps",
typ: reflect.TypeOf(struct {
Value map[string]int `json:"value" maxProperties:"1"`
}{}),
input: map[string]any{
"value": map[string]any{"one": 1, "two": 2},
},
errs: []string{"expected object with at most 1 properties"},
},
{
name: "object struct success",
typ: reflect.TypeOf(struct{}{}),
input: map[string]any{},
},
{
name: "expected object",
typ: reflect.TypeOf(struct{}{}),
input: true,
errs: []string{"expected object"},
},
{
name: "object optional success",
typ: reflect.TypeOf(struct {
Value string `json:"value,omitempty"`
}{}),
input: map[string]any{},
},
{
name: "readOnly set success",
typ: reflect.TypeOf(struct {
Value string `json:"value" readOnly:"true"`
}{}),
mode: ModeWriteToServer,
input: map[string]any{"value": "whoops"},
},
{
name: "readOnly missing success",
typ: reflect.TypeOf(struct {
Value string `json:"value" readOnly:"true"`
}{}),
mode: ModeWriteToServer,
input: map[string]any{},
},
{
name: "readOnly missing fail",
typ: reflect.TypeOf(struct {
Value string `json:"value" readOnly:"true"`
}{}),
mode: ModeReadFromServer,
input: map[string]any{},
errs: []string{"expected required property value to be present"},
},
{
name: "writeOnly missing fail",
typ: reflect.TypeOf(struct {
Value string `json:"value" writeOnly:"true"`
}{}),
mode: ModeReadFromServer,
input: map[string]any{"value": "should not be set"},
errs: []string{"write only property is non-zero"},
},
{
name: "unexpected property",
typ: reflect.TypeOf(struct {
Value string `json:"value,omitempty"`
}{}),
input: map[string]any{"value2": "whoops"},
errs: []string{"unexpected property"},
},
{
name: "nested success",
typ: reflect.TypeOf(struct {
Items []struct {
Value string `json:"value"`
} `json:"items"`
}{}),
input: map[string]any{"items": []any{map[string]any{"value": "hello"}}},
},
{
name: "expected nested",
typ: reflect.TypeOf(struct {
Items []struct {
Value string `json:"value"`
} `json:"items"`
}{}),
input: map[string]any{"items": []any{map[string]any{}}},
errs: []string{"expected required property value to be present"},
},
{
name: "enum success",
typ: reflect.TypeOf(struct {
Value string `json:"value" enum:"one,two"`
}{}),
input: map[string]any{"value": "one"},
},
{
name: "expected enum",
typ: reflect.TypeOf(struct {
Value string `json:"value" enum:"one,two"`
}{}),
input: map[string]any{"value": "three"},
errs: []string{"expected value to be one of \"one, two\""},
},
{
name: "optional success",
typ: reflect.TypeOf(struct {
Value string `json:"value,omitempty" minLength:"1"`
}{}),
input: map[string]any{},
},
{
name: "optional fail",
typ: reflect.TypeOf(struct {
Value string `json:"value,omitempty" minLength:"1"`
}{}),
input: map[string]any{"value": ""},
errs: []string{"expected length >= 1"},
},
}
func TestValidate(t *testing.T) {
pb := NewPathBuffer([]byte(""), 0)
res := &ValidateResult{}
for _, test := range validateTests {
t.Run(test.name, func(t *testing.T) {
registry := NewMapRegistry("#/components/schemas/", DefaultSchemaNamer)
var s *Schema
if test.panic != "" {
assert.Panics(t, func() {
registry.Schema(test.typ, false, "TestInput")
})
return
} else {
s = registry.Schema(test.typ, false, "TestInput")
}
pb.Reset()
res.Reset()
Validate(registry, s, pb, test.mode, test.input, res)
if len(test.errs) > 0 {
errs := mapTo(res.Errors, func(e error) string {
return e.(*ErrorDetail).Message
})
schemaJSON, _ := json.MarshalIndent(registry.Map(), "", " ")
for _, err := range test.errs {
assert.Contains(t, errs, err, string(schemaJSON))
}
} else {
assert.Empty(t, res.Errors)
}
})
}
}
var BenchValidatePB *PathBuffer
var BenchValidateRes *ValidateResult
func BenchmarkValidate(b *testing.B) {
pb := NewPathBuffer([]byte(""), 0)
res := &ValidateResult{}
BenchValidatePB = pb
BenchValidateRes = res
for _, test := range validateTests {
if test.panic != "" || len(test.errs) > 0 {
continue
}
b.Run(strings.TrimSuffix(test.name, " success"), func(b *testing.B) {
registry := NewMapRegistry("#/components/schemas/", DefaultSchemaNamer)
s := registry.Schema(test.typ, false, "TestInput")
input := test.input
if s.Type == TypeObject && s.Properties["value"] != nil {
s = s.Properties["value"]
input = input.(map[string]any)["value"]
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pb.Reset()
res.Reset()
Validate(registry, s, pb, test.mode, input, res)
}
})
}
}