diff --git a/baked_in.go b/baked_in.go index 9fe732c..04cf57d 100644 --- a/baked_in.go +++ b/baked_in.go @@ -177,7 +177,10 @@ func parseOneOfParam2(s string) []string { oneofValsCacheRWLock.RUnlock() if !ok { oneofValsCacheRWLock.Lock() - vals = strings.Fields(s) + vals = splitParamsRegex.FindAllString(s, -1) + for i := 0; i < len(vals); i++ { + vals[i] = strings.Replace(vals[i], "'", "", -1) + } oneofValsCache[s] = vals oneofValsCacheRWLock.Unlock() } diff --git a/doc.go b/doc.go index 95029dd..cdfb759 100644 --- a/doc.go +++ b/doc.go @@ -361,10 +361,12 @@ One Of For strings, ints, and uints, oneof will ensure that the value is one of the values in the parameter. The parameter should be -a list of values separated by whitespace. Values may be -strings or numbers. +a list of values separated by whitespace. Values may be +strings or numbers. To match strings with spaces in them, include +the target string between single quotes. Usage: oneof=red green + oneof='red green' 'blue yellow' oneof=5 7 9 Greater Than diff --git a/regexes.go b/regexes.go index 7161804..6343abe 100644 --- a/regexes.go +++ b/regexes.go @@ -46,6 +46,7 @@ const ( uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})` hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(>)|(<)|(")|(&)+[;]?` hTMLRegexString = `<[/]?([a-zA-Z]+).*?>` + splitParamsRegexString = `'[^']*'|\S+` ) var ( @@ -92,4 +93,5 @@ var ( uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString) hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString) hTMLRegex = regexp.MustCompile(hTMLRegexString) + splitParamsRegex = regexp.MustCompile(splitParamsRegexString) ) diff --git a/validator_test.go b/validator_test.go index 7ea5be5..968503e 100644 --- a/validator_test.go +++ b/validator_test.go @@ -4487,6 +4487,8 @@ func TestOneOfValidation(t *testing.T) { }{ {f: "red", t: "oneof=red green"}, {f: "green", t: "oneof=red green"}, + {f: "red green", t: "oneof='red green' blue"}, + {f: "blue", t: "oneof='red green' blue"}, {f: 5, t: "oneof=5 6"}, {f: 6, t: "oneof=5 6"}, {f: int8(6), t: "oneof=5 6"}, @@ -4512,6 +4514,7 @@ func TestOneOfValidation(t *testing.T) { }{ {f: "", t: "oneof=red green"}, {f: "yellow", t: "oneof=red green"}, + {f: "green", t: "oneof='red green' blue"}, {f: 5, t: "oneof=red green"}, {f: 6, t: "oneof=red green"}, {f: 6, t: "oneof=7"},