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

Adds ability to validate oneof for space separated strings #541

Merged
merged 2 commits into from
Dec 24, 2019

Conversation

JonathanWThom
Copy link
Contributor

Fixes Or Enhances #525.

Make sure that you've checked the boxes below before you submit PR:

  • 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

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
Copy link
Contributor

@deankarn deankarn left a 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+`)
Copy link
Contributor

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 🙏

Copy link
Contributor Author

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)
Copy link
Contributor

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*

Copy link
Contributor Author

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 👍

@JonathanWThom
Copy link
Contributor Author

Thanks for taking a look @deankarn. I'll try to find time to address your comments in the coming week.

@JonathanWThom
Copy link
Contributor Author

I believe I've accommodated your feedback - let me know if there's anything else that could be improved. Thanks!

@deankarn deankarn merged commit a6a294b into go-playground:master Dec 24, 2019
@JonathanWThom
Copy link
Contributor Author

Thanks for merging this and #548 @deankarn!

@deankarn
Copy link
Contributor

Np I still have to cut a release, merging and porting some other things before cutting one also :)

fairyhunter13 added a commit to fairyhunter13/validator that referenced this pull request Jul 12, 2020
…rated-oneof

Adds ability to validate oneof for space separated strings
@lhphongclassfunc
Copy link

Thanks for merging this and #548 @deankarn!

thanks for your contribution, today i encountered this case and solved it successfully 💪🏻

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.

None yet

3 participants