Skip to content

Commit 432c170

Browse files
committed
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:** - [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
1 parent dbbe695 commit 432c170

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

baked_in.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"os"
1111
"reflect"
12+
"regexp"
1213
"strconv"
1314
"strings"
1415
"sync"
@@ -177,7 +178,8 @@ func parseOneOfParam2(s string) []string {
177178
oneofValsCacheRWLock.RUnlock()
178179
if !ok {
179180
oneofValsCacheRWLock.Lock()
180-
vals = strings.Fields(s)
181+
re := regexp.MustCompile(`'[^']*'|\S+`)
182+
vals = re.FindAllString(s, -1)
181183
oneofValsCache[s] = vals
182184
oneofValsCacheRWLock.Unlock()
183185
}
@@ -213,7 +215,8 @@ func isOneOf(fl FieldLevel) bool {
213215
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
214216
}
215217
for i := 0; i < len(vals); i++ {
216-
if vals[i] == v {
218+
val := strings.Replace(vals[i], "'", "", -1)
219+
if val == v {
217220
return true
218221
}
219222
}

doc.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,12 @@ One Of
361361
362362
For strings, ints, and uints, oneof will ensure that the value
363363
is one of the values in the parameter. The parameter should be
364-
a list of values separated by whitespace. Values may be
365-
strings or numbers.
364+
a list of values separated by whitespace. Values may be
365+
strings or numbers. To match strings with spaces in them, include
366+
the target string between single quotes.
366367
367368
Usage: oneof=red green
369+
oneof='red green' 'blue yellow'
368370
oneof=5 7 9
369371
370372
Greater Than

validator_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
"testing"
1515
"time"
1616

17+
. "github.com/go-playground/assert/v2"
1718
"github.com/go-playground/locales/en"
1819
"github.com/go-playground/locales/fr"
1920
"github.com/go-playground/locales/nl"
2021
ut "github.com/go-playground/universal-translator"
21-
. "github.com/go-playground/assert/v2"
2222
)
2323

2424
// NOTES:
@@ -4484,6 +4484,8 @@ func TestOneOfValidation(t *testing.T) {
44844484
}{
44854485
{f: "red", t: "oneof=red green"},
44864486
{f: "green", t: "oneof=red green"},
4487+
{f: "red green", t: "oneof='red green' blue"},
4488+
{f: "blue", t: "oneof='red green' blue"},
44874489
{f: 5, t: "oneof=5 6"},
44884490
{f: 6, t: "oneof=5 6"},
44894491
{f: int8(6), t: "oneof=5 6"},
@@ -4509,6 +4511,7 @@ func TestOneOfValidation(t *testing.T) {
45094511
}{
45104512
{f: "", t: "oneof=red green"},
45114513
{f: "yellow", t: "oneof=red green"},
4514+
{f: "green", t: "oneof='red green' blue"},
45124515
{f: 5, t: "oneof=red green"},
45134516
{f: 6, t: "oneof=red green"},
45144517
{f: 6, t: "oneof=7"},

0 commit comments

Comments
 (0)