validator
validator copied to clipboard
How do I combine required_if with a rule
- [x] I have looked at the documentation here first?
- [x] I have looked at the examples provided that may showcase my question here?
Package version eg. v9, v10:
v10
Issue, Question or Enhancement:
I'd like to validate a field that is required only if another field has a certain value.
I see that there's required_if which allows me to achieve part of this, but I'd like to chain it with another rule uuid4 iff required_if is satisfied.
Code sample, to showcase or reproduce:
Example of what it could look like:
type Thing struct {
Type string `validate:"oneof=A B"`
Value string `validate:"required_if=Type A,uuid|omitempty"`
}
Value is required if Type == A, and must be a uuid. Otherwise it must be empty
duplicate https://github.com/go-playground/validator/issues/963