Skip to content

Commit a6a294b

Browse files
author
Dean Karn
authored
Merge pull request #541 from JonathanWThom/jt/space-separated-oneof
Adds ability to validate oneof for space separated strings
2 parents c067a8f + 685d3e2 commit a6a294b

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

baked_in.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ func parseOneOfParam2(s string) []string {
177177
oneofValsCacheRWLock.RUnlock()
178178
if !ok {
179179
oneofValsCacheRWLock.Lock()
180-
vals = strings.Fields(s)
180+
vals = splitParamsRegex.FindAllString(s, -1)
181+
for i := 0; i < len(vals); i++ {
182+
vals[i] = strings.Replace(vals[i], "'", "", -1)
183+
}
181184
oneofValsCache[s] = vals
182185
oneofValsCacheRWLock.Unlock()
183186
}

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

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})`
4747
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
4848
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
49+
splitParamsRegexString = `'[^']*'|\S+`
4950
)
5051

5152
var (
@@ -92,4 +93,5 @@ var (
9293
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
9394
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
9495
hTMLRegex = regexp.MustCompile(hTMLRegexString)
96+
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
9597
)

validator_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,6 +4487,8 @@ func TestOneOfValidation(t *testing.T) {
44874487
}{
44884488
{f: "red", t: "oneof=red green"},
44894489
{f: "green", t: "oneof=red green"},
4490+
{f: "red green", t: "oneof='red green' blue"},
4491+
{f: "blue", t: "oneof='red green' blue"},
44904492
{f: 5, t: "oneof=5 6"},
44914493
{f: 6, t: "oneof=5 6"},
44924494
{f: int8(6), t: "oneof=5 6"},
@@ -4512,6 +4514,7 @@ func TestOneOfValidation(t *testing.T) {
45124514
}{
45134515
{f: "", t: "oneof=red green"},
45144516
{f: "yellow", t: "oneof=red green"},
4517+
{f: "green", t: "oneof='red green' blue"},
45154518
{f: 5, t: "oneof=red green"},
45164519
{f: 6, t: "oneof=red green"},
45174520
{f: 6, t: "oneof=7"},

0 commit comments

Comments
 (0)