Skip to content

Commit

Permalink
fix validator excluded_unless (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenoudet committed Mar 19, 2023
1 parent f3086da commit ef342b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions baked_in.go
Expand Up @@ -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
Expand Down
47 changes: 43 additions & 4 deletions validator_test.go
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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 {
Expand Down

0 comments on commit ef342b6

Please sign in to comment.