Skip to content

Commit

Permalink
Fix validator excluded_if (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinClabaut committed Apr 29, 2023
1 parent adcf335 commit b7df869
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
4 changes: 2 additions & 2 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -1729,10 +1729,10 @@ func excludedIf(fl FieldLevel) bool {

for i := 0; i < len(params); i += 2 {
if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
return false
return true
}
}
return true
return !hasValue(fl)
}

// requiredUnless is the validation function
Expand Down
96 changes: 83 additions & 13 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11501,50 +11501,120 @@ func TestExcludedIf(t *testing.T) {
Field *string
}

shouldExclude := "exclude"
shouldNotExclude := "dontExclude"

test1 := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldER *string `validate:"excluded_if=FieldE test" json:"field_er"`
FieldER *string `validate:"excluded_if=FieldE exclude" json:"field_er"`
}{
FieldE: "test",
FieldE: shouldExclude,
}
errs := validate.Struct(test1)
Equal(t, errs, nil)

test2 := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldER string `validate:"excluded_if=FieldE test" json:"field_er"`
FieldER string `validate:"excluded_if=FieldE exclude" json:"field_er"`
}{
FieldE: "notest",
FieldE: shouldExclude,
FieldER: "set",
}
errs = validate.Struct(test2)
NotEqual(t, errs, nil)
ve := errs.(ValidationErrors)
Equal(t, len(ve), 1)
AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_if")

shouldError := "shouldError"
test3 := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldF string `validate:"omitempty" json:"field_f"`
FieldER string `validate:"excluded_if=FieldE exclude FieldF exclude" json:"field_er"`
}{
FieldE: shouldExclude,
FieldF: shouldExclude,
FieldER: "set",
}
errs = validate.Struct(test3)
NotEqual(t, errs, nil)
ve = errs.(ValidationErrors)
Equal(t, len(ve), 1)
AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_if")

test4 := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldF string `validate:"omitempty" json:"field_f"`
FieldER string `validate:"excluded_if=FieldE exclude FieldF exclude" json:"field_er"`
}{
FieldE: shouldExclude,
FieldF: shouldNotExclude,
FieldER: "set",
}
errs = validate.Struct(test4)
Equal(t, errs, nil)

test5 := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldER string `validate:"excluded_if=FieldE exclude" json:"field_er"`
}{
FieldE: shouldNotExclude,
}
errs = validate.Struct(test5)
Equal(t, errs, nil)

test6 := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldER string `validate:"excluded_if=FieldE exclude" json:"field_er"`
}{
FieldE: shouldNotExclude,
FieldER: "set",
}
errs = validate.Struct(test6)
Equal(t, errs, nil)

test7 := struct {
Inner *Inner
FieldE string `validate:"omitempty" json:"field_e"`
Field1 int `validate:"excluded_if=Inner.Field test" json:"field_1"`
Field1 int `validate:"excluded_if=Inner.Field exclude" json:"field_1"`
}{
Inner: &Inner{Field: &shouldError},
Inner: &Inner{Field: &shouldExclude},
}
errs = validate.Struct(test3)
errs = validate.Struct(test7)
Equal(t, errs, nil)

test8 := struct {
Inner *Inner
FieldE string `validate:"omitempty" json:"field_e"`
Field1 int `validate:"excluded_if=Inner.Field exclude" json:"field_1"`
}{
Inner: &Inner{Field: &shouldExclude},
Field1: 1,
}
errs = validate.Struct(test8)
NotEqual(t, errs, nil)
ve = errs.(ValidationErrors)
Equal(t, len(ve), 1)
AssertError(t, errs, "Field1", "Field1", "Field1", "Field1", "excluded_if")

shouldPass := "test"
test4 := struct {
test9 := struct {
Inner *Inner
FieldE string `validate:"omitempty" json:"field_e"`
Field1 int `validate:"excluded_if=Inner.Field test" json:"field_1"`
Field1 int `validate:"excluded_if=Inner.Field exclude" json:"field_1"`
}{
Inner: &Inner{Field: &shouldPass},
Inner: &Inner{Field: &shouldNotExclude},
}
errs = validate.Struct(test4)
errs = validate.Struct(test9)
Equal(t, errs, nil)

test10 := struct {
Inner *Inner
FieldE string `validate:"omitempty" json:"field_e"`
Field1 int `validate:"excluded_if=Inner.Field exclude" json:"field_1"`
}{
Inner: &Inner{Field: &shouldNotExclude},
Field1: 1,
}
errs = validate.Struct(test10)
Equal(t, errs, nil)

// Checks number of params in struct tag is correct
Expand Down

0 comments on commit b7df869

Please sign in to comment.