-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validate tag not working in ShouldBindWith #3234
Comments
You should use binding:"omitempty,oneof=ASC DESC" Because gin has modified the validator's tagname : func (v *defaultValidator) lazyinit() {
v.once.Do(func() {
v.validate = validator.New()
v.validate.SetTagName("binding")
})
} |
@eleven26 : If i use |
The |
@eleven26 : Thanks for the confirmation, let me try out and get back. Till then please leave the issue open, i expect to close on this by this Friday . I will close the issue with comments my self if all goes well |
@eleven26 : Thanks all the things worked except on last thing on the validator for dates type GeneralValidatorQueryParams struct {
StartedAt time.Time `form:"startedAt" binding:"omitempty,required_with=EndedAt"`
EndedAt time.Time `form:"endedAt" binding:"omitempty,required_with=StartedAt,gtfield=StartedAt"`
} In this case i was trying to use this for a GET request and these are query parameters.
|
type GeneralValidatorQueryParams struct {
StartedAt time.Time `form:"startedAt" binding:"required_with=EndedAt"`
EndedAt time.Time `form:"endedAt" binding:"omitempty,required_with=StartedAt,gtfield=StartedAt"`
} This may be a correct example.
You may not fully understand the usage of
|
got it thanks, i will close the issue now :) |
Description
I am trying to get the validation working in gin, with the validator package this validator tag works good. When i use it in gin it's not working and i am not sure if i am using it wrong or it's not working as a bug.
What all i did before opening this issue
I looked at the gin code and see here that
ShouldBindWith()
should eventually call this .https://github.com/gin-gonic/gin/blob/4b68a5f12af4d6d2be83e1895f783d5dd5d5a148/binding/query.go
Once validate() is called it should validate using the validator package for the validate tag, which is not working as expected
validate:"omitempty,oneof=ASC DESC
Any help is appreciated on this
How to reproduce
Expectations
Here i expect the curl command to return me a StatusBadRequest and text "failed"
Actual result
Actual result is code StatusOk and "Success"
Environment
github.com/gin-gonic/gin v1.8.1 // indirect
The text was updated successfully, but these errors were encountered: