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 4 commits 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

coveralls commented Jan 17, 2025

Coverage Status

coverage: 74.254% (+0.01%) from 74.241%
when pulling 1cd3439 on peczenyj:add-support-to-tag-valid
into 8592022 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

@deankarn
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

I agree I don't love the tag name especially because I could foresee a future similar validation added where the function signature is IsValid() bool which this tag would be more appropriate for, love the rest of the PR and it's intent thought, I have no issues there.

WDYT about validateFn or validateIface something to denote it's doing a validation from external code?

@tizu69
Copy link

tizu69 commented Feb 15, 2025

validateFn sounds good to me

@peczenyj
Copy link
Author

Sorry guys I just lost this entire discussion,

I have one clear motivation: if I have a custom validator, lets say is-a-valid-xxx-enumeration, and I am using in a library, I have to expose this custom validator as a public function (for instance) AND use it each time I need to include the structure that have this is-a-valid-xxx-enumeration or it will fail and that can be... tedious. and if I forget in one place (imagine a lib that include another, etc) I may have a problem in runtime if I don't add sufficient tests. Of course, it is not impossible, but tedious.

also, if I have several enumerations, I end up with several custom validators. Or I can use a generic one. That is my motivation

last the oneof tag is useful for string types, not for enumerations based on integers/ uint8 (like the ones generated by some tool like enumer)

so I can rename it to validateFn (or validateMethod) is everyone agrees

zemzale
zemzale previously approved these changes Mar 25, 2025
Copy link
Member

@zemzale zemzale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the PR, the only thing I would add is a negative test that it doesn't panic when the type doesn't implement the validate function.

It's not required, but would help to ensure that interface conversions are not throwing panics.

fix doc

Co-authored-by: nodivbyzero <[email protected]>
Copy link
Contributor

@nodivbyzero nodivbyzero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

It would be beneficial to add a new example to showcase the functionality. While unit tests are valuable, having both a unit test and an example is even better ;)

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.

6 participants