Validator: getting a failed validation for a struct with a single boolean field

Created on 15 Dec 2015  路  3Comments  路  Source: go-playground/validator

So i have a struct called
type Respond struct { Accepted bool "json:"accepted" binding:"required"" }

and trying to do validation on it and keep getting "Key: 'Respond.Accepted' Error:Field validation for 'Accepted' failed on the 'required' tag"

Im passing {"accepted":false} which is a valid json response.. and get the error above.. if I pass {"accepted":true} it validates properly..

Whats going on?? Im noticing this on validator.v5 all the way to validator.v8

question

Most helpful comment

Hi @donileo I've covered this in several issues https://github.com/go-playground/validator/issues/142 https://github.com/gin-gonic/gin/issues/491...

Anyways so summarise the "required" in the godocs states that it validates that the value isn't the fields default types default value, which for a bool is false and so properly fails validation. What I think your looking for is to use the "exists" tag and need to change the bool to be a pointer *bool like so:

type Respond struct { Accepted *bool "`json:"accepted" binding:"exists"`" }

The reason this needs to be done is because go is a static language, just to go through how binding works, assuming your using gin, is that the json is unmarshalled into your struct but before it can do that a new struct needs to be created and because go is static every field within will get a default value, the only way to validate exists is to make the field a pointer essentially making the field nullable.

I wish there was a better way but have yet to find one. Most of the time this is a non issue though as for a bool not passing false is false, for a string it's blank.

I hope my answer helps please let me know if this makes sense or have anymore questions.

All 3 comments

Hi @donileo I've covered this in several issues https://github.com/go-playground/validator/issues/142 https://github.com/gin-gonic/gin/issues/491...

Anyways so summarise the "required" in the godocs states that it validates that the value isn't the fields default types default value, which for a bool is false and so properly fails validation. What I think your looking for is to use the "exists" tag and need to change the bool to be a pointer *bool like so:

type Respond struct { Accepted *bool "`json:"accepted" binding:"exists"`" }

The reason this needs to be done is because go is a static language, just to go through how binding works, assuming your using gin, is that the json is unmarshalled into your struct but before it can do that a new struct needs to be created and because go is static every field within will get a default value, the only way to validate exists is to make the field a pointer essentially making the field nullable.

I wish there was a better way but have yet to find one. Most of the time this is a non issue though as for a bool not passing false is false, for a string it's blank.

I hope my answer helps please let me know if this makes sense or have anymore questions.

Thanks @joeybloggs. That clarifies some things yes. Whats the difference between the "exists" and "required" tag? Seems required implies exists....

@donileo I don't disagree if I were to redo it from scratch I may have named "required" something else like "nondefault" or something, but would be a big breaking change, maybe for v9 someday.

the difference with "exists" is that if validator finds that the field kind, after trying to find the underlying type, is still reflect.Ptr, reflect.Interface or reflect.Invalid and there are validation tags i.e. "exists" then that means there is no value and will fail on that tag. It's a little complicated if reading the code but validator will get into this is statement https://github.com/go-playground/validator/blob/v8/validator.go#L564 with your example when using "exists" and *bool

NOTE: "exists" should be the first tag if multiple as when an error like the above occurs it will use the first tag.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thaonx picture thaonx  路  3Comments

scisci picture scisci  路  5Comments

geraldstanje picture geraldstanje  路  5Comments

deschmih picture deschmih  路  3Comments

dumindu picture dumindu  路  5Comments