need to check atleast one of field should be present email, phone atleast one is mandatory
currently I have custom validator
func validateEmailPhone(fl validator.FieldLevel) bool {
user := fl.Top().Interface().(models.User)
validate := validator.Validate{}
if user.Email == "" && user.Phone == "" {
return false
}
if user.Email != "" {
if err := validate.Var(user.Email, "email"); err != nil {
return false
}
}
if user.Phone != "" {
if err := validate.Var(user.Phone, "e164"); err != nil {
return false
}
}
return true
}
but its panic
the user is gorm model
in this scenario how to check these two fields
There are plenty of conditional required
tags in validator
.
In your case, you probably want required_without_all
+ omitempty
.
required_without_all
- makes a field required if all fields in the list are empty.
omitempty
- simply allows the field to be empty.
Please keep in mind that the required tag must come before omitempty.