gin bind自动omitempty问题

type Foo struct {
Bar bool json:"bar" binding:"required" // True / False
}
When I send a JSON request with the value true it works, but when I send the same request with false I get the following error.



{
“error”: {
“Foo.Bar”: {
“FieldNamespace”: “Foo.Bar”,
“NameNamespace”: “Bar”,
“Field”: “Bar”,
“Name”: “Bar”,
“Tag”: “required”,
“ActualTag”: “required”,
“Kind”: 1,
“Type”: {},
“Param”: “”,
“Value”: false
}
}
},

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”.



to further this answer exists was removed in v9 and required works with *bool



I highly recommend updating to v9, there were many performance updates and changes for ease of use :)



https://github.com/gin-gonic/gin/issues/814


Category golang