diff --git a/baked_in.go b/baked_in.go index e08442b..eda5814 100644 --- a/baked_in.go +++ b/baked_in.go @@ -1652,10 +1652,10 @@ func excludedUnless(fl FieldLevel) bool { } for i := 0; i < len(params); i += 2 { if !requireCheckFieldValue(fl, params[i], params[i+1], false) { - return true + return !hasValue(fl) } } - return !hasValue(fl) + return true } // excludedWith is the validation function diff --git a/validator_test.go b/validator_test.go index 9ffcd9c..0f43db8 100644 --- a/validator_test.go +++ b/validator_test.go @@ -11259,7 +11259,7 @@ func TestExcludedUnless(t *testing.T) { FieldE string `validate:"omitempty" json:"field_e"` FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"` }{ - FieldE: "notest", + FieldE: "test", FieldER: "filled", } errs := validate.Struct(test) @@ -11269,7 +11269,7 @@ func TestExcludedUnless(t *testing.T) { FieldE string `validate:"omitempty" json:"field_e"` FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"` }{ - FieldE: "test", + FieldE: "notest", FieldER: "filled", } errs = validate.Struct(test2) @@ -11278,7 +11278,26 @@ func TestExcludedUnless(t *testing.T) { Equal(t, len(ve), 1) AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_unless") - shouldError := "test" + // test5 and test6: excluded_unless has no effect if FieldER is left blank + test5 := struct { + FieldE string `validate:"omitempty" json:"field_e"` + FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"` + }{ + FieldE: "test", + } + errs = validate.Struct(test5) + Equal(t, errs, nil) + + test6 := struct { + FieldE string `validate:"omitempty" json:"field_e"` + FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"` + }{ + FieldE: "notest", + } + errs = validate.Struct(test6) + Equal(t, errs, nil) + + shouldError := "notest" test3 := struct { Inner *Inner Field1 string `validate:"excluded_unless=Inner.Field test" json:"field_1"` @@ -11292,7 +11311,7 @@ func TestExcludedUnless(t *testing.T) { Equal(t, len(ve), 1) AssertError(t, errs, "Field1", "Field1", "Field1", "Field1", "excluded_unless") - shouldPass := "shouldPass" + shouldPass := "test" test4 := struct { Inner *Inner FieldE string `validate:"omitempty" json:"field_e"` @@ -11304,6 +11323,26 @@ func TestExcludedUnless(t *testing.T) { errs = validate.Struct(test4) Equal(t, errs, nil) + // test7 and test8: excluded_unless has no effect if FieldER is left blank + test7 := struct { + Inner *Inner + FieldE string `validate:"omitempty" json:"field_e"` + FieldER string `validate:"excluded_unless=Inner.Field test" json:"field_er"` + }{ + FieldE: "test", + } + errs = validate.Struct(test7) + Equal(t, errs, nil) + + test8 := struct { + FieldE string `validate:"omitempty" json:"field_e"` + FieldER string `validate:"excluded_unless=Inner.Field test" json:"field_er"` + }{ + FieldE: "test", + } + errs = validate.Struct(test8) + Equal(t, errs, nil) + // Checks number of params in struct tag is correct defer func() { if r := recover(); r == nil {