martini-contrib / binding

Martini handler for mapping and validating a raw request into a structure.
MIT License
140 stars 45 forks source link

Error with binding false `bool` values and binding:"required" #42

Closed oweidner closed 9 years ago

oweidner commented 9 years ago
type ThisObj struct {
    ID         string       `json:"id"`
    Name       string       `json:"name" binding:"required"`
    Online     bool         `json:"online" binding:"required"`
}

In the above example, bind gives me a RequiredErrorwhen I POST the following JSON string:

{
    "name": "test",
    "online": false
}

However, when I set online to 'true' in the above JSON string, binding works fine. I assume this is a bug as a false value should still fulfill the required tag?

mholt commented 9 years ago

No, this is expected. The readme says,

Note: Marking a field as "required" means that you do not allow the zero value for that type (i.e. if you want to allow 0 in an int field, do not make it required).

There is no way to infer from the zero value whether it was missing or not. This is because binding is a process independent of deserialization.

oweidner commented 9 years ago

Ah, never mind then. Thanks for clarifying!