Skip to content

Commit ca3636f

Browse files
committed
allows multiple values of same field on excluded_uunless to have opposite equivilant result to required_unless
1 parent a947377 commit ca3636f

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

baked_in.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1905,11 +1905,12 @@ func excludedUnless(fl FieldLevel) bool {
19051905
panic(fmt.Sprintf("Bad param number for excluded_unless %s", fl.FieldName()))
19061906
}
19071907
for i := 0; i < len(params); i += 2 {
1908-
if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
1909-
return !hasValue(fl)
1908+
if requireCheckFieldValue(fl, params[i], params[i+1], false) {
1909+
return true
19101910
}
19111911
}
1912-
return true
1912+
1913+
return !hasValue(fl)
19131914
}
19141915

19151916
// excludedWith is the validation function

validator_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -11140,6 +11140,24 @@ func TestRequiredUnless(t *testing.T) {
1114011140
Inner: &Inner{Field: &fieldVal},
1114111141
}
1114211142
_ = validate.Struct(test3)
11143+
11144+
test4 := struct {
11145+
Field1 int
11146+
Field2 string `validate:"required_unless: Field1 1 Field1 2"`
11147+
Field3 int
11148+
Field4 string `validate:"required_unless: Field3 1 Field3 2"`
11149+
}{
11150+
Field1: 1,
11151+
Field3: 3,
11152+
}
11153+
11154+
errs = validate.Struct(test4)
11155+
NotEqual(t, errs, nil)
11156+
11157+
ve = errs.(ValidationErrors)
11158+
Equal(t, len(ve), 1)
11159+
11160+
AssertError(t, errs, "Field2", "Field2", "Field2", "Field2", "required_unless")
1114311161
}
1114411162

1114511163
func TestSkipUnless(t *testing.T) {
@@ -12126,6 +12144,26 @@ func TestExcludedUnless(t *testing.T) {
1212612144
Inner: &Inner{Field: &fieldVal},
1212712145
}
1212812146
_ = validate.Struct(panicTest)
12147+
12148+
test9 := struct {
12149+
Field1 int
12150+
Field2 string `validate:"excluded_unless: Field1 1 Field1 2"`
12151+
Field3 int
12152+
Field4 string `validate:"excluded_unless: Field3 1 Field3 2"`
12153+
}{
12154+
Field1: 1,
12155+
Field2: "foo",
12156+
Field3: 3,
12157+
Field4: "foo",
12158+
}
12159+
12160+
errs = validate.Struct(test9)
12161+
NotEqual(t, errs, nil)
12162+
12163+
ve = errs.(ValidationErrors)
12164+
Equal(t, len(ve), 1)
12165+
12166+
AssertError(t, errs, "Field2", "Field2", "Field2", "Field2", "required_unless")
1212912167
}
1213012168

1213112169
func TestLookup(t *testing.T) {

0 commit comments

Comments
 (0)