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

Add support for excluded_with on pointer fields #746

Merged
merged 2 commits into from Apr 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion validator_instance.go
Expand Up @@ -29,6 +29,10 @@ const (
requiredWithAllTag = "required_with_all"
requiredIfTag = "required_if"
requiredUnlessTag = "required_unless"
excludedWithoutAllTag = "excluded_without_all"
excludedWithoutTag = "excluded_without"
excludedWithTag = "excluded_with"
excludedWithAllTag = "excluded_with_all"
skipValidationTag = "-"
diveTag = "dive"
keysTag = "keys"
Expand Down Expand Up @@ -111,7 +115,8 @@ func New() *Validate {

switch k {
// these require that even if the value is nil that the validation should run, omitempty still overrides this behaviour
case requiredIfTag, requiredUnlessTag, requiredWithTag, requiredWithAllTag, requiredWithoutTag, requiredWithoutAllTag:
case requiredIfTag, requiredUnlessTag, requiredWithTag, requiredWithAllTag, requiredWithoutTag, requiredWithoutAllTag,
excludedWithTag, excludedWithAllTag, excludedWithoutTag, excludedWithoutAllTag:
_ = v.registerValidation(k, wrapFunc(val), true, true)
default:
// no need to error check here, baked in will always be valid
Expand Down
90 changes: 89 additions & 1 deletion validator_test.go
Expand Up @@ -10202,6 +10202,28 @@ func TestExcludedWith(t *testing.T) {
name := fmt.Sprintf("Field%d", i)
AssertError(t, errs, name, name, name, name, "excluded_with")
}

test3 := struct {
Inner *Inner
Inner2 *Inner
Field string `validate:"omitempty" json:"field"`
FieldE string `validate:"omitempty" json:"field_e"`
Field1 string `validate:"excluded_with=FieldE" json:"field_1"`
Field2 *string `validate:"excluded_with=FieldE" json:"field_2"`
Field3 map[string]string `validate:"excluded_with=FieldE" json:"field_3"`
Field4 interface{} `validate:"excluded_with=FieldE" json:"field_4"`
Field5 string `validate:"excluded_with=Inner.FieldE" json:"field_5"`
Field6 string `validate:"excluded_with=Inner2.FieldE" json:"field_6"`
}{
Inner: &Inner{FieldE: "populated"},
Inner2: &Inner{FieldE: "populated"},
FieldE: "populated",
}

validate = New()

errs = validate.Struct(test3)
Equal(t, errs, nil)
}

func TestExcludedWithout(t *testing.T) {
Expand Down Expand Up @@ -10266,6 +10288,26 @@ func TestExcludedWithout(t *testing.T) {
name := fmt.Sprintf("Field%d", i)
AssertError(t, errs, name, name, name, name, "excluded_without")
}

test3 := struct {
Inner *Inner
Inner2 *Inner
Field string `validate:"omitempty" json:"field"`
FieldE string `validate:"omitempty" json:"field_e"`
Field1 string `validate:"excluded_without=Field" json:"field_1"`
Field2 *string `validate:"excluded_without=Field" json:"field_2"`
Field3 map[string]string `validate:"excluded_without=Field" json:"field_3"`
Field4 interface{} `validate:"excluded_without=Field" json:"field_4"`
Field5 string `validate:"excluded_without=Inner.Field" json:"field_5"`
}{
Inner: &Inner{Field: &fieldVal},
Field: "populated",
}

validate = New()

errs = validate.Struct(test3)
Equal(t, errs, nil)
}

func TestExcludedWithAll(t *testing.T) {
Expand Down Expand Up @@ -10334,6 +10376,29 @@ func TestExcludedWithAll(t *testing.T) {
name := fmt.Sprintf("Field%d", i)
AssertError(t, errs, name, name, name, name, "excluded_with_all")
}

test3 := struct {
Inner *Inner
Inner2 *Inner
Field string `validate:"omitempty" json:"field"`
FieldE string `validate:"omitempty" json:"field_e"`
Field1 string `validate:"excluded_with_all=FieldE Field" json:"field_1"`
Field2 *string `validate:"excluded_with_all=FieldE Field" json:"field_2"`
Field3 map[string]string `validate:"excluded_with_all=FieldE Field" json:"field_3"`
Field4 interface{} `validate:"excluded_with_all=FieldE Field" json:"field_4"`
Field5 string `validate:"excluded_with_all=Inner.FieldE" json:"field_5"`
Field6 string `validate:"excluded_with_all=Inner2.FieldE" json:"field_6"`
}{
Inner: &Inner{FieldE: "populated"},
Inner2: &Inner{FieldE: "populated"},
Field: "populated",
FieldE: "populated",
}

validate = New()

errs = validate.Struct(test3)
Equal(t, errs, nil)
}

func TestExcludedWithoutAll(t *testing.T) {
Expand All @@ -10352,9 +10417,10 @@ func TestExcludedWithoutAll(t *testing.T) {
Field2 *string `validate:"excluded_without_all=Field FieldE" json:"field_2"`
Field3 map[string]string `validate:"excluded_without_all=Field FieldE" json:"field_3"`
Field4 interface{} `validate:"excluded_without_all=Field FieldE" json:"field_4"`
Field5 string `validate:"excluded_without_all=Inner.Field Inner.Field2" json:"field_5"`
Field5 string `validate:"excluded_without_all=Inner.Field Inner2.Field" json:"field_5"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed like a typo

}{
Inner: &Inner{Field: &fieldVal},
Inner2: &Inner{Field: &fieldVal},
Field: "populated",
Field1: fieldVal,
Field2: &fieldVal,
Expand Down Expand Up @@ -10398,6 +10464,28 @@ func TestExcludedWithoutAll(t *testing.T) {
name := fmt.Sprintf("Field%d", i)
AssertError(t, errs, name, name, name, name, "excluded_without_all")
}

test3 := struct {
Inner *Inner
Inner2 *Inner
Field string `validate:"omitempty" json:"field"`
FieldE string `validate:"omitempty" json:"field_e"`
Field1 string `validate:"excluded_without_all=Field FieldE" json:"field_1"`
Field2 *string `validate:"excluded_without_all=Field FieldE" json:"field_2"`
Field3 map[string]string `validate:"excluded_without_all=Field FieldE" json:"field_3"`
Field4 interface{} `validate:"excluded_without_all=Field FieldE" json:"field_4"`
Field5 string `validate:"excluded_without_all=Inner.Field Inner2.Field" json:"field_5"`
}{
Inner: &Inner{Field: &fieldVal},
Inner2: &Inner{Field: &fieldVal},
Field: "populated",
FieldE: "populated",
}

validate = New()

errs = validate.Struct(test3)
Equal(t, errs, nil)
}

func TestRequiredWithAll(t *testing.T) {
Expand Down