Skip to content

Conversation

peczenyj
Copy link

@peczenyj peczenyj commented Jan 15, 2025

port of #82

this pull request will adds an optional method Validate() error -- and this is a very common interface -- that return an error if the IsA<Type>() bool returns false.

The advantage over IsA<Type>() bool is the interface. Makes easier to integrate with other tools such as validators

nodivbyzero added a commit to go-playground/validator that referenced this pull request Apr 16, 2025
## Fixes Or Enhances

This Pull Requests adds a new tag called `isvalid`

If the field is marked with the validator tag `isvalid`, the type must
implement the interface `Validate() error` and the return must be nil to
be considered `valid`

A possible use case is: when dealing with Enumerations, the type can
support a method `Validate() error` to check if the value is in specific
the range defined. If we use
[enumer](https://github.com/dmarkham/enumer) it generates a `IsA<Type>()
bool` method that can be used to verify if the enumeration is valid or
not, instead force the `oneof` tag (that needs to be always updated when
we add one new value.

I wrote a pull request to add a Validate method on enumerations
[here](dmarkham/enumer#102) and the interface
`Validate() error` seems pretty common.

It may clash with existing tags that people may register, this is
something that I don't know how to solve.

**Make sure that you've checked the boxes below before you submit PR:**
- [X] Tests exist or have been written that cover this particular
change.

@go-playground/validator-maintainers

---------

Co-authored-by: nodivbyzero <[email protected]>
@peczenyj
Copy link
Author

conflict solved

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

Successfully merging this pull request may close these issues.

1 participant