Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for type-specific Validation based on Interface #1304

Open
2 tasks done
mvrahden opened this issue Aug 12, 2024 · 0 comments
Open
2 tasks done

Add support for type-specific Validation based on Interface #1304

mvrahden opened this issue Aug 12, 2024 · 0 comments

Comments

@mvrahden
Copy link

  • 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant