-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidate_test.go
593 lines (555 loc) · 14.2 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
package validator_test
import (
"testing"
validator "github.com/neo7337/go-struct-validator"
)
var sv = validator.NewStructValidator()
func TestSkipValidation(t *testing.T) {
tests := []struct {
name string
input interface{}
}{
{
name: "Test-pass-1",
input: struct {
Name string `json:"name" constraints:"min-length=5"`
Age int `json:"age" constraints:"min=10"`
Mobile int `json:"mobile" constraints:""`
}{Name: "Testings", Age: 20, Mobile: 123456789},
},
{
name: "Test-pass-2",
input: struct {
Name string `json:"name" constraints:"min-length=5"`
Age int `json:"age" constraints:""`
Mobile int `json:"mobile"`
}{Name: "Testings", Age: 20, Mobile: 123456789},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := sv.Validate(tt.input)
if err != nil {
t.Errorf("Error in validation: %s", err)
}
})
}
}
func TestSuccessValidation(t *testing.T) {
type ReqMsg struct {
Name string `json:"name" constraints:"notnull=true"`
Age int `json:"age" constraints:"min=0"`
}
msg := ReqMsg{
Name: "Test",
Age: 11,
}
if err := sv.Validate(msg); err != nil {
t.Errorf("Error in validation: %s", err)
}
}
/**
min, max validations test
*/
func TestNumericValidations(t *testing.T) {
testsPass := []struct {
Name string
input interface{}
}{
{
Name: "Test-pass-1",
input: struct {
MinC1 int `json:"minC1" constraints:"min=10"`
MaxC1 int `json:"maxC1" constraints:"max=49"`
}{MinC1: 12, MaxC1: 45},
},
/**
exclusive min/max validation test
*/
{
Name: "Test-pass-2",
input: struct {
MinC4 int `json:"minC4" constraints:"exclusiveMin=10"`
MaxC4 int `json:"maxC4" constraints:"exclusiveMax=50"`
}{MinC4: 10, MaxC4: 50},
},
{
Name: "Test-pass-3",
input: struct {
Num7 int `json:"num7" constraints:"multipleOf=5"`
}{Num7: 10},
},
}
for _, tt := range testsPass {
t.Run(tt.Name, func(t *testing.T) {
err := sv.Validate(tt.input)
if err != nil {
t.Errorf("Error in validation: %s", err)
}
})
}
testsError := []struct {
Name string
input interface{}
want string
}{
{
Name: "Test-fail-1",
input: struct {
MinC2 int `json:"minC2" constraints:"min=10"`
MaxC2 int `json:"maxC2" constraints:"max=49"`
}{MinC2: 7, MaxC2: 45},
want: "min value validation failed for field MinC2",
},
{
Name: "Test-fail-2",
input: struct {
MinC3 int `json:"minC3" constraints:"min=10"`
MaxC3 int `json:"maxC3" constraints:"max=49"`
}{MinC3: 12, MaxC3: 55},
want: "max value validation failed for field MaxC3",
},
/**
exclusive min/max validation test
*/
{
Name: "Test-fail-3",
input: struct {
MinC5 int `json:"minC5" constraints:"exclusiveMin=10"`
MaxC5 int `json:"maxC5" constraints:"exclusiveMax=50"`
}{MinC5: 9, MaxC5: 50},
want: "exclusive min validation failed for field MinC5",
},
{
Name: "Test-fail-4",
input: struct {
MinC6 int `json:"minC6" constraints:"exclusiveMin=10"`
MaxC6 int `json:"maxC6" constraints:"exclusiveMax=50"`
}{MinC6: 10, MaxC6: 51},
want: "exclusive max validation failed for field MaxC6",
},
{
Name: "Test-fail-5",
input: struct {
Num8 int `json:"num8" constraints:"multipleOf=5"`
}{Num8: 11},
want: "multipleOf validation failed for field Num8",
},
{
Name: "Test-fail-6",
input: struct {
Name string `json:"name" constraints:"min=5"`
}{Name: "hello_world"},
want: "invalid validation applied to the field Name",
},
{
Name: "Test-fail-7",
input: struct {
Name string `json:"name" constraints:"max=5"`
}{Name: "hello_world"},
want: "invalid validation applied to the field Name",
},
{
Name: "Test-fail-8",
input: struct {
Name string `json:"name" constraints:"exclusiveMin=5"`
}{Name: "hello_world"},
want: "invalid validation applied to the field Name",
},
{
Name: "Test-fail-9",
input: struct {
Name string `json:"name" constraints:"exclusiveMax=5"`
}{Name: "hello_world"},
want: "invalid validation applied to the field Name",
},
{
Name: "Test-fail-10",
input: struct {
Name string `json:"name" constraints:"multipleOf=5"`
}{Name: "hello_world"},
want: "invalid validation applied to the field Name",
},
}
for _, tt := range testsError {
t.Run(tt.Name, func(t *testing.T) {
err := sv.Validate(tt.input)
if tt.want != err.Error() {
t.Errorf("Got: %s, want: %s", err, tt.want)
}
})
}
}
func TestStringValidation(t *testing.T) {
testsPass := []struct {
Name string
input interface{}
}{
{
Name: "Test-pass-1",
input: struct {
Name string `json:"name" constraints:"notnull=true"`
}{Name: "testing"},
},
{
Name: "Test-pass-2",
input: struct {
Str1T1 string `json:"str1T1" constraints:"min-length=10"`
Str2T1 string `json:"str2T1" constraints:"max-length=15"`
}{Str1T1: "hello_world", Str2T1: "hello_world_go"},
},
/**
pattern validations
*/
{
Name: "Test-pass-3",
input: struct {
Str4 string `json:"str4" constraints:"pattern=^[tes]{4}.*"`
}{Str4: "test1234"},
},
{
Name: "Test-pass-4",
input: struct {
Str4 string `json:"str4" constraints:"pattern=gray|grey"`
}{Str4: "grey"},
},
}
for _, tt := range testsPass {
t.Run(tt.Name, func(t *testing.T) {
err := sv.Validate(tt.input)
if err != nil {
t.Errorf("Error in validation: %s", err)
}
})
}
testsFail := []struct {
Name string
input interface{}
want string
}{
{
Name: "Test-fail-1",
input: struct {
Str1T2 string `json:"str1T2" constraints:"min-length=10"`
Str2T2 string `json:"str2T2" constraints:"max-length=15"`
}{Str1T2: "hell_worl", Str2T2: "hello_world_go"},
want: "min-length validation failed for field Str1T2",
},
{
Name: "Test-fail-2",
input: struct {
Str1T3 string `json:"str1T3" constraints:"min-length=10"`
Str2T3 string `json:"str2T3" constraints:"max-length=15"`
}{Str1T3: "hello_world", Str2T3: "hello_world_from_go"},
want: "max-length validation failed for field Str2T3",
},
/**
pattern validations
*/
{
Name: "Test-fail-3",
input: struct {
Str5 string `json:"str5" constraints:"pattern=^[tes]{4}.*"`
}{Str5: "abcd1234"},
want: "pattern validation failed for field Str5",
},
{
Name: "Test-fail-4",
input: struct {
Str6 string `json:"str6" constraints:"pattern=["`
}{Str6: "tsst1234"},
want: "invalid constraint pattern with value '[' for field Str6",
},
{
Name: "Test-fail-5",
input: struct {
Str string `json:"str" constraints:"pattern=gray|grey"`
}{Str: "gry"},
want: "pattern validation failed for field Str",
},
{
Name: "Test-fail-6",
input: struct {
Name string `json:"name" constraints:"notnull=true"`
}{Name: ""},
want: "notnull validation failed for field Name",
},
{
Name: "Test-fail-7",
input: struct {
Name string `json:"name" constraints:"notnull=dummy"`
}{Name: ""},
want: "invalid constraint notnull with value 'dummy' for field Name",
},
{
Name: "Test-fail-8",
input: struct {
Age int `json:"name" constraints:"notnull=dummy"`
}{Age: 22},
want: "invalid validation applied to the field Age",
},
{
Name: "Test-fail-9",
input: struct {
Age int `json:"name" constraints:"min-length=5"`
}{Age: 22},
want: "invalid validation applied to the field Age",
},
{
Name: "Test-fail-10",
input: struct {
Age int `json:"name" constraints:"max-length=14"`
}{Age: 22},
want: "invalid validation applied to the field Age",
},
{
Name: "Test-fail-11",
input: struct {
Age int `json:"name" constraints:"pattern=["`
}{Age: 22},
want: "invalid validation applied to the field Age",
},
}
for _, tt := range testsFail {
t.Run(tt.Name, func(t *testing.T) {
err := sv.Validate(tt.input)
if tt.want != err.Error() {
t.Errorf("Got: %s, want: %s", err, tt.want)
}
})
}
}
/**
Nested Structure Testing
**Not working as per latest algo**
*/
type Example struct {
Reference
Summary string `json:"summary,omitempty" constraints:""`
Description string `json:"description,omitempty" constraints:""`
Value interface{} `json:"example,omitempty" constraints:""`
}
type Reference struct {
Ref string `json:"ref" constraints:""`
RefDescription string `json:"ref-description" constraints:""`
RefSummary string `json:"ref-summary" constraints:""`
}
func TestNested(t *testing.T) {
msg := Example{
Reference: Reference{
Ref: "reference",
RefSummary: "ref summary",
RefDescription: "ref description",
},
Summary: "summary",
Description: "description",
Value: nil,
}
if err := sv.Validate(msg); err != nil {
t.Errorf("Error in validation: %s", err)
}
}
type ExampleFail struct {
ReferenceFail
Summary string `json:"summary,omitempty" constraints:""`
Description string `json:"description,omitempty" constraints:""`
Value interface{} `json:"example,omitempty" constraints:""`
}
type ReferenceFail struct {
Ref string `json:"ref" constraints:"min-length=10"`
RefDescription string `json:"ref-description" constraints:""`
RefSummary string `json:"ref-summary" constraints:""`
}
func TestNestedFail(t *testing.T) {
msg := ExampleFail{
ReferenceFail: ReferenceFail{
Ref: "reference",
RefSummary: "ref summary",
RefDescription: "ref description",
},
Summary: "summary",
Description: "description",
Value: nil,
}
err := sv.Validate(msg)
got := err.Error()
want := "min-length validation failed for field Ref"
if got != want {
t.Errorf("Expected: %s, got: %s", got, want)
}
}
/*
*
Empty Struct Validation
*/
func TestEmptyStruct(t *testing.T) {
type EmptyExample struct {
Field string `json:"field" constraints:""`
}
msg := EmptyExample{}
if err := sv.Validate(msg); err != nil {
t.Errorf("Error in validation: %s", err)
}
}
func TestConstStruct(t *testing.T) {
type ConstExample struct {
Summary string `json:"summary" constraints:"-"`
Description string `json:"description" constraints:""`
}
msg := ConstExample{
Summary: "testing",
Description: "this is testing",
}
if err := sv.Validate(msg); err != nil {
t.Errorf("Error in validation: %s", err)
}
}
func TestEnumValidation(t *testing.T) {
testsPass := []struct {
Name string
input interface{}
}{
{
Name: "Test-pass-1",
input: struct {
Status string `json:"status" constraints:"enum=Success,Error,Not Reachable"`
StatusCode int `json:"statusCode" constraints:"enum=200,404,500"`
}{Status: "Success", StatusCode: 200},
},
}
for _, tt := range testsPass {
t.Run(tt.Name, func(t *testing.T) {
err := sv.Validate(tt.input)
if err != nil {
t.Errorf("Error in validation: %s", err)
}
})
}
testsError := []struct {
Name string
input interface{}
want string
}{
{
Name: "Test-fail-2",
input: struct {
Status2 string `json:"status2" constraints:"enum=Success,Error,Not Reachable"`
StatusCode2 int `json:"statusCode2" constraints:"enum=200,404,500"`
}{Status2: "Success", StatusCode2: 503},
want: "enum validation failed for field StatusCode2",
},
}
for _, tt := range testsError {
t.Run(tt.Name, func(t *testing.T) {
err := sv.Validate(tt.input)
if tt.want != err.Error() {
t.Errorf("Got: %s, want: %s", err, tt.want)
}
})
}
}
func TestCacheSuccess(t *testing.T) {
vd := validator.NewStructValidator()
vd.SetCache(true)
// same structs with different fields will give the cached results on cached enabled
testsWithCache := []struct {
Name string
input interface{}
}{
{
Name: "Test-pass-1",
input: struct {
Name string `json:"name" constraints:"min-length=5"`
Age int `json:"age" constraints:"min=10"`
Mobile int `json:"mobile" constraints:""`
}{Name: "Testings", Age: 20, Mobile: 123456789},
},
{
Name: "Test-pass-2",
input: struct {
Name string `json:"name" constraints:"min-length=5"`
Age int `json:"age" constraints:"min=10"`
Mobile int `json:"mobile" constraints:""`
}{Name: "Testings", Age: 5, Mobile: 123456789},
},
}
for _, tt := range testsWithCache {
t.Run(tt.Name, func(t *testing.T) {
err := vd.Validate(tt.input)
if err != nil {
t.Errorf("Error in validation: %s", err)
}
})
}
}
func TestCacheErrs(t *testing.T) {
withoutCache := validator.NewStructValidator()
// same structs with different values and caching disabled will parse field with each request
testsWithoutCache := []struct {
Name string
input interface{}
want interface{}
}{
{
Name: "Test-fail-1",
input: struct {
Name string `json:"name" constraints:"min-length=5"`
Age int `json:"age" constraints:"min=10"`
Mobile int `json:"mobile" constraints:""`
}{Name: "Testings", Age: 20, Mobile: 123456789},
want: nil,
},
{
Name: "Test-fail-2",
input: struct {
Name string `json:"name" constraints:"min-length=5"`
Age int `json:"age" constraints:"min=10"`
Mobile int `json:"mobile" constraints:""`
}{Name: "Testings", Age: 5, Mobile: 123456789},
want: "min value validation failed for field Age",
},
}
for _, tt := range testsWithoutCache {
t.Run(tt.Name, func(t *testing.T) {
err := withoutCache.Validate(tt.input)
if err != nil {
if tt.want != err.Error() {
t.Errorf("Got: %s, want: %s", err, tt.want)
}
}
})
}
}
func TestStructValidator_SetTagName(t *testing.T) {
vd := validator.NewStructValidator()
vd.SetTagName("customTag")
testsWithCache := []struct {
Name string
input interface{}
}{
{
Name: "Test-pass-1",
input: struct {
Name string `json:"name" customTag:"min-length=5"`
Age int `json:"age" customTag:"min=10"`
Mobile int `json:"mobile" customTag:""`
}{Name: "Testings", Age: 20, Mobile: 123456789},
},
{
Name: "Test-pass-2",
input: struct {
Name string `json:"name" customTag:"min-length=5"`
Age int `json:"age" customTag:"min=10"`
Mobile int `json:"mobile" customTag:""`
}{Name: "Testings", Age: 21, Mobile: 123456789},
},
}
for _, tt := range testsWithCache {
t.Run(tt.Name, func(t *testing.T) {
err := vd.Validate(tt.input)
if err != nil {
t.Errorf("Error in validation: %s", err)
}
})
}
}