New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation seems to fail for boolean 'false' #713
Comments
the same issue |
by default however, if see https://pkg.go.dev/gopkg.in/go-playground/validator.v10#hdr-Required |
By default catalogo will be private, unless EsPublico explicitly set to True go-playground/validator#713
Same issue here. validate:"boolean" fails whichever value (true/false) is passed in a json request |
I am having the same issue. |
Me too. Anyone can help me? |
Forgive the short reply, answering from phone. Please read the documentation for required and it should make sense why false fails the required check AND this was explained above by @joebartels Please let me know if I’m missing something and something else is wrong. |
If you want to validate that the value is present you will have to use a pointer there. So for the example given, the change should be something like this. something struct {
- Fieldx bool `json:"fieldx" validate:"required"`
+ Fieldx *bool `json:"fieldx" validate:"required"`
} I will close this as answered. |
You can use this way:
Use a pointer thanks @zemzale |
This is extremely dangerous because if a caller forgets/does not to specify a field when it is required by contract then it would lead to unintended consequences. This is very bad design. |
@SharkFourSix not sure how long you’ve been using Go but there is no concept of an uninitialized variable and so ever variable will get a default value including when unmarshalling data into a struct. and so to that end there are two options:
I think you may be confusing bad design with the facts and semantics of the Go language. I would be glad to be wrong but as far as I know there is no other way to detect presence vs default. |
Well, at the same time, it doesn't make sense to treat That right there is where the bad design is. Without even looking at semantics or language behavior. Assignment of default values to uninitialized variables is not exclusive to go. So this is not a problem with the underlying language. You want to leave that behavior to the language and let it do what it does (throw a null pointer exception or panic or whatever or assign whatever initialization value the compiler may decide) because then and only then will the programmer realize that they did not provide a value to that value, instead of the library hijacking that behavior because it decided to interpret certain valid values as if the memory/variable containing that said value doesn't exist, which again, is bad design. Also, it's not like the library is allocating the memory for me to worry about initialization. That's my business, not the library's. |
If the Deployable bool field is `false`, the validation from the "required" tag will fail, as it's considered the zero-value for bools. In our case both `true` and `false` are valid, so there's no need to use the "required" tag. go-playground/validator#713
* fix: remove "required" binding from Deployable bool field If the Deployable bool field is `false`, the validation from the "required" tag will fail, as it's considered the zero-value for bools. In our case both `true` and `false` are valid, so there's no need to use the "required" tag. go-playground/validator#713 * fix: handle duplicated key error for group creation
@deankarn @SharkFourSix |
Package version:
v10
Issue, Question or Enhancement:
Seems that validation fails on boolean types when value is passed as 'false', but works for 'true'. Appreciate any insight - thanks!
Postman submission gives the same result.
Code sample, to showcase or reproduce:
The text was updated successfully, but these errors were encountered: