fix validator excluded_unless (#1064)

pull/1081/head
Arthur Jenoudet 2 years ago committed by GitHub
parent f3086daf63
commit ef342b6f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      baked_in.go
  2. 47
      validator_test.go

@ -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

@ -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)
@ -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)
@ -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"`
@ -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"`
@ -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 {

Loading…
Cancel
Save