-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidator_string_test.go
More file actions
1262 lines (1126 loc) · 33.4 KB
/
Copy pathvalidator_string_test.go
File metadata and controls
1262 lines (1126 loc) · 33.4 KB
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package validator
import "testing"
func TestValidateHexColor(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
// Valid formats
{"empty string", "", true},
{"3 char with hash", "#fff", true},
{"3 char uppercase with hash", "#FFF", true},
{"3 char mixed case with hash", "#fFf", true},
{"6 char with hash", "#ffffff", true},
{"6 char uppercase with hash", "#FFFFFF", true},
{"6 char mixed case with hash", "#ffFFff", true},
{"3 char without hash", "fff", true},
{"6 char without hash", "ffffff", true},
{"valid color #000", "#000", true},
{"valid color #abc123", "#abc123", true},
// Invalid formats
{"4 char", "#ffff", false},
{"5 char", "#fffff", false},
{"7 char", "#fffffff", false},
{"invalid char g", "#ggg", false},
{"invalid char z", "#zzz", false},
{"too short", "#ff", false},
{"too long", "#fffffffff", false},
{"double hash", "##fff", false},
{"spaces", "# fff", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateHexColor(tt.input); got != tt.want {
t.Errorf("ValidateHexColor(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidateTimezone(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
// Valid timezones
{"empty string", "", true},
{"UTC", "UTC", true},
{"Local", "Local", true},
{"America/New_York", "America/New_York", true},
{"America/Los_Angeles", "America/Los_Angeles", true},
{"Europe/London", "Europe/London", true},
{"Europe/Paris", "Europe/Paris", true},
{"Asia/Tokyo", "Asia/Tokyo", true},
{"Asia/Shanghai", "Asia/Shanghai", true},
{"Australia/Sydney", "Australia/Sydney", true},
{"Pacific/Auckland", "Pacific/Auckland", true},
// Invalid timezones
{"invalid", "Invalid/Timezone", false},
{"random string", "not_a_timezone", false},
{"partial", "America", false},
{"typo", "America/NewYork", false},
{"spaces", "America/New York", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateTimezone(tt.input); got != tt.want {
t.Errorf("ValidateTimezone(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidateStructHexColor(t *testing.T) {
type Theme struct {
PrimaryColor string `valid:"required,hexColor"`
SecondaryColor string `valid:"hexColor"`
}
tests := []struct {
name string
theme Theme
wantError bool
}{
{
name: "valid colors",
theme: Theme{PrimaryColor: "#ff5733", SecondaryColor: "#333"},
wantError: false,
},
{
name: "valid without secondary",
theme: Theme{PrimaryColor: "#ffffff"},
wantError: false,
},
{
name: "invalid primary color",
theme: Theme{PrimaryColor: "not-a-color"},
wantError: true,
},
{
name: "invalid secondary color",
theme: Theme{PrimaryColor: "#fff", SecondaryColor: "#gggggg"},
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.theme)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateStructTimezone(t *testing.T) {
type UserSettings struct {
Timezone string `valid:"required,timezone"`
}
tests := []struct {
name string
settings UserSettings
wantError bool
}{
{
name: "valid timezone UTC",
settings: UserSettings{Timezone: "UTC"},
wantError: false,
},
{
name: "valid timezone America/New_York",
settings: UserSettings{Timezone: "America/New_York"},
wantError: false,
},
{
name: "valid timezone Asia/Tokyo",
settings: UserSettings{Timezone: "Asia/Tokyo"},
wantError: false,
},
{
name: "invalid timezone",
settings: UserSettings{Timezone: "Invalid/Zone"},
wantError: true,
},
{
name: "empty timezone fails required",
settings: UserSettings{Timezone: ""},
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.settings)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateASCII(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty string", "", true},
{"ascii only", "hello world", true},
{"ascii with numbers", "hello123", true},
{"ascii with symbols", "hello@world.com", true},
{"unicode", "héllo", false},
{"chinese", "你好", false},
{"emoji", "hello 😀", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateASCII(tt.input); got != tt.want {
t.Errorf("ValidateASCII(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidateLowercase(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty string", "", true},
{"lowercase", "hello", true},
{"lowercase with numbers", "hello123", true},
{"uppercase", "HELLO", false},
{"mixed case", "Hello", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateLowercase(tt.input); got != tt.want {
t.Errorf("ValidateLowercase(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidateUppercase(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty string", "", true},
{"uppercase", "HELLO", true},
{"uppercase with numbers", "HELLO123", true},
{"lowercase", "hello", false},
{"mixed case", "Hello", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateUppercase(tt.input); got != tt.want {
t.Errorf("ValidateUppercase(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidateStartsWith(t *testing.T) {
tests := []struct {
name string
input string
params []string
want bool
}{
{"empty string", "", []string{"hello"}, true},
{"starts with", "hello world", []string{"hello"}, true},
{"starts with one of many", "hello world", []string{"hi", "hello"}, true},
{"does not start with", "hello world", []string{"world"}, false},
{"case sensitive", "Hello world", []string{"hello"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateStartsWith(tt.input, tt.params); got != tt.want {
t.Errorf("ValidateStartsWith(%q, %v) = %v, want %v", tt.input, tt.params, got, tt.want)
}
})
}
}
func TestValidateEndsWith(t *testing.T) {
tests := []struct {
name string
input string
params []string
want bool
}{
{"empty string", "", []string{"world"}, true},
{"ends with", "hello world", []string{"world"}, true},
{"ends with one of many", "hello world", []string{"hello", "world"}, true},
{"does not end with", "hello world", []string{"hello"}, false},
{"case sensitive", "hello World", []string{"world"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateEndsWith(tt.input, tt.params); got != tt.want {
t.Errorf("ValidateEndsWith(%q, %v) = %v, want %v", tt.input, tt.params, got, tt.want)
}
})
}
}
func TestValidateDoesntStartWith(t *testing.T) {
tests := []struct {
name string
input string
params []string
want bool
}{
{"empty string", "", []string{"hello"}, true},
{"does not start with", "hello world", []string{"world"}, true},
{"starts with", "hello world", []string{"hello"}, false},
{"starts with one of many", "hello world", []string{"hi", "hello"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateDoesntStartWith(tt.input, tt.params); got != tt.want {
t.Errorf("ValidateDoesntStartWith(%q, %v) = %v, want %v", tt.input, tt.params, got, tt.want)
}
})
}
}
func TestValidateDoesntEndWith(t *testing.T) {
tests := []struct {
name string
input string
params []string
want bool
}{
{"empty string", "", []string{"world"}, true},
{"does not end with", "hello world", []string{"hello"}, true},
{"ends with", "hello world", []string{"world"}, false},
{"ends with one of many", "hello world", []string{"hello", "world"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateDoesntEndWith(tt.input, tt.params); got != tt.want {
t.Errorf("ValidateDoesntEndWith(%q, %v) = %v, want %v", tt.input, tt.params, got, tt.want)
}
})
}
}
func TestValidateMACAddress(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty string", "", true},
{"colon format", "00:1A:2B:3C:4D:5E", true},
{"hyphen format", "00-1A-2B-3C-4D-5E", true},
{"cisco format", "001A.2B3C.4D5E", true},
{"lowercase", "00:1a:2b:3c:4d:5e", true},
{"invalid", "00:1A:2B:3C:4D", false},
{"invalid chars", "00:1G:2B:3C:4D:5E", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateMACAddress(tt.input); got != tt.want {
t.Errorf("ValidateMACAddress(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidateULID(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{"empty string", "", true},
{"valid ulid", "01ARZ3NDEKTSV4RRFFQ69G5FAV", true},
{"lowercase invalid", "01arz3ndektsv4rrffq69g5fav", false},
{"too short", "01ARZ3NDEKTSV4RRFFQ69G5FA", false},
{"too long", "01ARZ3NDEKTSV4RRFFQ69G5FAVX", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ValidateULID(tt.input); got != tt.want {
t.Errorf("ValidateULID(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
func TestValidateStructASCII(t *testing.T) {
type TestASCII struct {
Name string `valid:"ascii"`
}
tests := []struct {
name string
data TestASCII
wantError bool
}{
{"ascii valid", TestASCII{Name: "hello"}, false},
{"ascii invalid", TestASCII{Name: "héllo"}, true},
{"empty valid", TestASCII{Name: ""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateStructCase(t *testing.T) {
type TestCase struct {
Lower string `valid:"lowercase"`
Upper string `valid:"uppercase"`
}
tests := []struct {
name string
data TestCase
wantError bool
}{
{"both valid", TestCase{Lower: "hello", Upper: "WORLD"}, false},
{"lower invalid", TestCase{Lower: "Hello", Upper: "WORLD"}, true},
{"upper invalid", TestCase{Lower: "hello", Upper: "World"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateStructStartsEndsWith(t *testing.T) {
type TestPrefix struct {
URL string `valid:"startsWith=http|https"`
}
type TestSuffix struct {
Email string `valid:"endsWith=.com|.org"`
}
t.Run("startsWith valid", func(t *testing.T) {
err := ValidateStruct(TestPrefix{URL: "https://example.com"})
if err != nil {
t.Errorf("ValidateStruct() error = %v", err)
}
})
t.Run("startsWith invalid", func(t *testing.T) {
err := ValidateStruct(TestPrefix{URL: "ftp://example.com"})
if err == nil {
t.Errorf("ValidateStruct() expected error")
}
})
t.Run("endsWith valid", func(t *testing.T) {
err := ValidateStruct(TestSuffix{Email: "test@example.com"})
if err != nil {
t.Errorf("ValidateStruct() error = %v", err)
}
})
t.Run("endsWith invalid", func(t *testing.T) {
err := ValidateStruct(TestSuffix{Email: "test@example.net"})
if err == nil {
t.Errorf("ValidateStruct() expected error")
}
})
}
func TestValidateStructMAC(t *testing.T) {
type TestMAC struct {
Address string `valid:"macAddress"`
}
tests := []struct {
name string
data TestMAC
wantError bool
}{
{"valid mac", TestMAC{Address: "00:1A:2B:3C:4D:5E"}, false},
{"invalid mac", TestMAC{Address: "invalid"}, true},
{"empty valid", TestMAC{Address: ""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateAccepted(t *testing.T) {
type TestAccepted struct {
Terms string `valid:"accepted"`
}
tests := []struct {
name string
data TestAccepted
wantError bool
}{
{"accepted yes", TestAccepted{Terms: "yes"}, false},
{"accepted on", TestAccepted{Terms: "on"}, false},
{"accepted 1", TestAccepted{Terms: "1"}, false},
{"accepted true", TestAccepted{Terms: "true"}, false},
{"not accepted no", TestAccepted{Terms: "no"}, true},
{"not accepted empty", TestAccepted{Terms: ""}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateDeclined(t *testing.T) {
type TestDeclined struct {
OptOut string `valid:"declined"`
}
tests := []struct {
name string
data TestDeclined
wantError bool
}{
{"declined no", TestDeclined{OptOut: "no"}, false},
{"declined off", TestDeclined{OptOut: "off"}, false},
{"declined 0", TestDeclined{OptOut: "0"}, false},
{"declined false", TestDeclined{OptOut: "false"}, false},
{"not declined yes", TestDeclined{OptOut: "yes"}, true},
{"not declined empty", TestDeclined{OptOut: ""}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateProhibited(t *testing.T) {
type TestProhibited struct {
SecretField string `valid:"prohibited"`
}
tests := []struct {
name string
data TestProhibited
wantError bool
}{
{"empty allowed", TestProhibited{SecretField: ""}, false},
{"has value prohibited", TestProhibited{SecretField: "secret"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateMissing(t *testing.T) {
type TestMissing struct {
Field string `valid:"missing"`
}
tests := []struct {
name string
data TestMissing
wantError bool
}{
{"empty is missing", TestMissing{Field: ""}, false},
{"has value not missing", TestMissing{Field: "value"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidatePresent(t *testing.T) {
type TestPresent struct {
Field string `valid:"present"`
}
tests := []struct {
name string
data TestPresent
wantError bool
}{
{"empty is present", TestPresent{Field: ""}, false},
{"with value is present", TestPresent{Field: "value"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateMultipleOf(t *testing.T) {
type TestMultiple struct {
Number int `valid:"multipleOf=5"`
}
tests := []struct {
name string
data TestMultiple
wantError bool
}{
{"10 is multiple of 5", TestMultiple{Number: 10}, false},
{"15 is multiple of 5", TestMultiple{Number: 15}, false},
{"0 is multiple of 5", TestMultiple{Number: 0}, false},
{"7 is not multiple of 5", TestMultiple{Number: 7}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateMaxDigits(t *testing.T) {
type TestMaxDigits struct {
Code int `valid:"maxDigits=4"`
}
tests := []struct {
name string
data TestMaxDigits
wantError bool
}{
{"1234 has 4 digits", TestMaxDigits{Code: 1234}, false},
{"123 has 3 digits", TestMaxDigits{Code: 123}, false},
{"12345 has 5 digits", TestMaxDigits{Code: 12345}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateMinDigits(t *testing.T) {
type TestMinDigits struct {
Code int `valid:"minDigits=4"`
}
tests := []struct {
name string
data TestMinDigits
wantError bool
}{
{"1234 has 4 digits", TestMinDigits{Code: 1234}, false},
{"12345 has 5 digits", TestMinDigits{Code: 12345}, false},
{"123 has 3 digits", TestMinDigits{Code: 123}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateContains(t *testing.T) {
type TestContains struct {
Text string `valid:"contains=hello"`
}
tests := []struct {
name string
data TestContains
wantError bool
}{
{"contains hello", TestContains{Text: "say hello world"}, false},
{"does not contain", TestContains{Text: "say goodbye"}, true},
{"empty string", TestContains{Text: ""}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateDoesntContain(t *testing.T) {
type TestDoesntContain struct {
Text string `valid:"doesntContain=spam"`
}
tests := []struct {
name string
data TestDoesntContain
wantError bool
}{
{"doesnt contain spam", TestDoesntContain{Text: "hello world"}, false},
{"contains spam", TestDoesntContain{Text: "this is spam"}, true},
{"empty is ok", TestDoesntContain{Text: ""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateDecimalPrecision(t *testing.T) {
type TestDecimal struct {
Price float64 `valid:"decimal=2"`
}
tests := []struct {
name string
data TestDecimal
wantError bool
}{
{"10.99 has 2 decimals", TestDecimal{Price: 10.99}, false},
{"10.12 has 2 decimals", TestDecimal{Price: 10.12}, false},
{"10 has 0 decimals", TestDecimal{Price: 10}, true},
{"10.1 has 1 decimal", TestDecimal{Price: 10.1}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateList(t *testing.T) {
type TestList struct {
Items []string `valid:"list"`
}
tests := []struct {
name string
data TestList
wantError bool
}{
{"valid list", TestList{Items: []string{"a", "b", "c"}}, false},
{"empty list", TestList{Items: []string{}}, false},
{"nil list", TestList{Items: nil}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateRequiredArrayKeys(t *testing.T) {
type TestArrayKeys struct {
Data map[string]string `valid:"requiredArrayKeys=name|email"`
}
tests := []struct {
name string
data TestArrayKeys
wantError bool
}{
{"has all keys", TestArrayKeys{Data: map[string]string{"name": "John", "email": "john@example.com"}}, false},
{"missing email", TestArrayKeys{Data: map[string]string{"name": "John"}}, true},
{"missing name", TestArrayKeys{Data: map[string]string{"email": "john@example.com"}}, true},
{"empty map", TestArrayKeys{Data: map[string]string{}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateRequiredIfAccepted(t *testing.T) {
type TestRequiredIfAccepted struct {
Newsletter string `valid:""`
Email string `valid:"requiredIfAccepted=Newsletter"`
}
tests := []struct {
name string
data TestRequiredIfAccepted
wantError bool
}{
{"newsletter yes, email provided", TestRequiredIfAccepted{Newsletter: "yes", Email: "test@example.com"}, false},
{"newsletter yes, email missing", TestRequiredIfAccepted{Newsletter: "yes", Email: ""}, true},
{"newsletter no, email not required", TestRequiredIfAccepted{Newsletter: "no", Email: ""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateRequiredIfDeclined(t *testing.T) {
type TestRequiredIfDeclined struct {
AutoPay string `valid:""`
Reason string `valid:"requiredIfDeclined=AutoPay"`
}
tests := []struct {
name string
data TestRequiredIfDeclined
wantError bool
}{
{"autopay no, reason provided", TestRequiredIfDeclined{AutoPay: "no", Reason: "prefer manual"}, false},
{"autopay no, reason missing", TestRequiredIfDeclined{AutoPay: "no", Reason: ""}, true},
{"autopay yes, reason not required", TestRequiredIfDeclined{AutoPay: "yes", Reason: ""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateProhibitedIfAccepted(t *testing.T) {
type TestProhibitedIfAccepted struct {
AutoFill string `valid:""`
Manual string `valid:"prohibitedIfAccepted=AutoFill"`
}
tests := []struct {
name string
data TestProhibitedIfAccepted
wantError bool
}{
{"autofill yes, manual empty", TestProhibitedIfAccepted{AutoFill: "yes", Manual: ""}, false},
{"autofill yes, manual provided", TestProhibitedIfAccepted{AutoFill: "yes", Manual: "data"}, true},
{"autofill no, manual allowed", TestProhibitedIfAccepted{AutoFill: "no", Manual: "data"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateProhibitedIfDeclined(t *testing.T) {
type TestProhibitedIfDeclined struct {
Subscribe string `valid:""`
Preferences string `valid:"prohibitedIfDeclined=Subscribe"`
}
tests := []struct {
name string
data TestProhibitedIfDeclined
wantError bool
}{
{"subscribe no, prefs empty", TestProhibitedIfDeclined{Subscribe: "no", Preferences: ""}, false},
{"subscribe no, prefs provided", TestProhibitedIfDeclined{Subscribe: "no", Preferences: "daily"}, true},
{"subscribe yes, prefs allowed", TestProhibitedIfDeclined{Subscribe: "yes", Preferences: "daily"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateAcceptedIf(t *testing.T) {
type TestAcceptedIf struct {
Country string `valid:""`
Terms string `valid:"acceptedIf=Country|US"`
}
tests := []struct {
name string
data TestAcceptedIf
wantError bool
}{
{"US country, terms accepted", TestAcceptedIf{Country: "US", Terms: "yes"}, false},
{"US country, terms not accepted", TestAcceptedIf{Country: "US", Terms: "no"}, true},
{"other country, terms not required", TestAcceptedIf{Country: "UK", Terms: "no"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateDeclinedIf(t *testing.T) {
type TestDeclinedIf struct {
Type string `valid:""`
OptOut string `valid:"declinedIf=Type|premium"`
}
tests := []struct {
name string
data TestDeclinedIf
wantError bool
}{
{"premium type, optout declined", TestDeclinedIf{Type: "premium", OptOut: "no"}, false},
{"premium type, optout accepted", TestDeclinedIf{Type: "premium", OptOut: "yes"}, true},
{"basic type, no restriction", TestDeclinedIf{Type: "basic", OptOut: "yes"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestValidateMissingIf(t *testing.T) {
type TestMissingIf struct {
Type string `valid:""`
Secret string `valid:"missingIf=Type|public"`
}
tests := []struct {
name string
data TestMissingIf
wantError bool
}{
{"public type, secret missing", TestMissingIf{Type: "public", Secret: ""}, false},
{"public type, secret present", TestMissingIf{Type: "public", Secret: "value"}, true},
{"private type, secret allowed", TestMissingIf{Type: "private", Secret: "value"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStruct(tt.data)
if (err != nil) != tt.wantError {
t.Errorf("ValidateStruct() error = %v, wantError %v", err, tt.wantError)
}
})
}
}