-
Notifications
You must be signed in to change notification settings - Fork 1.4k
required_without doesn't seem to work with multiple fields #617
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
Comments
Hey @fizzy123 this will work if you use I could use some help with that if you have some time :) |
I just got bit w/ this too 😃
So, am I right in thinking that It looks that way from my tests but thought I'd ask before I go too deep into the rabbit hole. Also, are these the docs you refer too? Line 293 in 0ab8e03
Cheers 👍 |
I'm having the same problem:
This should imho error but doesn't. My expectation for string was
Could you kindly clarify what the expected required behaviour depending on type is? I'd be happy in contributing to the docs (or making doc.go more visible), but I'm still struggling with how it's supposed to work. |
See examples from the docs:
Yes it says "or". To achieve that "or" validation, here's what I do:
|
@yaliv reading the play from @fizzy123 the requirement is for validation to fail when all fields are empty. Your suggestion is the same as the play and imho works when any of the fields are empty. The way to go is |
@andig I don't think so. @fizzy123 wrote in the playground:
So the expected result is: |
Now let's compare it with
If we modify the validation above:
The expected result is: If any of those 3 fields has value, then Field1 is not required. I've tried both scenarios. |
Indeed, some of the docs are not 100% correct. But I hope this library has 3 types of
As I said before, the current workaround for
|
In the play, fields 2..4 are populated. Field 1 should never throw a validation error regardless if without or without_all is used, yet it does. If you see my issue above you‘ll see another problem with the logic- also without_all seems to error (or rather not error) when it should. Imho there is something wrong with the without* validations. |
@andig let's try both validators right in the playground:
Furthermore, let's stay with the
That's because |
Now let's talk about your example code. If you don't fill any fields like this:
you'll get an error: Field |
@yaliv you are right. Thank you for correcting my mistake. I really appreciate your feedback. There is still something wrong with
This should error on |
Nope, that's fine. I do the same. See mine:
From the data flow perspective, I actually don't need any validations here, because:
But there are still advantages of setting validations here:
As you can see above, it's currently the best way I can write the validations. |
Hi, Just checking if this was fixed already, or planned to be fixed? type a struct { type b struct { https://go.dev/play/p/kOF6ROYXlw1 Will be happy if this would be solved. Thanks. |
…1422) ## Fixes Or Enhances Resolves #617 Fixes `required_without` so that it aligns with the stated contract and usage examples. The [docs](https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Required_Without) valid usage to be: ``` // require the field if the Field1 is not present: Usage: required_without=Field1 // require the field if the Field1 or Field2 is not present: Usage: required_without=Field1 Field2 ``` However, as show in this issue: #617, you can only use `required_without` on multiple fields like: ```golang type Test struct { Field1 string `validator:required_without=Field2,required_without=Field3" ... } ``` This PR aligns the behavior of the `required_without` validator such that you can also use it as shown in the example: ```golang type Test struct { Field1 string `validator:required_without=Field2 Field3" ... } ``` **Make sure that you've checked the boxes below before you submit PR:** - [x] Tests exist or have been written that cover this particular change. Note: Sorry for the duplication of #1324. I was cleaning up my profile and deleted my fork before it could be merged. @go-playground/validator-maintainers
Package version eg. v8, v9:
Package version v9
Issue, Question or Enhancement:
In this example, I would expect validation to succeed, but it doesn't despite all the
required_without
fields being present.Code sample, to showcase or reproduce:
https://play.golang.org/p/O9CuEA2aOFr
The text was updated successfully, but these errors were encountered: