@@ -29,6 +29,7 @@ type TestInterface struct {
2929}
3030
3131type TestString struct {
32+ BlankTag string `validate:""`
3233 Required string `validate:"required"`
3334 Len string `validate:"len=10"`
3435 Min string `validate:"min=1"`
@@ -70,12 +71,13 @@ type TestUint64 struct {
7071}
7172
7273type 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"`
7981}
8082
8183type TestSlice struct {
@@ -121,6 +123,133 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str
121123 c .Assert (val .ErrorTag , Equals , expectedTag )
122124}
123125
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+
124253func (ms * MySuite ) TestUrl (c * C ) {
125254
126255 var tests = []struct {
@@ -172,6 +301,9 @@ func (ms *MySuite) TestUrl(c *C) {
172301 c .Assert (err .ErrorTag , Equals , "url" )
173302 }
174303 }
304+
305+ i := 1
306+ c .Assert (func () { validator .ValidateFieldByTag (i , "url" ) }, PanicMatches , "Bad field type int" )
175307}
176308
177309func (ms * MySuite ) TestUri (c * C ) {
@@ -225,6 +357,9 @@ func (ms *MySuite) TestUri(c *C) {
225357 c .Assert (err .ErrorTag , Equals , "uri" )
226358 }
227359 }
360+
361+ i := 1
362+ c .Assert (func () { validator .ValidateFieldByTag (i , "uri" ) }, PanicMatches , "Bad field type int" )
228363}
229364
230365func (ms * MySuite ) TestOrTag (c * C ) {
@@ -293,6 +428,9 @@ func (ms *MySuite) TestHsla(c *C) {
293428 err = validator .ValidateFieldByTag (s , "hsla" )
294429 c .Assert (err , NotNil )
295430 c .Assert (err .ErrorTag , Equals , "hsla" )
431+
432+ i := 1
433+ c .Assert (func () { validator .ValidateFieldByTag (i , "hsla" ) }, PanicMatches , "Bad field type int" )
296434}
297435
298436func (ms * MySuite ) TestHsl (c * C ) {
@@ -324,6 +462,9 @@ func (ms *MySuite) TestHsl(c *C) {
324462 err = validator .ValidateFieldByTag (s , "hsl" )
325463 c .Assert (err , NotNil )
326464 c .Assert (err .ErrorTag , Equals , "hsl" )
465+
466+ i := 1
467+ c .Assert (func () { validator .ValidateFieldByTag (i , "hsl" ) }, PanicMatches , "Bad field type int" )
327468}
328469
329470func (ms * MySuite ) TestRgba (c * C ) {
@@ -354,6 +495,9 @@ func (ms *MySuite) TestRgba(c *C) {
354495 err = validator .ValidateFieldByTag (s , "rgba" )
355496 c .Assert (err , NotNil )
356497 c .Assert (err .ErrorTag , Equals , "rgba" )
498+
499+ i := 1
500+ c .Assert (func () { validator .ValidateFieldByTag (i , "rgba" ) }, PanicMatches , "Bad field type int" )
357501}
358502
359503func (ms * MySuite ) TestRgb (c * C ) {
@@ -380,6 +524,9 @@ func (ms *MySuite) TestRgb(c *C) {
380524 err = validator .ValidateFieldByTag (s , "rgb" )
381525 c .Assert (err , NotNil )
382526 c .Assert (err .ErrorTag , Equals , "rgb" )
527+
528+ i := 1
529+ c .Assert (func () { validator .ValidateFieldByTag (i , "rgb" ) }, PanicMatches , "Bad field type int" )
383530}
384531
385532func (ms * MySuite ) TestEmail (c * C ) {
@@ -407,6 +554,9 @@ func (ms *MySuite) TestEmail(c *C) {
407554 err = validator .ValidateFieldByTag (s , "email" )
408555 c .Assert (err , NotNil )
409556 c .Assert (err .ErrorTag , Equals , "email" )
557+
558+ i := true
559+ c .Assert (func () { validator .ValidateFieldByTag (i , "email" ) }, PanicMatches , "Bad field type bool" )
410560}
411561
412562func (ms * MySuite ) TestHexColor (c * C ) {
@@ -428,6 +578,9 @@ func (ms *MySuite) TestHexColor(c *C) {
428578 err = validator .ValidateFieldByTag (s , "hexcolor" )
429579 c .Assert (err , NotNil )
430580 c .Assert (err .ErrorTag , Equals , "hexcolor" )
581+
582+ i := true
583+ c .Assert (func () { validator .ValidateFieldByTag (i , "hexcolor" ) }, PanicMatches , "Bad field type bool" )
431584}
432585
433586func (ms * MySuite ) TestHexadecimal (c * C ) {
@@ -440,6 +593,9 @@ func (ms *MySuite) TestHexadecimal(c *C) {
440593 err = validator .ValidateFieldByTag (s , "hexadecimal" )
441594 c .Assert (err , NotNil )
442595 c .Assert (err .ErrorTag , Equals , "hexadecimal" )
596+
597+ i := true
598+ c .Assert (func () { validator .ValidateFieldByTag (i , "hexadecimal" ) }, PanicMatches , "Bad field type bool" )
443599}
444600
445601func (ms * MySuite ) TestNumber (c * C ) {
@@ -482,6 +638,9 @@ func (ms *MySuite) TestNumber(c *C) {
482638 err = validator .ValidateFieldByTag (s , "number" )
483639 c .Assert (err , NotNil )
484640 c .Assert (err .ErrorTag , Equals , "number" )
641+
642+ i := 1
643+ c .Assert (func () { validator .ValidateFieldByTag (i , "number" ) }, PanicMatches , "Bad field type int" )
485644}
486645
487646func (ms * MySuite ) TestNumeric (c * C ) {
@@ -519,6 +678,9 @@ func (ms *MySuite) TestNumeric(c *C) {
519678 err = validator .ValidateFieldByTag (s , "numeric" )
520679 c .Assert (err , NotNil )
521680 c .Assert (err .ErrorTag , Equals , "numeric" )
681+
682+ i := 1
683+ c .Assert (func () { validator .ValidateFieldByTag (i , "numeric" ) }, PanicMatches , "Bad field type int" )
522684}
523685
524686func (ms * MySuite ) TestAlphaNumeric (c * C ) {
@@ -704,6 +866,9 @@ func (ms *MySuite) TestStructStringValidation(c *C) {
704866 c .Assert (len (val .StructErrors ), Equals , 0 )
705867
706868 AssertFieldError (val , "Test" , "required" , c )
869+
870+ errors := err .Error ()
871+ c .Assert (errors , NotNil )
707872}
708873
709874func (ms * MySuite ) TestStructInt32Validation (c * C ) {
0 commit comments