go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.73k stars 1.32k forks source link

Undefined validation function ' max' on field '*' #453

Open mh-cbon opened 5 years ago

mh-cbon commented 5 years ago

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

Given that field definition

Password    string     `json:"password" validate:"iftrue=public, max=20, min=4" conform:"text"`

Application emitted a panic because it could not find the rule " max" with a prefixed white space.

I d have expected the tag to be trimmed, if possible.

stacktrace


2019/03/07 19:08:05 http: panic serving [::1]:45894: Undefined validation function ' max' on field 'Password'
goroutine 40 [running]:
net/http.(*conn).serve.func1(0xc4203c19a0)
    /home/mh-cbon/.gvm/gos/go1.10/src/net/http/server.go:1726 +0xd0
panic(0x9a2d40, 0xc4200a70d0)
    /home/mh-cbon/.gvm/gos/go1.10/src/runtime/panic.go:505 +0x229
gopkg.in/go-playground/validator%2ev9.(*Validate).parseFieldTagsRecursive(0xc42017fe00, 0x9c4475, 0x1c, 0x9c4451, 0x8, 0x9c4483, 0x0, 0x9a2d00, 0x9c445b, 0x46)
    /home/mh-cbon/gow/src/gopkg.in/go-playground/validator.v9/cache.go:304 +0x102f
gopkg.in/go-playground/validator%2ev9.(*Validate).extractStructCache(0xc42017fe00, 0xa4f120, 0xc4203f05b0, 0x199, 0x961a36, 0x7, 0x0)
    /home/mh-cbon/gow/src/gopkg.in/go-playground/validator.v9/cache.go:156 +0x464
gopkg.in/go-playground/validator%2ev9.(*validate).validateStruct(0xc4200d0fc0, 0xada600, 0xc4200b2010, 0x9885e0, 0xc4203f05b0, 0x16, 0xa4f120, 0xc4203f05b0, 0x199, 0xae0940, ...)
    /home/mh-cbon/gow/src/gopkg.in/go-playground/validator.v9/validator.go:37 +0xadc
gopkg.in/go-playground/validator%2ev9.(*Validate).StructCtx(0xc42017fe00, 0xada600, 0xc4200b2010, 0x9885e0, 0xc4203f05b0, 0x988501, 0xc4200a6ff0)
    /home/mh-cbon/gow/src/gopkg.in/go-playground/validator.v9/validator_instance.go:289 +0x320
gopkg.in/go-playground/validator%2ev9.(*Validate).Struct(0xc42017fe00, 0x9885e0, 0xc4203f05b0, 0x9885e0, 0xc4203f05b0)
    /home/mh-cbon/gow/src/gopkg.in/go-playground/validator.v9/validator_instance.go:262 +0x57
...

only the relevant part of the stacktrace.
deankarn commented 5 years ago

@mh-cbon I suppose a LeftTrim could fix this after the splitting of the tag, however, is definitely a breaking change, just in case someone has registered a validation containing a space already.

will add it to a v10 enhancement.

MounirOnGithub commented 4 years ago

Encountered the same issue, is there any solution ?