Fix validator excluded_if

pull/939/head
CorentinClabaut 3 years ago
parent 9e2ea40380
commit 145ebc8f6a
  1. 4
      baked_in.go
  2. 96
      validator_test.go

@ -1587,10 +1587,10 @@ func excludedIf(fl FieldLevel) bool {
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 false return true
} }
} }
return true return !hasValue(fl)
} }
// requiredUnless is the validation function // requiredUnless is the validation function

@ -11064,20 +11064,24 @@ func TestExcludedIf(t *testing.T) {
Field *string Field *string
} }
shouldExclude := "exclude"
shouldNotExclude := "dontExclude"
test1 := struct { test1 := struct {
FieldE string `validate:"omitempty" json:"field_e"` 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) errs := validate.Struct(test1)
Equal(t, errs, nil) Equal(t, errs, nil)
test2 := struct { test2 := struct {
FieldE string `validate:"omitempty" json:"field_e"` 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) errs = validate.Struct(test2)
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
@ -11085,31 +11089,97 @@ func TestExcludedIf(t *testing.T) {
Equal(t, len(ve), 1) Equal(t, len(ve), 1)
AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_if") AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_if")
shouldError := "shouldError"
test3 := struct { test3 := struct {
Inner *Inner
FieldE string `validate:"omitempty" json:"field_e"` FieldE string `validate:"omitempty" json:"field_e"`
Field1 int `validate:"excluded_if=Inner.Field test" json:"field_1"` FieldF string `validate:"omitempty" json:"field_f"`
FieldER string `validate:"excluded_if=FieldE exclude FieldF exclude" json:"field_er"`
}{ }{
Inner: &Inner{Field: &shouldError}, FieldE: shouldExclude,
FieldF: shouldExclude,
FieldER: "set",
} }
errs = validate.Struct(test3) errs = validate.Struct(test3)
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
ve = errs.(ValidationErrors) ve = errs.(ValidationErrors)
Equal(t, len(ve), 1) Equal(t, len(ve), 1)
AssertError(t, errs, "Field1", "Field1", "Field1", "Field1", "excluded_if") AssertError(t, errs, "FieldER", "FieldER", "FieldER", "FieldER", "excluded_if")
shouldPass := "test"
test4 := struct { test4 := struct {
Inner *Inner
FieldE string `validate:"omitempty" json:"field_e"` FieldE string `validate:"omitempty" json:"field_e"`
Field1 int `validate:"excluded_if=Inner.Field test" json:"field_1"` FieldF string `validate:"omitempty" json:"field_f"`
FieldER string `validate:"excluded_if=FieldE exclude FieldF exclude" json:"field_er"`
}{ }{
Inner: &Inner{Field: &shouldPass}, FieldE: shouldExclude,
FieldF: shouldNotExclude,
FieldER: "set",
} }
errs = validate.Struct(test4) errs = validate.Struct(test4)
Equal(t, errs, nil) 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 exclude" json:"field_1"`
}{
Inner: &Inner{Field: &shouldExclude},
}
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")
test9 := 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},
}
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 // Checks number of params in struct tag is correct
defer func() { defer func() {
if r := recover(); r == nil { if r := recover(); r == nil {

Loading…
Cancel
Save