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
Add validating sub structures #224
Add validating sub structures #224
Conversation
@zazab I totally need this PR! This is what I also mentioned in this issue: @javierprovecho any chance of this being included any time soon? Binding is kinda broken now as sub structs (very common in all but the most simple JSON setups) are not checked against |
Add validating sub structures
I was testing this PR and did not get the expected result. The provided example actually parses fine and does give the expected error. Either I'm missing something here or something else is wrong. Please see this gist to reproduce: |
@tnolet tested this case. |
@zazab You are absolutely right. I thought something was wrong with my test and there was. Thanks for the reply. I'll just edit the gist so other people may later find it useful. |
@zazab Sorry to open this one up again immediately. I think I did find an issue with structures nested in arrays/slices. These are not validated, am I correct? I've updated the gist with a test case. |
@tnolet problem is not with slices. Problem is pointers. If you change struct to something like this:
Validation would work as expected. But if you use pointers in struct, for example:
This would not validate correct. Pointer in slices fails too. |
For slices works the same pointer problem. Here minimal example:
Validation works
Validation doesn't work |
Validation in Gin is a third party dependency, but you can still use your own validation engine. binding.Validator = YOUR_VALIDATOR in fact, Gin has a shortcut to disable the validation: func DisableBindValidation() {
binding.Validator = nil
} let's see what @joeybloggs thinks about this. I or someone may should prepare a PR to the validator repo. |
I knew this would come up sooner or later ;) I've been thinking about how to represent the errors within my error hierarchy and just hadn't firmed up exactly how I wanted to do it. I've created an issue for this and it is my top priority go-playground/validator#78 any ideas or suggestions would definitely be helpful |
@manucorporat and all others I have implemented validation of slices, arrays and maps within issue go-playground/validator#78 it allows for validation of any nested type, not just structs, and supports multidimensional validation at any or all levels. see the new "dive" tag within the documentation. I hope this addition serves you well! |
@joeybloggs that's awesome! good job. @zazab is it ok for you? |
@manucorporat @joeybloggs Does Gin via Bind/BindWith expose FieldError's as well? |
Looks like it is possible via |
And depending on your needs you can use the struct errors as is, which are stored in the same hierarchy as the struct itself, which I find better for backend processing and automation tasks. or you can use structure errors Flatten() function, which will give back a namespaced map[string]*FieldError which I find better for rendering For example. type Test structure {
Names []string 'validate:"dive,required"'
}
t := &Test{
Names: []string{"OK",""}
}
...
If you call Flatten() on the resulting struct error the result would be a map entry with key "Names[1]" and value of *FieldError It's whatever works best for you, just thought I'd highlight that the option is there. |
It was hard for me to find this result. If you are looking for validating nested struct in gin, do the following.
|
This fix can help validating sub structures, for example we have something like this:
Without this patch Bind wont fail on something like this json
This allow to define optional fields, that, if present, shoud fit some critreias.