File tree 3 files changed +45
-2
lines changed
3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,11 @@ if errs := validator.Validate(nur); errs != nil {
42
42
43
43
Builtin validators
44
44
45
- Here is the list of validators buildin in the package.
45
+ Here is the list of validators buildin in the package. Validators buildin
46
+ will check the element pointed to if the value to check is a pointer.
47
+ The ` nil ` pointer is treated as a valid value by validators buildin other
48
+ than ` nonzero ` , so you should to use ` nonzero ` if you don't want to
49
+ accept a ` nil ` pointer.
46
50
47
51
```
48
52
len
Original file line number Diff line number Diff line change @@ -62,6 +62,12 @@ func nonzero(v interface{}, param string) error {
62
62
func length (v interface {}, param string ) error {
63
63
st := reflect .ValueOf (v )
64
64
valid := true
65
+ if st .Kind () == reflect .Ptr {
66
+ if st .IsNil () {
67
+ return nil
68
+ }
69
+ st = st .Elem ()
70
+ }
65
71
switch st .Kind () {
66
72
case reflect .String :
67
73
p , err := asInt (param )
@@ -109,6 +115,12 @@ func length(v interface{}, param string) error {
109
115
func min (v interface {}, param string ) error {
110
116
st := reflect .ValueOf (v )
111
117
invalid := false
118
+ if st .Kind () == reflect .Ptr {
119
+ if st .IsNil () {
120
+ return nil
121
+ }
122
+ st = st .Elem ()
123
+ }
112
124
switch st .Kind () {
113
125
case reflect .String :
114
126
p , err := asInt (param )
@@ -156,6 +168,12 @@ func min(v interface{}, param string) error {
156
168
func max (v interface {}, param string ) error {
157
169
st := reflect .ValueOf (v )
158
170
var invalid bool
171
+ if st .Kind () == reflect .Ptr {
172
+ if st .IsNil () {
173
+ return nil
174
+ }
175
+ st = st .Elem ()
176
+ }
159
177
switch st .Kind () {
160
178
case reflect .String :
161
179
p , err := asInt (param )
@@ -201,7 +219,14 @@ func max(v interface{}, param string) error {
201
219
func regex (v interface {}, param string ) error {
202
220
s , ok := v .(string )
203
221
if ! ok {
204
- return ErrUnsupported
222
+ sptr , ok := v .(* string )
223
+ if ! ok {
224
+ return ErrUnsupported
225
+ }
226
+ if sptr == nil {
227
+ return nil
228
+ }
229
+ s = * sptr
205
230
}
206
231
207
232
re , err := regexp .Compile (param )
Original file line number Diff line number Diff line change @@ -313,6 +313,20 @@ func (ms *MySuite) TestValidatePointerVar(c *C) {
313
313
314
314
err = validator .Validate (& test6 {& test2 {}})
315
315
c .Assert (err , IsNil )
316
+
317
+ type test7 struct {
318
+ A * string `validate:"min=6"`
319
+ B * int `validate:"len=7"`
320
+ C * int `validate:"min=12"`
321
+ }
322
+ s := "aaa"
323
+ b := 8
324
+ err = validator .Validate (& test7 {& s , & b , nil })
325
+ errs , ok = err .(validator.ErrorMap )
326
+ c .Assert (ok , Equals , true )
327
+ c .Assert (errs ["A" ], HasError , validator .ErrMin )
328
+ c .Assert (errs ["B" ], HasError , validator .ErrLen )
329
+ c .Assert (errs ["C" ], Not (HasError ), validator .ErrMin )
316
330
}
317
331
318
332
func (ms * MySuite ) TestValidateOmittedStructVar (c * C ) {
You can’t perform that action at this time.
0 commit comments