Skip to content

Commit 65df2bb

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

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11443,6 +11443,24 @@ 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+
ve = errs.(ValidationErrors)
11461+
Equal(t, len(ve), 1)
11462+
11463+
AssertError(t, errs, "Field2", "Field2", "Field2", "Field2", "required_unless")
1144611464
}
1144711465

1144811466
func TestSkipUnless(t *testing.T) {
@@ -12474,6 +12492,26 @@ func TestExcludedUnless(t *testing.T) {
1247412492
Inner: &Inner{Field: &fieldVal},
1247512493
}
1247612494
_ = validate.Struct(panicTest)
12495+
12496+
test9 := struct {
12497+
Field1 int
12498+
Field2 string `validate:"excluded_unless: Field1 1 Field1 2"`
12499+
Field3 int
12500+
Field4 string `validate:"excluded_unless: Field3 1 Field3 2"`
12501+
}{
12502+
Field1: 1,
12503+
Field2: "foo",
12504+
Field3: 3,
12505+
Field4: "foo",
12506+
}
12507+
12508+
errs = validate.Struct(test9)
12509+
NotEqual(t, errs, nil)
12510+
12511+
ve = errs.(ValidationErrors)
12512+
Equal(t, len(ve), 1)
12513+
12514+
AssertError(t, errs, "Field2", "Field2", "Field2", "Field2", "required_unless")
1247712515
}
1247812516

1247912517
func TestLookup(t *testing.T) {

0 commit comments

Comments
 (0)