-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
How to validate bool
#142
Comments
I think there may be some small confusion about how this works. Validator validates the data within the struct after it has been in unmarshalled from JSON. So the steps that happen are:
Since go is statically typed, regardless if truthiness is passed or not, the structure will always have a value and since go's bool's are not tri-state. It does not validate the JSON but data after unmarshalling. Sorry for no samples, answering from my phone. |
Give me a few minutes, I have an idea that may work for you. Just tied up at the moment |
I was thinking you could make the bool value a pointer, then it would fail when no value was present, but it would still fail ifvaluewas false so that won't work. I may look into adding a new tag, "exists" it will not have any validation functions associated with it, however will be the failure tag when value is a pointer and is value of nil. In the mean time however, there really isn't a way to check if a bool is set. |
I was going to maybe take a stab at writing a custom validator that could just check if something exists, but I figured I'd ask here first in case I misread the situation. |
I'm actually in the middle of adding the "exists" tag it's seem to work well when using bool as a pointer, will let you know when it's released; hopefully tonight. |
Awesome! thanks! |
Hey @davisford, guess I was closer than I though lol Exampleif you use "exists" tag like below and bool is a pointer you will get the functionality you are looking for. type Thing struct {
Truthiness *bool `json:"truthiness" validate:"exists"`
} NotesThis change was implemented in v6 and back-ported to v5. I Highly recommend everyone to update to v6 if possible as some things added in v6 already cannot be back-ported to v5 due to the code change between v5 and v6 UPDATE: Please let me know if this works as expected for you and I can close this issue |
Awesome -- thank you. I may have to make my own fork of gin and update to v6. Might take a bit -- sounds like you tested it out...feel free to close the issue. I appreciate the super immediate response time. |
no problems, yes I tested with your exact scenario with bool as a pointer and using exists flag. P.S. @davisford I think gin allows for custom/other validation libraries to be plugged in, in this case just the updated version, however I don't have a link to the commit, or documentation. P.P.S star if you like, just trying to get more stars than the other validators out there, even ones that are unmaintained have more stars 😦 |
Hey @davisford I came across how to add your own validator in gin to avoid forking, it's in this issue 2nd or 3rd comment gin-gonic/gin#224 (comment) |
Is this still supported in v9 ? I need the same behaviour for HTTP PATCH where I have to distinguish between ommited and set values - therefore the bool pointer. I'm getting the error that EDIT: I found the |
Hey @codepushr I dropped if not then it could be a bug, could you provide some sample code and I'll check it out ASAP |
Sure! This should demonstrate what I mean. Basically the validation fails although I'm using a pointer to a bool with package main
import (
"log"
v "gopkg.in/go-playground/validator.v9"
)
type Foo struct {
Bar *bool `validate:"required"`
}
func main() {
b := false
model := Foo{
Bar: &b,
}
err := v.New().Struct(model)
log.Println(err)
} |
Hey @codepushr thanks for the code, I already replicated the issue, just running last minute tests and fix will be out in a few minutes so will tag you and this issue when making the final release. P.S. thanks for reporting. |
ok @codepushr release 9.2.0 should fix this for you. Please let me know if it works for you 😃 NOTE: the reason I removed |
@joeybloggs Looking good! Thanks for the amazingly quick response! :) |
thanks all |
Payload:
Response message:
Source: https://github.com/lucassimon/fiber-estudos |
Same issue as the above. Could you suggest the best solution for v10? |
Reopened in #714. |
model
func for validate
and it works. To validate if bool exists, you need to make bool as a pointer. Note: I tested it in v9 |
Status *bool , If the bool value is set to a pointer, the error is fixed. Version 10.10.0 |
getting this error
|
*bool still doesn't work for me. Any ideas to solve this guys? |
Same problem here guys. Version v10.11.1: type BaseMaterialData struct {
Name string `json:"name" validate:"required,max=125"`
Description string `json:"description" validate:"max=256"`
Active bool `json:"active" validate:"required"`
} When a json with "active: false" arrives, validator fails. |
@EduFrazao try remove required, set default value to false |
Thanks. I've done that as a workaround and its running. |
Hi, man! Can u show me how u did that? I'm experiencing the same error but cant change my type to *bool |
if you use Active bool `json:"active" validate:"required"` and if the payload send Active bool `json:"active" validate:"required,boolean"` the same I tried it like so: Active bool `json:"active" validate:"boolean"` it works, but when payload does not contain the This is my validator version: |
@nsmosi this has been explained many times in a number of issues. I believe it is a misunderstanding of how Go works. as defined on your struct is a bool which has two states true and false. In go there is no such thing as an uninitialized variable and so if the value is not defined in the JSON it will be false by default on your struct. if false being the default state if not in the JSON is desired then theres really no need to validate it at all. if you need to detect presence then you need a third state. Changing to a pointer *bool is one way to give this third state to ensure it was set. |
Using gin-gonic/gin with this, when it marshals JSON into a struct, this passes:
But this fails, b/c it is the golang zero-value for
bool
I just want to validate that the property exists -- if it exists, I know it will have a value of true or false.
Nothing obvious popped out at me from: https://godoc.org/gopkg.in/bluesuncorp/validator.v5#pkg-variables
The text was updated successfully, but these errors were encountered: