Merge pull request #541 from JonathanWThom/jt/space-separated-oneof

Adds ability to validate oneof for space separated strings
pull/564/head
Dean Karn 5 years ago committed by GitHub
commit a6a294bfeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      baked_in.go
  2. 4
      doc.go
  3. 2
      regexes.go
  4. 3
      validator_test.go

@ -177,7 +177,10 @@ func parseOneOfParam2(s string) []string {
oneofValsCacheRWLock.RUnlock() oneofValsCacheRWLock.RUnlock()
if !ok { if !ok {
oneofValsCacheRWLock.Lock() 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 oneofValsCache[s] = vals
oneofValsCacheRWLock.Unlock() oneofValsCacheRWLock.Unlock()
} }

@ -362,9 +362,11 @@ One Of
For strings, ints, and uints, oneof will ensure that the value For strings, ints, and uints, oneof will ensure that the value
is one of the values in the parameter. The parameter should be is one of the values in the parameter. The parameter should be
a list of values separated by whitespace. Values may be a list of values separated by whitespace. Values may be
strings or numbers. strings or numbers. To match strings with spaces in them, include
the target string between single quotes.
Usage: oneof=red green Usage: oneof=red green
oneof='red green' 'blue yellow'
oneof=5 7 9 oneof=5 7 9
Greater Than Greater Than

@ -46,6 +46,7 @@ const (
uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})` uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})`
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?` hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>` hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
splitParamsRegexString = `'[^']*'|\S+`
) )
var ( var (
@ -92,4 +93,5 @@ var (
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString) uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString) hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
hTMLRegex = regexp.MustCompile(hTMLRegexString) hTMLRegex = regexp.MustCompile(hTMLRegexString)
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
) )

@ -4487,6 +4487,8 @@ func TestOneOfValidation(t *testing.T) {
}{ }{
{f: "red", t: "oneof=red green"}, {f: "red", t: "oneof=red green"},
{f: "green", 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: 5, t: "oneof=5 6"},
{f: 6, t: "oneof=5 6"}, {f: 6, t: "oneof=5 6"},
{f: int8(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: "", t: "oneof=red green"},
{f: "yellow", t: "oneof=red green"}, {f: "yellow", t: "oneof=red green"},
{f: "green", t: "oneof='red green' blue"},
{f: 5, t: "oneof=red green"}, {f: 5, t: "oneof=red green"},
{f: 6, t: "oneof=red green"}, {f: 6, t: "oneof=red green"},
{f: 6, t: "oneof=7"}, {f: 6, t: "oneof=7"},

Loading…
Cancel
Save