Skip to content

Commit 2cb9f1d

Browse files
committed
allows multiple values of same field on excluded_uunless to have opposite equivilant result to required_unless
1 parent 0c1335d commit 2cb9f1d

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)