Skip to content

Commit 0066799

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

File tree

2 files changed

+72
-36
lines changed

2 files changed

+72
-36
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: 68 additions & 33 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, "Field2", "Field2", "Field2", "Field2", "required_unless")
1247712512
}
1247812513

1247912514
func TestLookup(t *testing.T) {
@@ -13374,39 +13409,39 @@ func TestBicIso2022FormatValidation(t *testing.T) {
1337413409
{"SBICKENXX9", "bic", false},
1337513410
{"SBICKEN13458", "bic", false},
1337613411
{"SBICKEN", "bic", false},
13377-
{"DEUTDEFF", "bic", true}, // 8-char classic (Germany)
13378-
{"DEUTDEFF500", "bic", true}, // 11-char with numeric branch
13379-
{"A1B2US33", "bic", true}, // digits allowed in 4!c (bank code)
13380-
{"1234US33", "bic", true}, // all digits in 4!c (2022)
13381-
{"ZZZ1USAA", "bic", true}, // mixed alnum bank + alnum location
13382-
{"AB12AE00", "bic", true}, // UAE 8-char
13383-
{"AB12AE009Z9", "bic", true}, // UAE 11-char with mixed branch
13384-
{"WG11US335AB", "bic", true}, // example-style with digits in branch
13385-
{"BNPAFRPP", "bic", true}, // France (BNP Paribas style)
13386-
{"BOFAUS3NXXX", "bic", true}, // US with default XXX branch
13387-
{"HSBCHKHHXXX", "bic", true}, // Hong Kong, default branch
13388-
{"NEDSZAJJ", "bic", true}, // South Africa 8-char
13389-
{"BARCGB22", "bic", true}, // GB 8-char
13390-
{"BARCGB22XXX", "bic", true}, // GB 11-char with XXX branch
13391-
{"0000GB00", "bic", true}, // 4!c all digits + 2!c all digits (allowed)
13392-
{"A1B2GB00XXX", "bic", true}, // valid 11-char with numeric location and XXX
13393-
{"TATRAEBX", "bic", true}, // UAE 8-char
13394-
{"TATRSABX", "bic", true}, // Saudi 8-char
13395-
{"TATREGBX", "bic", true}, // Egypt 8-char
13396-
{"TATRBHBX", "bic", true}, // Bahrain 8-char
13397-
13398-
{"DEUTDEFFF", "bic", false}, // 9-char (invalid length)
13399-
{"DEUTDEFF5", "bic", false}, // 9-char (invalid length)
13400-
{"DEUTDE", "bic", false}, // 6-char (invalid length)
13401-
{"DEUTDEFF50", "bic", false}, // 10-char (invalid length)
13402-
{"DEUTDEFF5000", "bic", false}, // 12-char (invalid length)
13403-
{"deUTDEFF", "bic", false}, // lowercase not allowed
13404-
{"DEUTDEfF", "bic", false}, // lowercase in location
13405-
{"DEU@DEFF", "bic", false}, // special char in bank
13406-
{"ABCD12FF", "bic", false}, // digits in 2!a country (invalid)
13407-
{"ABCDDE1-", "bic", false}, // hyphen in location
13408-
{"ABCDDE1_", "bic", false}, // underscore in location
13409-
{"ABCDDE١٢", "bic", false}, // non-ASCII digits in location
13412+
{"DEUTDEFF", "bic", true}, // 8-char classic (Germany)
13413+
{"DEUTDEFF500", "bic", true}, // 11-char with numeric branch
13414+
{"A1B2US33", "bic", true}, // digits allowed in 4!c (bank code)
13415+
{"1234US33", "bic", true}, // all digits in 4!c (2022)
13416+
{"ZZZ1USAA", "bic", true}, // mixed alnum bank + alnum location
13417+
{"AB12AE00", "bic", true}, // UAE 8-char
13418+
{"AB12AE009Z9", "bic", true}, // UAE 11-char with mixed branch
13419+
{"WG11US335AB", "bic", true}, // example-style with digits in branch
13420+
{"BNPAFRPP", "bic", true}, // France (BNP Paribas style)
13421+
{"BOFAUS3NXXX", "bic", true}, // US with default XXX branch
13422+
{"HSBCHKHHXXX", "bic", true}, // Hong Kong, default branch
13423+
{"NEDSZAJJ", "bic", true}, // South Africa 8-char
13424+
{"BARCGB22", "bic", true}, // GB 8-char
13425+
{"BARCGB22XXX", "bic", true}, // GB 11-char with XXX branch
13426+
{"0000GB00", "bic", true}, // 4!c all digits + 2!c all digits (allowed)
13427+
{"A1B2GB00XXX", "bic", true}, // valid 11-char with numeric location and XXX
13428+
{"TATRAEBX", "bic", true}, // UAE 8-char
13429+
{"TATRSABX", "bic", true}, // Saudi 8-char
13430+
{"TATREGBX", "bic", true}, // Egypt 8-char
13431+
{"TATRBHBX", "bic", true}, // Bahrain 8-char
13432+
13433+
{"DEUTDEFFF", "bic", false}, // 9-char (invalid length)
13434+
{"DEUTDEFF5", "bic", false}, // 9-char (invalid length)
13435+
{"DEUTDE", "bic", false}, // 6-char (invalid length)
13436+
{"DEUTDEFF50", "bic", false}, // 10-char (invalid length)
13437+
{"DEUTDEFF5000", "bic", false}, // 12-char (invalid length)
13438+
{"deUTDEFF", "bic", false}, // lowercase not allowed
13439+
{"DEUTDEfF", "bic", false}, // lowercase in location
13440+
{"DEU@DEFF", "bic", false}, // special char in bank
13441+
{"ABCD12FF", "bic", false}, // digits in 2!a country (invalid)
13442+
{"ABCDDE1-", "bic", false}, // hyphen in location
13443+
{"ABCDDE1_", "bic", false}, // underscore in location
13444+
{"ABCDDE١٢", "bic", false}, // non-ASCII digits in location
1341013445
}
1341113446

1341213447
validate := New()

0 commit comments

Comments
 (0)