Skip to content

OR Operators (Or / OrElse)

This content is for v0.7. Switch to the latest version for up-to-date documentation.

Valgo supports OR-style logic inside a validator chain.

  • Or(...) evaluates an alternative branch.
  • OrElse(...) is like Or, but lets you override the resulting message when all branches fail.

Require either an email-like string or a phone-like string:

emailRe := regexp.MustCompile(`^.+@.+\..+$`)
phoneRe := regexp.MustCompile(`^\+?[0-9]{7,}$`)
val := v.Is(
v.String(contact, "contact").
MatchingTo(emailRe).
Or(v.String(contact, "contact").MatchingTo(phoneRe)),
)

If you want a single consolidated error message:

val := v.Is(
v.String(contact, "contact").
MatchingTo(emailRe).
OrElse(
v.String(contact, "contact").MatchingTo(phoneRe),
"Contact must be a valid email or phone number",
),
)