-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
Add alphaspace validator #1343
Conversation
@@ -7,6 +7,7 @@ import ( | |||
|
|||
const ( | |||
alphaRegexString = "^[a-zA-Z]+$" | |||
alphaSpaceRegexString = "^[a-zA-Z ]+$" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
But then, should leading and trailing space are valid?
foo
, trailing space, valid or invalid?foo
, leading space, valid or invalid- `` empty, invalid?
foo bar
I'm asking because the regexp will need to be adapted
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
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 ]+$" |
There was a problem hiding this comment.
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
But then, should leading and trailing space are valid?
foo
, trailing space, valid or invalid?foo
, leading space, valid or invalid- `` empty, 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 | |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your explanation
There was a problem hiding this 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 ]+$" |
There was a problem hiding this comment.
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 | |
There was a problem hiding this comment.
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.
Fixes Or Enhances
Add alphaspace validator for #1191.
Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers