-
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
Adds ability to validate oneof for space separated strings #541
Adds ability to validate oneof for space separated strings #541
Conversation
Fixes Or Enhances go-playground#525. **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. Change Details: * Adds the ability to match on space separated strings when using the `oneof` validation. Space separted strings must be surrounded by single quotes to be validated as one string. For example: ``` oneof='Awaiting Verification' 'Verified' 'Failed Verification' ``` passes validation for a field that is exactly `Failed Verification` (though just `Failed` would...fail). @go-playground/admins
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.
Awesome start, with a few changes, can not only help oneof
😄 🔥
baked_in.go
Outdated
@@ -177,7 +178,8 @@ func parseOneOfParam2(s string) []string { | |||
oneofValsCacheRWLock.RUnlock() | |||
if !ok { | |||
oneofValsCacheRWLock.Lock() | |||
vals = strings.Fields(s) | |||
re := regexp.MustCompile(`'[^']*'|\S+`) |
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.
Can this be moved out out the hotpath? eg. a global variable 🙏
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.
Good idea, I will do that.
baked_in.go
Outdated
@@ -213,7 +215,8 @@ func isOneOf(fl FieldLevel) bool { | |||
panic(fmt.Sprintf("Bad field type %T", field.Interface())) | |||
} | |||
for i := 0; i < len(vals); i++ { | |||
if vals[i] == v { | |||
val := strings.Replace(vals[i], "'", "", -1) |
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.
instead of separating this logic from the parseOneOfParam2
function is it possible to do the replace before adding to the oneofValsCache
cache, then the output of the function is constent and all other validations can benefit from the same changes 😄 eg. required_with*
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'll give this a shot 👍
Thanks for taking a look @deankarn. I'll try to find time to address your comments in the coming week. |
2ba4e5d
to
3b3263e
Compare
3b3263e
to
685d3e2
Compare
I believe I've accommodated your feedback - let me know if there's anything else that could be improved. Thanks! |
Np I still have to cut a release, merging and porting some other things before cutting one also :) |
…rated-oneof Adds ability to validate oneof for space separated strings
Fixes Or Enhances #525.
Make sure that you've checked the boxes below before you submit PR:
Change Details:
oneof
validation. Space separted strings must be surrounded by singlequotes to be validated as one string. For example:
passes validation for a field that is exactly
Failed Verification
(though just
Failed
would...fail).@go-playground/admins