Closed rahmathu closed 2 years ago
You should use binding
, not validate
:
binding:"omitempty,oneof=ASC DESC"
Because gin has modified the validator's tagname : default_validator.go#L95
func (v *defaultValidator) lazyinit() {
v.once.Do(func() {
v.validate = validator.New()
v.validate.SetTagName("binding")
})
}
@eleven26 : Thanks for the replies , some more questions to this
If i use binding
in place of validate
the "omitempty" does not work as it tries to unmarshall and expects it to be there. Do you know how may i do an omitempty
and then oneof in the same tag. In case it will be a custom validator please give me an example with this sample code as i have tried may options but it's not wokring for me . Thanks in advance
The omitempty
in binding
just means that if your request does not have this field in it, it will not check other validation rules.
@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.
startedAt
field. required_with
is not taking effect here with binding . omitempty
, because you already have required_with
, which means you only check StartedAt
when you pass EndedAt
parameter, otherwise, validator
will not check other validation rules when it sees omitempty
: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.
required_with
you can't use omitempty
anymore, if you add omitempty
and the field doesn't exist required_with
and other validation rules no longer workYou may not fully understand the usage of omitempty
. The following is the description of omitempty in the official documentation:
validator.v9#hdr-Omit_Empty Allows conditional validation, for example if a field is not set with a value (Determined by the "required" validator) then other validation such as min or max won't run, but if a value is set validation will run.
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 expectedvalidate:"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