validator icon indicating copy to clipboard operation
validator copied to clipboard

How do I combine required_if with a rule

Open OscarVanL opened this issue 3 years ago • 1 comments

  • [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

OscarVanL avatar Aug 23 '22 19:08 OscarVanL

duplicate https://github.com/go-playground/validator/issues/963

oneart-dev avatar Sep 06 '22 18:09 oneart-dev