You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm attempting to enforce required JSON request fields using the binding="required" tag. I expected c.BindJSON to raise an error when a required field was not present, however, it also raises an error when a required bool field is set to false.
Is there a better way to enforce required JSON request bodies?
Here is an example:
funcmyHandler(c*gin.Context) {
varreqstruct {
Foostring`json:"foo" binding="required"`Barbool`json:"bar" binding="required"`
}
iferr:=c.BindJSON(&req); err!=nil {
// When a request's `foo` field is set to `false`, the following error is raised:// err="Key: '.Foo' Error:Field validation for 'Foo' failed on the 'required' tag"
}
}
The text was updated successfully, but these errors were encountered:
Just an update to whomever is still stuck with this bug. Addind *bool is not the entire solution. You must change the binding:"required" to binding:"exists".
Just an update to whomever is still stuck with this bug. Addind *bool is not the entire solution. You must change the binding:"required" to binding:"exists".
I'm attempting to enforce required JSON request fields using the
binding="required"
tag. I expectedc.BindJSON
to raise an error when a required field was not present, however, it also raises an error when a required bool field is set tofalse
.Is there a better way to enforce required JSON request bodies?
Here is an example:
The text was updated successfully, but these errors were encountered: