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