-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidator_test.go
654 lines (610 loc) · 18.1 KB
/
validator_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
package validator
import (
"reflect"
"testing"
)
type FieldsRequired struct {
Name string ``
Email string `valid:"required"`
}
type MultipleFieldsRequired struct {
URL string `valid:"required"`
Email string `valid:"required"`
}
type FieldsEmail struct {
Email string `valid:"email"`
}
type FieldsURL struct {
URL string `valid:"url"`
}
func TestFieldsRequired(t *testing.T) {
var tests = []struct {
param FieldsRequired
expected bool
}{
{FieldsRequired{}, false},
{FieldsRequired{Name: "", Email: ""}, false},
{FieldsRequired{Name: "TEST"}, false},
{FieldsRequired{Name: "TEST", Email: ""}, false},
{FieldsRequired{Email: "[email protected]"}, true},
{FieldsRequired{Name: "", Email: "[email protected]"}, true},
{FieldsRequired{Name: "TEST", Email: "[email protected]"}, true},
}
for _, test := range tests {
err := ValidateStruct(test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%q) to be %v, got %v", test.param, test.expected, actual)
if err == nil {
t.Errorf("Got Error on validateateStruct(%q): %s", test.param, err)
}
}
}
}
func TestMultipleFieldsRequired(t *testing.T) {
var tests = []struct {
param MultipleFieldsRequired
expected bool
}{
{MultipleFieldsRequired{}, false},
{MultipleFieldsRequired{URL: "", Email: ""}, false},
{MultipleFieldsRequired{URL: "TEST"}, false},
{MultipleFieldsRequired{URL: "TEST", Email: ""}, false},
{MultipleFieldsRequired{Email: "[email protected]"}, false},
{MultipleFieldsRequired{URL: "", Email: "[email protected]"}, false},
{MultipleFieldsRequired{URL: "TEST", Email: "[email protected]"}, true},
}
for _, test := range tests {
err := ValidateStruct(&test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%q) to be %v, got %v", test.param, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%q): %s", test.param, err)
}
}
}
}
func TestRequiredIf(t *testing.T) {
type SubTest struct {
Test string
}
type RequiredIf struct {
String string `valid:"requiredIf=RequiredIfString|otwell"`
RequiredIfString string
Bool string `valid:"requiredIf=RequiredIfBool|true"`
RequiredIfBool bool
Number string `valid:"requiredIf=RequiredIfNumber|888"`
RequiredIfNumber int
Array string `valid:"requiredIf=RequiredIfArray|888"`
RequiredIfArray []string
SubTest string `valid:"requiredIf=RequiredIfSub.Test|otwell"`
RequiredIfSub *SubTest
}
var tests = []struct {
param RequiredIf
expected bool
}{
{RequiredIf{}, true},
{RequiredIf{String: "", RequiredIfString: ""}, true},
{RequiredIf{String: "String"}, true},
{RequiredIf{RequiredIfString: "otwell"}, false},
{RequiredIf{String: "", RequiredIfString: "otwell"}, false},
{RequiredIf{String: "String", RequiredIfString: "otwell"}, true},
{RequiredIf{Bool: "Bool"}, true},
{RequiredIf{Bool: "", RequiredIfBool: false}, true},
{RequiredIf{Bool: "", RequiredIfBool: true}, false},
{RequiredIf{Number: "Number"}, true},
{RequiredIf{Number: "", RequiredIfNumber: 555}, true},
{RequiredIf{Number: "", RequiredIfNumber: 888}, false},
{RequiredIf{SubTest: "SubTest"}, true},
{RequiredIf{SubTest: "", RequiredIfSub: &SubTest{Test: "aaa"}}, true},
{RequiredIf{SubTest: "", RequiredIfSub: &SubTest{Test: "otwell"}}, false},
}
for i, test := range tests {
err := ValidateStruct(&test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%T) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%T): %s", test.param, err)
}
}
}
}
func TestMultipleRequiredIf(t *testing.T) {
type RequiredIf struct {
First string
Last string `valid:"requiredIf=First|taylor|dayle"`
}
var tests = []struct {
param RequiredIf
expected bool
}{
{RequiredIf{}, true},
{RequiredIf{First: "", Last: ""}, true},
{RequiredIf{First: "TEST"}, true},
{RequiredIf{First: "taylor", Last: ""}, false},
{RequiredIf{First: "dayle", Last: ""}, false},
{RequiredIf{First: "taylor", Last: "otwell"}, true},
{RequiredIf{First: "dayle", Last: "otwell"}, true},
}
for _, test := range tests {
err := ValidateStruct(&test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%q) to be %v, got %v", test.param, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%q): %s", test.param, err)
}
}
}
}
func TestRequiredUnless(t *testing.T) {
type SubTest struct {
Test string
}
type RequiredUnlessString struct {
First string `valid:"requiredUnless=Last|otwell"`
Last string
}
type RequiredUnlessBool struct {
First string `valid:"requiredUnless=Last|true"`
Last bool
}
type RequiredUnlessNumber struct {
First string `valid:"requiredUnless=Last|888"`
Last int
}
type RequiredUnlessSub struct {
First string `valid:"requiredUnless=Last.Test|otwell"`
Last *SubTest
}
var tests = []struct {
param interface{}
expected bool
}{
{RequiredUnlessString{}, false},
{RequiredUnlessString{Last: "aa"}, false},
{RequiredUnlessString{Last: "otwell"}, true},
{RequiredUnlessBool{Last: false}, false},
{RequiredUnlessBool{Last: true}, true},
{RequiredUnlessNumber{Last: 555}, false},
{RequiredUnlessNumber{Last: 888}, true},
{RequiredUnlessSub{Last: &SubTest{Test: "test"}}, false},
{RequiredUnlessSub{Last: &SubTest{Test: "otwell"}}, true},
}
for i, test := range tests {
var err error
switch test.param.(type) {
case RequiredUnlessString:
err = ValidateStruct(test.param.(RequiredUnlessString))
case RequiredUnlessBool:
err = ValidateStruct(test.param.(RequiredUnlessBool))
case RequiredUnlessNumber:
err = ValidateStruct(test.param.(RequiredUnlessNumber))
case RequiredUnlessSub:
err = ValidateStruct(test.param.(RequiredUnlessSub))
}
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%T) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%T): %s", test.param, err)
}
}
}
}
func TestMax(t *testing.T) {
type TestMaxStruct struct {
String string `valid:"max=5"`
Int int `valid:"max=5"`
Unit uint `valid:"max=5"`
Float float64 `valid:"max=5.3"`
Array []string `valid:"max=5"`
Map map[string]string `valid:"max=5"`
}
var tests = []struct {
param TestMaxStruct
expected bool
}{
{TestMaxStruct{}, true},
{TestMaxStruct{String: "Hell"}, true},
{TestMaxStruct{String: "Hello World"}, false},
{TestMaxStruct{Int: 4}, true},
{TestMaxStruct{Int: 6}, false},
{TestMaxStruct{Unit: 4}, true},
{TestMaxStruct{Unit: 6}, false},
{TestMaxStruct{Float: 5.2}, true},
{TestMaxStruct{Float: 5.9}, false},
{TestMaxStruct{Array: []string{"1", "2", "3", "4"}}, true},
{TestMaxStruct{Array: []string{"1", "2", "3", "4", "5", "6"}}, false},
{TestMaxStruct{Array: []string{"1", "2", "3", "4", "5"}}, true},
{TestMaxStruct{Map: map[string]string{
"rsc": "string",
"r": "string",
"gri": "string",
"adg": "string",
"ab": "string",
"cd": "string",
}}, false},
{TestMaxStruct{Map: map[string]string{
"rsc": "string",
"r": "string",
"gri": "string",
"adg": "string",
"tt": "string",
}}, true},
}
for i, test := range tests {
err := ValidateStruct(test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%T) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%T): %s", test.param, err)
}
}
}
}
func TestMin(t *testing.T) {
type TestMinStruct struct {
String string `valid:"omitempty,min=5"`
Int int `valid:"omitempty,min=5"`
Unit uint `valid:"omitempty,min=5"`
Float float64 `valid:"omitempty,min=5.3"`
Array []string `valid:"omitempty,min=5"`
Map map[string]string `valid:"omitempty,min=5"`
}
var tests = []struct {
param TestMinStruct
expected bool
}{
{TestMinStruct{}, true},
{TestMinStruct{String: "Hell"}, false},
{TestMinStruct{String: "Hello"}, true},
{TestMinStruct{Int: 4}, false},
{TestMinStruct{Int: 5}, true},
{TestMinStruct{Unit: 4}, false},
{TestMinStruct{Unit: 5}, true},
{TestMinStruct{Float: 5.2}, false},
{TestMinStruct{Float: 5.9}, true},
{TestMinStruct{Array: []string{"1", "2", "3", "4"}}, false},
{TestMinStruct{Array: []string{"1", "2", "3", "4", "5"}}, true},
{TestMinStruct{Map: map[string]string{
"rsc": "string",
"r": "string",
"gri": "string",
"adg": "string",
}}, false},
{TestMinStruct{Map: map[string]string{
"rsc": "string",
"r": "string",
"gri": "string",
"adg": "string",
"tt": "string",
}}, true},
}
for i, test := range tests {
err := ValidateStruct(test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%T) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%T): %s", test.param, err)
}
}
}
}
func TestSize(t *testing.T) {
type TestSizeStruct struct {
String string `valid:"omitempty,size=5"`
Int int `valid:"omitempty,size=5"`
Unit uint `valid:"omitempty,size=5"`
Float float64 `valid:"omitempty,size=5.3"`
Array []string `valid:"omitempty,size=5"`
Map map[string]string `valid:"omitempty,size=5"`
}
var tests = []struct {
param TestSizeStruct
expected bool
}{
{TestSizeStruct{}, true},
{TestSizeStruct{String: "Hell"}, false},
{TestSizeStruct{String: "Hello"}, true},
{TestSizeStruct{Int: 4}, false},
{TestSizeStruct{Int: 5}, true},
{TestSizeStruct{Unit: 4}, false},
{TestSizeStruct{Unit: 5}, true},
{TestSizeStruct{Float: 5.2}, false},
{TestSizeStruct{Float: 5.3}, true},
{TestSizeStruct{Array: []string{"1", "2", "3", "4"}}, false},
{TestSizeStruct{Array: []string{"1", "2", "3", "4", "5"}}, true},
{TestSizeStruct{Map: map[string]string{
"rsc": "string",
"r": "string",
"gri": "string",
"adg": "string",
}}, false},
{TestSizeStruct{Map: map[string]string{
"rsc": "string",
"r": "string",
"gri": "string",
"adg": "string",
"tt": "string",
}}, true},
}
for i, test := range tests {
err := ValidateStruct(test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%T) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%T): %s", test.param, err)
}
}
}
}
func TestGt(t *testing.T) {
type GtStruct struct {
String string `valid:"omitempty,gt=GtStructString"`
GtStructString string
Number int `valid:"omitempty,gt=GtStructNumber"`
GtStructNumber int
Array []string `valid:"omitempty,gt=GtStructArray"`
GtStructArray []string
}
var tests = []struct {
param GtStruct
expected bool
}{
{GtStruct{}, true},
{GtStruct{String: "Hell", GtStructString: "Hello"}, false},
{GtStruct{String: "Hello World", GtStructString: "Hello"}, true},
{GtStruct{Number: 4, GtStructNumber: 5}, false},
{GtStruct{Number: 10, GtStructNumber: 5}, true},
{GtStruct{Array: []string{"1", "2", "3", "4"}, GtStructArray: []string{"1", "2", "3", "4", "5"}}, false},
{GtStruct{Array: []string{"1", "2", "3", "4", "5"}, GtStructArray: []string{"1", "2", "3", "4"}}, true},
}
for i, test := range tests {
err := ValidateStruct(&test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%T) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%T): %s", test.param, err)
}
}
}
}
func TestGte(t *testing.T) {
type GteStruct struct {
String string `valid:"omitempty,gte=GteStructString"`
GteStructString string
Number int `valid:"omitempty,gte=GteStructNumber"`
GteStructNumber int
Array []string `valid:"omitempty,gte=GteStructArray"`
GteStructArray []string
}
var tests = []struct {
param GteStruct
expected bool
}{
{GteStruct{}, true},
{GteStruct{String: "Hell", GteStructString: "Hello"}, false},
{GteStruct{String: "Hello World", GteStructString: "Hello"}, true},
{GteStruct{Number: 4, GteStructNumber: 5}, false},
{GteStruct{Number: 10, GteStructNumber: 5}, true},
{GteStruct{Array: []string{"1", "2", "3", "4"}, GteStructArray: []string{"1", "2", "3", "4", "5"}}, false},
{GteStruct{Array: []string{"1", "2", "3", "4", "5"}, GteStructArray: []string{"1", "2", "3", "4"}}, true},
}
for i, test := range tests {
err := ValidateStruct(&test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%T) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%T): %s", test.param, err)
}
}
}
}
func TestFieldsEmail(t *testing.T) {
var tests = []struct {
param FieldsEmail
expected bool
}{
{FieldsEmail{}, false},
{FieldsEmail{Email: ""}, false},
{FieldsEmail{Email: "aaa"}, false},
{FieldsEmail{Email: "[email protected]"}, true},
}
for i, test := range tests {
err := ValidateStruct(test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%+v) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%+v): %s", test.param, err)
}
}
}
}
func TestDistinct(t *testing.T) {
var result bool
result = ValidateDistinct([]string{"zh-HK", "zh-CN"})
if result != true {
t.Errorf("Got Error on validateateStruct case 1 %v, got %v", true, result)
}
result = ValidateDistinct([]string{"zh-HK", "zh-CN", "zh-CN"})
if result != false {
t.Errorf("Got Error on validateateStruct case 2 %v, got %v", false, result)
}
}
func TestInnerStruct(t *testing.T) {
CustomTypeRuleMap.Set("languageCode", func(v reflect.Value, o reflect.Value, validTag *ValidTag) bool {
if v.Kind() != reflect.String {
return true
}
return false
})
MessageMap["languageCode"] = "Language Code is not valid."
type CreditCard struct {
Number string
UserMemberNumber string `valid:"languageCode"`
}
type User struct {
MemberNumber string
CreditCards []CreditCard `json:"CreditCards" valid:"languageCode"`
}
c := []CreditCard{
{
Number: "1",
UserMemberNumber: "1",
},
{
Number: "2",
UserMemberNumber: "2",
},
}
u := User{
MemberNumber: "MemberNumber",
CreditCards: c,
}
ValidateStruct(u)
}
func TestInnerStruct2(t *testing.T) {
type CreditCard struct {
Number string
UserMemberNumber string `valid:"required,max=64"`
}
type User struct {
MemberNumber string
CreditCards []CreditCard `json:"CreditCards"`
}
c := []CreditCard{
{
Number: "1",
UserMemberNumber: "",
},
{
Number: "2",
UserMemberNumber: "2",
},
}
u := User{
MemberNumber: "MemberNumber",
CreditCards: c,
}
err := ValidateStruct(u)
if err == nil {
t.Errorf("Got Error on validateateStruct: %s", err)
}
}
func TestNameSpace(t *testing.T) {
type CreditCard struct {
Number string
UserMemberNumber string `valid:"languageCode"`
}
type User struct {
MemberNumber string
CreditCards []CreditCard `json:"CreditCards" valid:"languageCode"`
}
c := []CreditCard{
{
Number: "1",
UserMemberNumber: "1",
},
{
Number: "2",
UserMemberNumber: "2",
},
}
u := User{
MemberNumber: "MemberNumber",
CreditCards: c,
}
ValidateStruct(u)
}
func TestIsURL(t *testing.T) {
var tests = []struct {
param string
expected bool
}{
{"http://foo.bar#com", true},
{"http://foobar.com", true},
{"https://foobar.com", true},
{"foobar.com", false},
{"http://foobar.coffee/", true},
{"http://foobar.中文网/", true},
{"http://foobar.org/", true},
{"http://foobar.org:8080/", true},
{"ftp://foobar.ru/", true},
{"http://user:[email protected]/", true},
{"http://127.0.0.1/", true},
{"http://duckduckgo.com/?q=%2F", true},
{"http://localhost:3000/", true},
{"http://foobar.com/?foo=bar#baz=qux", true},
{"http://foobar.com?foo=bar", true},
{"http://www.xn--froschgrn-x9a.net/", true},
{"", true},
{"xyz://foobar.com", true},
{"invalid.", false},
{".com", false},
{"rtmp://foobar.com", true},
{"http://www.foo_bar.com/", true},
{"http://localhost:3000/", true},
{"http://foobar.com/#baz", true},
{"http://foobar.com#baz=qux", true},
{"http://foobar.com/t$-_.+!*\\'(),", true},
{"http://www.foobar.com/~foobar", true},
{"http://www.-foobar.com/", true},
{"http://www.foo---bar.com/", true},
{"mailto:[email protected]", true},
{"irc://irc.server.org/channel", true},
{"irc://#channel@network", true},
{"/abs/test/dir", false},
{"./rel/test/dir", false},
}
for i, test := range tests {
actual := ValidateURL(test.param)
if actual != test.expected {
t.Errorf("Expected IsURL(%+v) Case %d to be %v, got %v", test.param, i, test.expected, actual)
}
}
}
func TestPointer(t *testing.T) {
type FieldsPointer struct {
Name *string ``
UserName *string `valid:"max=5"`
Email *string `valid:"required,email"`
}
e1 := ""
u1 := ""
e2 := "test"
u2 := "test"
e3 := "[email protected]"
u3 := "test123456"
e4 := "[email protected]"
u4 := "test"
var tests = []struct {
param FieldsPointer
expected bool
}{
{FieldsPointer{}, false},
{FieldsPointer{Email: &e1, UserName: &u1}, false},
{FieldsPointer{Email: &e2, UserName: &u2}, false},
{FieldsPointer{Email: &e3, UserName: &u3}, false},
{FieldsPointer{Email: &e4, UserName: &u4}, true},
}
for i, test := range tests {
err := ValidateStruct(test.param)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected validateateStruct(%+v) Case %d to be %v, got %v", test.param, i, test.expected, actual)
if err != nil {
t.Errorf("Got Error on validateateStruct(%+v): %s", test.param, err)
}
}
}
}