Skip to content

Add optional Validate() error method - #102

Closed
peczenyj wants to merge 4 commits into
dmarkham:masterfrom
peczenyj:add-validate-method
Closed

peczenyj wants to merge 4 commits into
dmarkham:masterfrom
peczenyj:add-validate-method

Conversation

@peczenyj

@peczenyj peczenyj commented Jan 15, 2025 •

Copy link
Copy Markdown
Contributor

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 <nodivbyzero@gmail.com>
@peczenyj

Copy link
Copy Markdown
Contributor Author

conflict solved

mia-rx pushed a commit to mia-rx/rx-ad-go-playground-validator that referenced this pull request Apr 17, 2026
## 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 <nodivbyzero@gmail.com>
mia-rx pushed a commit to mia-rx/rx-ad-go-playground-validator that referenced this pull request Apr 17, 2026
## 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 <nodivbyzero@gmail.com>
Resolved conflicts in stringer.go, enumer.go and golden_test.go, caused
by master refactoring Generator.generate to take a generateOptions
struct while this branch added a -validate flag as a positional bool.

- stringer.go: added includeValidateFunc to generateOptions, registered
  the -validate flag via flag.BoolVar(&opts.includeValidateFunc, ...) in
  init(), and switched the guard in generate to opts.includeValidateFunc.
- enumer.go: kept both sides' additions (buildValidateMethod plus
  master's flag/pflag method builders).
- golden_test.go: kept master's generateOptions-based runGoldenTest and
  re-expressed the validate golden case as
  generateOptions{transformMethod: "noop", includeValidateFunc: true}.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@dmarkham

dmarkham commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Thank you, this was cleanly done. Landed as #130, based on your PR with credit to you and @mbyio. The only addition is that Validate() now wraps enumerrs.ErrValueInvalid when -typederrors is set, so errors.Is works the same way it does for TString(). Closing in favor of #130

@dmarkham dmarkham closed this Oct 3, 2026
dmarkham added a commit to peczenyj/enumer that referenced this pull request Oct 3, 2026
Validate() returns nil for a declared constant and an error naming the
value otherwise, so enum fields can be checked through a common
interface instead of the type-specific IsAT() bool.

With -typederrors the error wraps enumerrs.ErrValueInvalid, matching
TString() and the unmarshal methods.

The end-to-end harness now takes extra flags per fixture instead of a
single typed-errors boolean. Two fixtures cover declared values, gap
values, out-of-range values, calls through an interface, and errors.Is
against the sentinel.

Based on dmarkham#102 by peczenyj, a port of dmarkham#82 by mbyio.
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.

2 participants