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 to tag isvalid #1363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

peczenyj
Copy link

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 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 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:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@peczenyj peczenyj requested a review from a team as a code owner January 17, 2025 12:53
@coveralls
Copy link

Coverage Status

coverage: 74.338% (+0.02%) from 74.318%
when pulling d05e279 on peczenyj:add-support-to-tag-valid
into 2cce309 on go-playground:master.

@nodivbyzero
Copy link
Contributor

How does this isvalid differ from custom validators?
https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Custom_Validation_Functions

String string `validate:"is-awesome"`

@tizu69
Copy link

tizu69 commented Feb 11, 2025

How does this isvalid differ from custom validators? pkg.go.dev/github.com/go-playground/validator/v10#hdr-Custom_Validation_Functions

String string `validate:"is-awesome"`

This seems more like a utility validator, and I'm all for it. It allows custom validators without ever touching reflection yourself, and instead having the library do the heavy lifting. The developer using isvalid just returns an error if they notice the struct is bad, and thats it. Benefit would also be that this may work with other validators if they adapt it.

@nodivbyzero
Copy link
Contributor

The isvalid tag seems somewhat vague in its intent. Without a clear definition, it may be unclear how it interacts with other validation mechanisms, especially since different users might interpret it differently.
Would love to hear others’ thoughts as well!
cc: @chipaca, @deankarn, @alob-mtc @zemzale

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.

4 participants