You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi @aloha1003 , in the readme there is a example how to use custom validators.
Custom validators
It is possible to define custom validators by using SetValidationFunc. First, one needs to create a validation function.
// Very simple validator
func notZZ(v interface{}, param string) error {
st := reflect.ValueOf(v)
if st.Kind() != reflect.String {
return errors.New("notZZ only validates strings")
}
if st.String() == "ZZ" {
return errors.New("value cannot be ZZ")
}
return nil
}
Then one needs to add it to the list of validators and give it a "tag" name.
validator.SetValidationFunc("notzz", notZZ)
Then it is possible to use the notzz validation tag. This will print "Field A error: value cannot be ZZ"
type T struct {
A string `validate:"nonzero,notzz"`
}
t := T{"ZZ"}
if errs := validator.Validate(t); errs != nil {
fmt.Printf("Field A error: %s\n", errs["A"][0])
}
I want to add some custom validator rule base on your validator
How can I do to add custom rule ?
The text was updated successfully, but these errors were encountered: