Skip to content

Commit afce000

Browse files
authored
excluded_unless bug fix (#1307)
## Fixes Or Enhances The verbiage in the documentation for both `excluded_unless` and `required_unless` is very similar. One would expect the opposite behavior for each when using multiple values of the same field that is being validated against. For example: This fails in all 3 cases in sample code ```go package main import ( "fmt" "github.com/go-playground/validator/v10" ) type TestStruct struct { Foo int Bar string `validate:"excluded_unless=Foo 1 Foo 2"` } func main() { validator := validator.New() testA := TestStruct{ Foo: 1, Bar: "test", } testB := TestStruct{ Foo: 2, Bar: "test", } testC := TestStruct{ Foo: 3, Bar: "test", } err := validator.Struct(testA) if err != nil { fmt.Println(err.Error()) } err = validator.Struct(testB) if err != nil { fmt.Println(err.Error()) } err = validator.Struct(testC) if err != nil { fmt.Println(err.Error()) } } ``` However, the equivilant opposite of this only fails in TestC ```go type TestStruct struct { Foo int Bar string `validate:"required_unless=Foo 1 Foo 2"` } func main() { validator := validator.New() testA := TestStruct{ Foo: 1, } testB := TestStruct{ Foo: 2, } err := validator.Struct(testA) if err != nil { fmt.Println(err.Error()) } err = validator.Struct(testB) if err != nil { fmt.Println(err.Error()) } } ``` resolves #1306
1 parent 0c1335d commit afce000

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

baked_in.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,11 +1989,12 @@ func excludedUnless(fl FieldLevel) bool {
19891989
panic(fmt.Sprintf("Bad param number for excluded_unless %s", fl.FieldName()))
19901990
}
19911991
for i := 0; i < len(params); i += 2 {
1992-
if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
1993-
return !hasValue(fl)
1992+
if requireCheckFieldValue(fl, params[i], params[i+1], false) {
1993+
return true
19941994
}
19951995
}
1996-
return true
1996+
1997+
return !hasValue(fl)
19971998
}
19981999

19992000
// excludedWith is the validation function

validator_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11443,6 +11443,21 @@ func TestRequiredUnless(t *testing.T) {
1144311443
}
1144411444
errs = validate.Struct(test6)
1144511445
NotEqual(t, errs, nil)
11446+
11447+
test7 := struct {
11448+
Field1 int
11449+
Field2 string `validate:"required_unless=Field1 1 Field1 2"`
11450+
Field3 int
11451+
Field4 string `validate:"required_unless=Field3 1 Field3 2"`
11452+
}{
11453+
Field1: 1,
11454+
Field3: 3,
11455+
}
11456+
11457+
errs = validate.Struct(test7)
11458+
NotEqual(t, errs, nil)
11459+
11460+
AssertError(t, errs, "Field4", "Field4", "Field4", "Field4", "required_unless")
1144611461
}
1144711462

1144811463
func TestSkipUnless(t *testing.T) {
@@ -12474,6 +12489,26 @@ func TestExcludedUnless(t *testing.T) {
1247412489
Inner: &Inner{Field: &fieldVal},
1247512490
}
1247612491
_ = validate.Struct(panicTest)
12492+
12493+
test9 := struct {
12494+
Field1 int
12495+
Field2 string `validate:"excluded_unless=Field1 1 Field1 2"`
12496+
Field3 int
12497+
Field4 string `validate:"excluded_unless=Field3 1 Field3 2"`
12498+
}{
12499+
Field1: 1,
12500+
Field2: "foo",
12501+
Field3: 3,
12502+
Field4: "foo",
12503+
}
12504+
12505+
errs = validate.Struct(test9)
12506+
NotEqual(t, errs, nil)
12507+
12508+
ve = errs.(ValidationErrors)
12509+
Equal(t, len(ve), 1)
12510+
12511+
AssertError(t, errs, "Field4", "Field4", "Field4", "Field4", "excluded_unless")
1247712512
}
1247812513

1247912514
func TestLookup(t *testing.T) {

0 commit comments

Comments
 (0)