-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
- I have looked at the documentation here first?
- I have looked at the examples provided that may showcase my question here?
Package version eg. v9, v10:
v10
Enhancement:
Topic: Add support for Type-specific Validation based on Interface.
I'd like to propose to extend the required
validation logic (under a new name, e.g. must
) from a simple "Not Zero" check to contain an alternative path which would call an interface – if present – on the type and validate based on the implemented logic.
The interface could look like
type Validator interface { Validate() error }
Benefit with this interface: It is compatible with other validation libs as well.
Adding this validation, will allow to define custom type-specific validations and ensure their invocation based on the given tag (e.g. must
).
If the validator interface is not defined on a type, the validation logic will fallback to the required
validation logic.
Alternatively: The validation could also first perform the required
validation and subsequently perform the interface based validation logic.
Feedback highly appreciated.
Similar Request: Issue 854
Code sample, to showcase or reproduce:
type SomeStruct struct {
ID RID `validate:"must"`
}
type RID string
func (r RID) Validate() error {
// type specific validation, just an example
if !strings.HasPrefix(r, "_") {
return errors.New("missing prefix \"_\"")
}
return nil
}