@@ -29,6 +29,7 @@ type TestInterface struct {
29
29
}
30
30
31
31
type TestString struct {
32
+ BlankTag string `validate:""`
32
33
Required string `validate:"required"`
33
34
Len string `validate:"len=10"`
34
35
Min string `validate:"min=1"`
@@ -70,12 +71,13 @@ type TestUint64 struct {
70
71
}
71
72
72
73
type TestFloat64 struct {
73
- Required int64 `validate:"required"`
74
- Len int64 `validate:"len=10"`
75
- Min int64 `validate:"min=1"`
76
- Max int64 `validate:"max=10"`
77
- MinMax int64 `validate:"min=1,max=10"`
78
- OmitEmpty int64 `validate:"omitempty,min=1,max=10"`
74
+ Required float64 `validate:"required"`
75
+ Len float64 `validate:"len=10"`
76
+ Min float64 `validate:"min=1"`
77
+ Max float64 `validate:"max=10"`
78
+ MinMax float64 `validate:"min=1,max=10"`
79
+ Lte float64 `validate:"lte=10"`
80
+ OmitEmpty float64 `validate:"omitempty,min=1,max=10"`
79
81
}
80
82
81
83
type TestSlice struct {
@@ -121,6 +123,133 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str
121
123
c .Assert (val .ErrorTag , Equals , expectedTag )
122
124
}
123
125
126
+ func newValidatorFunc (field interface {}, param string ) bool {
127
+
128
+ return true
129
+ }
130
+
131
+ func (ms * MySuite ) TestAddFunctions (c * C ) {
132
+
133
+ validate := validator .NewValidator ("validateme" , validator .BakedInValidators )
134
+
135
+ err := validate .AddFunction ("new" , newValidatorFunc )
136
+ c .Assert (err , IsNil )
137
+
138
+ err = validate .AddFunction ("" , newValidatorFunc )
139
+ c .Assert (err , NotNil )
140
+
141
+ validate .AddFunction ("new" , nil )
142
+ c .Assert (err , NotNil )
143
+
144
+ err = validator .AddFunction ("new" , newValidatorFunc )
145
+ c .Assert (err , IsNil )
146
+ }
147
+
148
+ func (ms * MySuite ) TestChangeTag (c * C ) {
149
+
150
+ validate := validator .NewValidator ("validateme" , validator .BakedInValidators )
151
+ validate .SetTag ("val" )
152
+
153
+ type Test struct {
154
+ Name string `val:"len=4"`
155
+ }
156
+ s := & Test {
157
+ Name : "TEST" ,
158
+ }
159
+
160
+ err := validator .ValidateStruct (s )
161
+ c .Assert (err , IsNil )
162
+
163
+ validator .SetTag ("v" )
164
+ validator .SetTag ("validate" )
165
+ }
166
+
167
+ func (ms * MySuite ) TestUnexposedStruct (c * C ) {
168
+
169
+ type Test struct {
170
+ Name string
171
+ unexposed struct {
172
+ A string `validate:"required"`
173
+ }
174
+ }
175
+ s := & Test {
176
+ Name : "TEST" ,
177
+ }
178
+
179
+ err := validator .ValidateStruct (s )
180
+ c .Assert (err , IsNil )
181
+ }
182
+
183
+ func (ms * MySuite ) TestBadParams (c * C ) {
184
+
185
+ i := 1
186
+ err := validator .ValidateFieldByTag (i , "-" )
187
+ c .Assert (err , IsNil )
188
+
189
+ c .Assert (func () { validator .ValidateFieldByTag (i , "len=a" ) }, PanicMatches , "strconv.ParseInt: parsing \" a\" : invalid syntax" )
190
+ c .Assert (func () { validator .ValidateFieldByTag (i , "len=a" ) }, PanicMatches , "strconv.ParseInt: parsing \" a\" : invalid syntax" )
191
+
192
+ var ui uint = 1
193
+ c .Assert (func () { validator .ValidateFieldByTag (ui , "len=a" ) }, PanicMatches , "strconv.ParseUint: parsing \" a\" : invalid syntax" )
194
+
195
+ f := 1.23
196
+ c .Assert (func () { validator .ValidateFieldByTag (f , "len=a" ) }, PanicMatches , "strconv.ParseFloat: parsing \" a\" : invalid syntax" )
197
+ }
198
+
199
+ func (ms * MySuite ) TestLength (c * C ) {
200
+
201
+ i := true
202
+ c .Assert (func () { validator .ValidateFieldByTag (i , "len" ) }, PanicMatches , "Bad field type bool" )
203
+ }
204
+
205
+ func (ms * MySuite ) TestIsGt (c * C ) {
206
+
207
+ myMap := map [string ]string {}
208
+ err := validator .ValidateFieldByTag (myMap , "gt=0" )
209
+ c .Assert (err , NotNil )
210
+
211
+ f := 1.23
212
+ err = validator .ValidateFieldByTag (f , "gt=5" )
213
+ c .Assert (err , NotNil )
214
+
215
+ var ui uint = 5
216
+ err = validator .ValidateFieldByTag (ui , "gt=10" )
217
+ c .Assert (err , NotNil )
218
+
219
+ i := true
220
+ c .Assert (func () { validator .ValidateFieldByTag (i , "gt" ) }, PanicMatches , "Bad field type bool" )
221
+ }
222
+
223
+ func (ms * MySuite ) TestIsGte (c * C ) {
224
+
225
+ i := true
226
+ c .Assert (func () { validator .ValidateFieldByTag (i , "gte" ) }, PanicMatches , "Bad field type bool" )
227
+ }
228
+
229
+ func (ms * MySuite ) TestIsLt (c * C ) {
230
+
231
+ myMap := map [string ]string {}
232
+ err := validator .ValidateFieldByTag (myMap , "lt=0" )
233
+ c .Assert (err , NotNil )
234
+
235
+ f := 1.23
236
+ err = validator .ValidateFieldByTag (f , "lt=0" )
237
+ c .Assert (err , NotNil )
238
+
239
+ var ui uint = 5
240
+ err = validator .ValidateFieldByTag (ui , "lt=0" )
241
+ c .Assert (err , NotNil )
242
+
243
+ i := true
244
+ c .Assert (func () { validator .ValidateFieldByTag (i , "lt" ) }, PanicMatches , "Bad field type bool" )
245
+ }
246
+
247
+ func (ms * MySuite ) TestIsLte (c * C ) {
248
+
249
+ i := true
250
+ c .Assert (func () { validator .ValidateFieldByTag (i , "lte" ) }, PanicMatches , "Bad field type bool" )
251
+ }
252
+
124
253
func (ms * MySuite ) TestUrl (c * C ) {
125
254
126
255
var tests = []struct {
@@ -172,6 +301,9 @@ func (ms *MySuite) TestUrl(c *C) {
172
301
c .Assert (err .ErrorTag , Equals , "url" )
173
302
}
174
303
}
304
+
305
+ i := 1
306
+ c .Assert (func () { validator .ValidateFieldByTag (i , "url" ) }, PanicMatches , "Bad field type int" )
175
307
}
176
308
177
309
func (ms * MySuite ) TestUri (c * C ) {
@@ -225,6 +357,9 @@ func (ms *MySuite) TestUri(c *C) {
225
357
c .Assert (err .ErrorTag , Equals , "uri" )
226
358
}
227
359
}
360
+
361
+ i := 1
362
+ c .Assert (func () { validator .ValidateFieldByTag (i , "uri" ) }, PanicMatches , "Bad field type int" )
228
363
}
229
364
230
365
func (ms * MySuite ) TestOrTag (c * C ) {
@@ -293,6 +428,9 @@ func (ms *MySuite) TestHsla(c *C) {
293
428
err = validator .ValidateFieldByTag (s , "hsla" )
294
429
c .Assert (err , NotNil )
295
430
c .Assert (err .ErrorTag , Equals , "hsla" )
431
+
432
+ i := 1
433
+ c .Assert (func () { validator .ValidateFieldByTag (i , "hsla" ) }, PanicMatches , "Bad field type int" )
296
434
}
297
435
298
436
func (ms * MySuite ) TestHsl (c * C ) {
@@ -324,6 +462,9 @@ func (ms *MySuite) TestHsl(c *C) {
324
462
err = validator .ValidateFieldByTag (s , "hsl" )
325
463
c .Assert (err , NotNil )
326
464
c .Assert (err .ErrorTag , Equals , "hsl" )
465
+
466
+ i := 1
467
+ c .Assert (func () { validator .ValidateFieldByTag (i , "hsl" ) }, PanicMatches , "Bad field type int" )
327
468
}
328
469
329
470
func (ms * MySuite ) TestRgba (c * C ) {
@@ -354,6 +495,9 @@ func (ms *MySuite) TestRgba(c *C) {
354
495
err = validator .ValidateFieldByTag (s , "rgba" )
355
496
c .Assert (err , NotNil )
356
497
c .Assert (err .ErrorTag , Equals , "rgba" )
498
+
499
+ i := 1
500
+ c .Assert (func () { validator .ValidateFieldByTag (i , "rgba" ) }, PanicMatches , "Bad field type int" )
357
501
}
358
502
359
503
func (ms * MySuite ) TestRgb (c * C ) {
@@ -380,6 +524,9 @@ func (ms *MySuite) TestRgb(c *C) {
380
524
err = validator .ValidateFieldByTag (s , "rgb" )
381
525
c .Assert (err , NotNil )
382
526
c .Assert (err .ErrorTag , Equals , "rgb" )
527
+
528
+ i := 1
529
+ c .Assert (func () { validator .ValidateFieldByTag (i , "rgb" ) }, PanicMatches , "Bad field type int" )
383
530
}
384
531
385
532
func (ms * MySuite ) TestEmail (c * C ) {
@@ -407,6 +554,9 @@ func (ms *MySuite) TestEmail(c *C) {
407
554
err = validator .ValidateFieldByTag (s , "email" )
408
555
c .Assert (err , NotNil )
409
556
c .Assert (err .ErrorTag , Equals , "email" )
557
+
558
+ i := true
559
+ c .Assert (func () { validator .ValidateFieldByTag (i , "email" ) }, PanicMatches , "Bad field type bool" )
410
560
}
411
561
412
562
func (ms * MySuite ) TestHexColor (c * C ) {
@@ -428,6 +578,9 @@ func (ms *MySuite) TestHexColor(c *C) {
428
578
err = validator .ValidateFieldByTag (s , "hexcolor" )
429
579
c .Assert (err , NotNil )
430
580
c .Assert (err .ErrorTag , Equals , "hexcolor" )
581
+
582
+ i := true
583
+ c .Assert (func () { validator .ValidateFieldByTag (i , "hexcolor" ) }, PanicMatches , "Bad field type bool" )
431
584
}
432
585
433
586
func (ms * MySuite ) TestHexadecimal (c * C ) {
@@ -440,6 +593,9 @@ func (ms *MySuite) TestHexadecimal(c *C) {
440
593
err = validator .ValidateFieldByTag (s , "hexadecimal" )
441
594
c .Assert (err , NotNil )
442
595
c .Assert (err .ErrorTag , Equals , "hexadecimal" )
596
+
597
+ i := true
598
+ c .Assert (func () { validator .ValidateFieldByTag (i , "hexadecimal" ) }, PanicMatches , "Bad field type bool" )
443
599
}
444
600
445
601
func (ms * MySuite ) TestNumber (c * C ) {
@@ -482,6 +638,9 @@ func (ms *MySuite) TestNumber(c *C) {
482
638
err = validator .ValidateFieldByTag (s , "number" )
483
639
c .Assert (err , NotNil )
484
640
c .Assert (err .ErrorTag , Equals , "number" )
641
+
642
+ i := 1
643
+ c .Assert (func () { validator .ValidateFieldByTag (i , "number" ) }, PanicMatches , "Bad field type int" )
485
644
}
486
645
487
646
func (ms * MySuite ) TestNumeric (c * C ) {
@@ -519,6 +678,9 @@ func (ms *MySuite) TestNumeric(c *C) {
519
678
err = validator .ValidateFieldByTag (s , "numeric" )
520
679
c .Assert (err , NotNil )
521
680
c .Assert (err .ErrorTag , Equals , "numeric" )
681
+
682
+ i := 1
683
+ c .Assert (func () { validator .ValidateFieldByTag (i , "numeric" ) }, PanicMatches , "Bad field type int" )
522
684
}
523
685
524
686
func (ms * MySuite ) TestAlphaNumeric (c * C ) {
@@ -704,6 +866,9 @@ func (ms *MySuite) TestStructStringValidation(c *C) {
704
866
c .Assert (len (val .StructErrors ), Equals , 0 )
705
867
706
868
AssertFieldError (val , "Test" , "required" , c )
869
+
870
+ errors := err .Error ()
871
+ c .Assert (errors , NotNil )
707
872
}
708
873
709
874
func (ms * MySuite ) TestStructInt32Validation (c * C ) {
0 commit comments