Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validator excluded_if #939

Merged
merged 1 commit into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -1587,10 +1587,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 @@ -11064,50 +11064,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