Fix excluded_unless.

Value check was happening when the value was present ("if"), not absent ("unless").
Tests were cheated to pass this behaviour.
pull/959/head
Gomez Ramos, Eneko 2 years ago
parent 9e2ea40380
commit 0e62c17e43
  1. 2
      baked_in.go
  2. 10
      validator_test.go

@ -1617,7 +1617,7 @@ func excludedUnless(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad param number for excluded_unless %s", fl.FieldName())) panic(fmt.Sprintf("Bad param number for excluded_unless %s", fl.FieldName()))
} }
for i := 0; i < len(params); i += 2 { for i := 0; i < len(params); i += 2 {
if !requireCheckFieldValue(fl, params[i], params[i+1], false) { if requireCheckFieldValue(fl, params[i], params[i+1], false) {
return true return true
} }
} }

@ -11137,7 +11137,7 @@ func TestExcludedUnless(t *testing.T) {
FieldE string `validate:"omitempty" json:"field_e"` FieldE string `validate:"omitempty" json:"field_e"`
FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"` FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"`
}{ }{
FieldE: "notest", FieldE: "test",
FieldER: "filled", FieldER: "filled",
} }
errs := validate.Struct(test) errs := validate.Struct(test)
@ -11147,7 +11147,7 @@ func TestExcludedUnless(t *testing.T) {
FieldE string `validate:"omitempty" json:"field_e"` FieldE string `validate:"omitempty" json:"field_e"`
FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"` FieldER string `validate:"excluded_unless=FieldE test" json:"field_er"`
}{ }{
FieldE: "test", FieldE: "notest",
FieldER: "filled", FieldER: "filled",
} }
errs = validate.Struct(test2) errs = validate.Struct(test2)
@ -11156,7 +11156,7 @@ func TestExcludedUnless(t *testing.T) {
Equal(t, len(ve), 1) Equal(t, len(ve), 1)
AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_unless") AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_unless")
shouldError := "test" shouldError := "notest"
test3 := struct { test3 := struct {
Inner *Inner Inner *Inner
Field1 string `validate:"excluded_unless=Inner.Field test" json:"field_1"` Field1 string `validate:"excluded_unless=Inner.Field test" json:"field_1"`
@ -11170,7 +11170,7 @@ func TestExcludedUnless(t *testing.T) {
Equal(t, len(ve), 1) Equal(t, len(ve), 1)
AssertError(t, errs, "Field1", "Field1", "Field1", "Field1", "excluded_unless") AssertError(t, errs, "Field1", "Field1", "Field1", "Field1", "excluded_unless")
shouldPass := "shouldPass" shouldPass := "test"
test4 := struct { test4 := struct {
Inner *Inner Inner *Inner
FieldE string `validate:"omitempty" json:"field_e"` FieldE string `validate:"omitempty" json:"field_e"`
@ -12285,4 +12285,4 @@ func TestMultiOrOperatorGroup(t *testing.T) {
} }
} }
} }
} }

Loading…
Cancel
Save