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 alphaspace validator #1343

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

takaaa220
Copy link

Fixes Or Enhances

Add alphaspace validator for #1191.

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

@coveralls
Copy link

coveralls commented Dec 7, 2024

Coverage Status

coverage: 74.322% (+0.004%) from 74.318%
when pulling da6de87 on takaaa220:add_alpha_space_validator
into 6c3307e on go-playground:master.

@@ -7,6 +7,7 @@ import (

const (
alphaRegexString = "^[a-zA-Z]+$"
alphaSpaceRegexString = "^[a-zA-Z ]+$"
Copy link
Author

Choose a reason for hiding this comment

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

Would it be better for the regex to not match if the input contains only spaces?

Choose a reason for hiding this comment

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

I would leave it like that

But people already asked for what you suggest, so I don't know

#1191 (comment)

But then, should leading and trailing space are valid?

  • foo, trailing space, valid or invalid?
  • foo , leading space, valid or invalid
  • `` empty, invalid?
  • only space, invalid?
  • foo bar

I'm asking because the regexp will need to be adapted

Copy link
Author

@takaaa220 takaaa220 Dec 9, 2024

Choose a reason for hiding this comment

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

Actually, I reviewed #1191 (comment) before asking the question. My question was more for clarification to ensure we’re aligned on the intended behavior.
I think that making the regex more complex to handle edge cases like these is unnecessary. Current implementation avoids unnecessary complexity while meeting the expected use cases.

Copy link
Member

Choose a reason for hiding this comment

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

I think the validator should do what it says and nothing else. If a string contains any of the alpha characters or an empty space it should be valid.

Anything about spaces before or after should then be dealt with by the user IMO. Unless someone can give a great example why " a" is invalid and "a a" should be valid I think this is good.

@takaaa220 takaaa220 marked this pull request as ready for review December 7, 2024 21:47
@takaaa220 takaaa220 requested a review from a team as a code owner December 7, 2024 21:47
doc.go Outdated
@@ -762,6 +762,12 @@ This validates that a string value contains ASCII alpha characters only

Usage: alpha

# Alpha Space

This validate that a string value contains ASCII alpha characters and spaces only

Choose a reason for hiding this comment

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

Suggested change
This validate that a string value contains ASCII alpha characters and spaces only
This validates that a string value contains ASCII alpha characters and spaces only

Copy link
Author

@takaaa220 takaaa220 Dec 9, 2024

Choose a reason for hiding this comment

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

fixed it
da6de87

@@ -7,6 +7,7 @@ import (

const (
alphaRegexString = "^[a-zA-Z]+$"
alphaSpaceRegexString = "^[a-zA-Z ]+$"

Choose a reason for hiding this comment

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

I would leave it like that

But people already asked for what you suggest, so I don't know

#1191 (comment)

But then, should leading and trailing space are valid?

  • foo, trailing space, valid or invalid?
  • foo , leading space, valid or invalid
  • `` empty, invalid?
  • only space, invalid?
  • foo bar

I'm asking because the regexp will need to be adapted

@@ -134,6 +134,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
| Tag | Description |
| - | - |
| alpha | Alpha Only |
| alphaspace | Alpha Space |

Choose a reason for hiding this comment

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

I'm a bit annoyed, this could lead to obesity

I mean, someone will then ask for alphanumspace, alphaunicodespace, and so on

The regexps are computed by the lib even if no one use it, so lib becomes slower and slower.

The PR addresses the issue raised. So it's OK.

But I would like to step out a bit.

The regexp rule idea was already declined, for good reasons

So I think the way to consider doing this is via the custom validator

#1191 (comment)

Copy link
Author

Choose a reason for hiding this comment

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

@ccoVeille
Thank you for your detailed feedback. I understand and agree your concerns regarding the potential growth and its impact on library performance.

If you feel a custom validator is the better path for this functionality, I’m happy to close this pull request if necessary.

Choose a reason for hiding this comment

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

I'm a random Gopher,user of the lib

Let's wait for a maintainer's feedback

Copy link
Author

Choose a reason for hiding this comment

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

I'm a random Gopher,user of the lib

oh, I mistakenly thought you were the maintainer.

Copy link
Member

Choose a reason for hiding this comment

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

Well, the regex's are lazy loaded, so if anyone doesn't use them, it doesn't affect them as much.

In this case if people think this is a good addition, I don't think we should block it because we worry about it not being used and slightly bigger binary.

Choose a reason for hiding this comment

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

Thanks for your explanation

@takaaa220 takaaa220 requested a review from ccoVeille December 9, 2024 03:44
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.

LGTM

@@ -7,6 +7,7 @@ import (

const (
alphaRegexString = "^[a-zA-Z]+$"
alphaSpaceRegexString = "^[a-zA-Z ]+$"
Copy link
Member

Choose a reason for hiding this comment

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

I think the validator should do what it says and nothing else. If a string contains any of the alpha characters or an empty space it should be valid.

Anything about spaces before or after should then be dealt with by the user IMO. Unless someone can give a great example why " a" is invalid and "a a" should be valid I think this is good.

@@ -134,6 +134,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
| Tag | Description |
| - | - |
| alpha | Alpha Only |
| alphaspace | Alpha Space |
Copy link
Member

Choose a reason for hiding this comment

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

Well, the regex's are lazy loaded, so if anyone doesn't use them, it doesn't affect them as much.

In this case if people think this is a good addition, I don't think we should block it because we worry about it not being used and slightly bigger binary.

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