Feat: support slice on isOneOf

pull/949/head
Marine Bal 3 years ago
parent 9e2ea40380
commit b7cdec1ddb
  1. 25
      baked_in.go
  2. 5
      validator_test.go

@ -250,11 +250,7 @@ func isHTML(fl FieldLevel) bool {
return hTMLRegex.MatchString(fl.Field().String()) return hTMLRegex.MatchString(fl.Field().String())
} }
func isOneOf(fl FieldLevel) bool { func checkIsOneOf(field reflect.Value, vals []string) bool {
vals := parseOneOfParam2(fl.Param())
field := fl.Field()
var v string var v string
switch field.Kind() { switch field.Kind() {
case reflect.String: case reflect.String:
@ -266,6 +262,7 @@ func isOneOf(fl FieldLevel) bool {
default: default:
panic(fmt.Sprintf("Bad field type %T", field.Interface())) panic(fmt.Sprintf("Bad field type %T", field.Interface()))
} }
for i := 0; i < len(vals); i++ { for i := 0; i < len(vals); i++ {
if vals[i] == v { if vals[i] == v {
return true return true
@ -274,6 +271,24 @@ func isOneOf(fl FieldLevel) bool {
return false return false
} }
func isOneOf(fl FieldLevel) bool {
vals := parseOneOfParam2(fl.Param())
field := fl.Field()
value, kind, _ := fl.ExtractType(field)
if kind == reflect.Slice {
for i := 0; i < value.Len(); i++ {
if !checkIsOneOf(value.Index(i), vals) {
return false
}
}
return true
}
return checkIsOneOf(field, vals)
}
// isUnique is the validation function for validating if each array|slice|map value is unique // isUnique is the validation function for validating if each array|slice|map value is unique
func isUnique(fl FieldLevel) bool { func isUnique(fl FieldLevel) bool {
field := fl.Field() field := fl.Field()

@ -5493,10 +5493,13 @@ 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: []string{"red", "green"}, t: "oneof=red green"},
{f: "red green", t: "oneof='red green' blue"}, {f: "red green", t: "oneof='red green' blue"},
{f: "blue", t: "oneof='red green' blue"}, {f: "blue", t: "oneof='red green' blue"},
{f: []string{"red green", "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: []int{6, 5}, t: "oneof=5 6"},
{f: int8(6), t: "oneof=5 6"}, {f: int8(6), t: "oneof=5 6"},
{f: int16(6), t: "oneof=5 6"}, {f: int16(6), t: "oneof=5 6"},
{f: int32(6), t: "oneof=5 6"}, {f: int32(6), t: "oneof=5 6"},
@ -5521,9 +5524,11 @@ 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: "green", t: "oneof='red green' blue"},
{f: []string{"green", "blue"}, 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"},
{f: []int{6, 7}, t: "oneof=7"},
{f: uint(6), t: "oneof=7"}, {f: uint(6), t: "oneof=7"},
{f: int8(5), t: "oneof=red green"}, {f: int8(5), t: "oneof=red green"},
{f: int16(5), t: "oneof=red green"}, {f: int16(5), t: "oneof=red green"},

Loading…
Cancel
Save