Merge pull request #469 from mashmooli/v9

Add `required_with`, `required_with_all`, `required_without`, `required_without_all` validators
pull/480/head
Dean Karn 6 years ago committed by GitHub
commit 56b7caa399
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 89
      baked_in.go
  2. 62
      doc.go
  3. 142
      validator_test.go

@ -63,6 +63,10 @@ var (
// or even disregard and use your own map if so desired. // or even disregard and use your own map if so desired.
bakedInValidators = map[string]Func{ bakedInValidators = map[string]Func{
"required": hasValue, "required": hasValue,
"required_with": requiredWith,
"required_with_all": requiredWithAll,
"required_without": requiredWithout,
"required_without_all": requiredWithoutAll,
"isdefault": isDefault, "isdefault": isDefault,
"len": hasLengthOf, "len": hasLengthOf,
"min": hasMinOf, "min": hasMinOf,
@ -1297,22 +1301,101 @@ func isDefault(fl FieldLevel) bool {
// HasValue is the validation function for validating if the current field's value is not the default static value. // HasValue is the validation function for validating if the current field's value is not the default static value.
func hasValue(fl FieldLevel) bool { func hasValue(fl FieldLevel) bool {
return requireCheckFieldKind(fl, "")
}
// requireCheckField is a func for check field kind
func requireCheckFieldKind(fl FieldLevel, param string) bool {
field := fl.Field() field := fl.Field()
if len(param) > 0 {
field = fl.Parent().FieldByName(param)
}
switch field.Kind() { switch field.Kind() {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func: case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
return !field.IsNil() return !field.IsNil()
default: default:
if fl.(*validate).fldIsPointer && field.Interface() != nil { if fl.(*validate).fldIsPointer && field.Interface() != nil {
return true return true
} }
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface() return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
} }
} }
// RequiredWith is the validation function
// The field under validation must be present and not empty only if any of the other specified fields are present.
func requiredWith(fl FieldLevel) bool {
params := parseOneOfParam2(fl.Param())
for _, param := range params {
if requireCheckFieldKind(fl, param) {
return requireCheckFieldKind(fl, "")
}
}
return true
}
// RequiredWithAll is the validation function
// The field under validation must be present and not empty only if all of the other specified fields are present.
func requiredWithAll(fl FieldLevel) bool {
isValidateCurrentField := true
params := parseOneOfParam2(fl.Param())
for _, param := range params {
if !requireCheckFieldKind(fl, param) {
isValidateCurrentField = false
}
}
if isValidateCurrentField {
return requireCheckFieldKind(fl, "")
}
return true
}
// RequiredWithout is the validation function
// The field under validation must be present and not empty only when any of the other specified fields are not present.
func requiredWithout(fl FieldLevel) bool {
isValidateCurrentField := false
params := parseOneOfParam2(fl.Param())
for _, param := range params {
if requireCheckFieldKind(fl, param) {
isValidateCurrentField = true
}
}
if !isValidateCurrentField {
return requireCheckFieldKind(fl, "")
}
return true
}
// RequiredWithoutAll is the validation function
// The field under validation must be present and not empty only when all of the other specified fields are not present.
func requiredWithoutAll(fl FieldLevel) bool {
isValidateCurrentField := true
params := parseOneOfParam2(fl.Param())
for _, param := range params {
if requireCheckFieldKind(fl, param) {
isValidateCurrentField = false
}
}
if isValidateCurrentField {
return requireCheckFieldKind(fl, "")
}
return true
}
// IsGteField is the validation function for validating if the current field's value is greater than or equal to the field specified by the param's value. // IsGteField is the validation function for validating if the current field's value is greater than or equal to the field specified by the param's value.
func isGteField(fl FieldLevel) bool { func isGteField(fl FieldLevel) bool {

@ -245,6 +245,68 @@ ensures the value is not nil.
Usage: required Usage: required
Required With
The field under validation must be present and not empty only if any
of the other specified fields are present. For strings ensures value is
not "". For slices, maps, pointers, interfaces, channels and functions
ensures the value is not nil.
Usage: required_with
Examples:
// require the field if the Field1 is present:
Usage: required_with=Field1
// require the field if the Field1 or Field2 is present:
Usage: required_with=Field1 Field2
Required With All
The field under validation must be present and not empty only if all
of the other specified fields are present. For strings ensures value is
not "". For slices, maps, pointers, interfaces, channels and functions
ensures the value is not nil.
Usage: required_with_all
Example:
// require the field if the Field1 and Field2 is present:
Usage: required_with_all=Field1 Field2
Required Without
The field under validation must be present and not empty only when any
of the other specified fields are not present. For strings ensures value is
not "". For slices, maps, pointers, interfaces, channels and functions
ensures the value is not nil.
Usage: required_without
Examples:
// require the field if the Field1 is not present:
Usage: required_without=Field1
// require the field if the Field1 or Field2 is not present:
Usage: required_without=Field1 Field2
Required Without All
The field under validation must be present and not empty only when all
of the other specified fields are not present. For strings ensures value is
not "". For slices, maps, pointers, interfaces, channels and functions
ensures the value is not nil.
Usage: required_without_all
Example:
// require the field if the Field1 and Field2 is not present:
Usage: required_without_all=Field1 Field2
Is Default Is Default
This validates that the value is the default value and is almost the This validates that the value is the default value and is almost the

@ -8620,4 +8620,146 @@ func TestEndsWithValidation(t *testing.T) {
} }
} }
func TestRequiredWith(t *testing.T) {
fieldVal := "test"
test := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldER string `validate:"required_with=FieldE" json:"field_er"`
Field1 string `validate:"omitempty" json:"field_1"`
Field2 *string `validate:"required_with=Field1" json:"field_2"`
Field3 map[string]string `validate:"required_with=Field2" json:"field_3"`
Field4 interface{} `validate:"required_with=Field3" json:"field_4"`
Field5 string `validate:"required_with=Field3" json:"field_5"`
}{
Field1: "test_field1",
Field2: &fieldVal,
Field3: map[string]string{"key": "val"},
Field4: "test",
Field5: "test",
}
validate := New()
errs := validate.Struct(test)
if errs != nil {
t.Fatalf("failed Error: %s", errs)
}
}
func TestRequiredWithAll(t *testing.T) {
fieldVal := "test"
test := struct {
FieldE string `validate:"omitempty" json:"field_e"`
FieldER string `validate:"required_with_all=FieldE" json:"field_er"`
Field1 string `validate:"omitempty" json:"field_1"`
Field2 *string `validate:"required_with_all=Field1" json:"field_2"`
Field3 map[string]string `validate:"required_with_all=Field2" json:"field_3"`
Field4 interface{} `validate:"required_with_all=Field3" json:"field_4"`
Field5 string `validate:"required_with_all=Field3" json:"field_5"`
}{
Field1: "test_field1",
Field2: &fieldVal,
Field3: map[string]string{"key": "val"},
Field4: "test",
Field5: "test",
}
validate := New()
errs := validate.Struct(test)
if errs != nil {
t.Fatalf("failed Error: %s", errs)
}
}
func TestRequiredWithout(t *testing.T) {
fieldVal := "test"
test := struct {
Field1 string `validate:"omitempty" json:"field_1"`
Field2 *string `validate:"required_without=Field1" json:"field_2"`
Field3 map[string]string `validate:"required_without=Field2" json:"field_3"`
Field4 interface{} `validate:"required_without=Field3" json:"field_4"`
Field5 string `validate:"required_without=Field3" json:"field_5"`
}{
Field2: &fieldVal,
Field3: map[string]string{"key": "val"},
Field4: "test",
Field5: "test",
}
validate := New()
errs := validate.Struct(test)
if errs != nil {
t.Fatalf("failed Error: %s", errs)
}
test2 := struct {
Field1 string `validate:"omitempty" json:"field_1"`
Field2 *string `validate:"required_without=Field1" json:"field_2"`
Field3 map[string]string `validate:"required_without=Field2" json:"field_3"`
Field4 interface{} `validate:"required_without=Field3" json:"field_4"`
Field5 string `validate:"required_without=Field3" json:"field_5"`
Field6 string `validate:"required_without=Field1" json:"field_6"`
}{
Field3: map[string]string{"key": "val"},
Field4: "test",
Field5: "test",
}
errs = validate.Struct(test2)
if errs == nil {
t.Fatalf("failed Error: %s", errs)
}
}
func TestRequiredWithoutAll(t *testing.T) {
fieldVal := "test"
test := struct {
Field1 string `validate:"omitempty" json:"field_1"`
Field2 *string `validate:"required_without_all=Field1" json:"field_2"`
Field3 map[string]string `validate:"required_without_all=Field2" json:"field_3"`
Field4 interface{} `validate:"required_without_all=Field3" json:"field_4"`
Field5 string `validate:"required_without_all=Field3" json:"field_5"`
}{
Field1: "",
Field2: &fieldVal,
Field3: map[string]string{"key": "val"},
Field4: "test",
Field5: "test",
}
validate := New()
errs := validate.Struct(test)
if errs != nil {
t.Fatalf("failed Error: %s", errs)
}
test2 := struct {
Field1 string `validate:"omitempty" json:"field_1"`
Field2 *string `validate:"required_without_all=Field1" json:"field_2"`
Field3 map[string]string `validate:"required_without_all=Field2" json:"field_3"`
Field4 interface{} `validate:"required_without_all=Field3" json:"field_4"`
Field5 string `validate:"required_without_all=Field3" json:"field_5"`
Field6 string `validate:"required_without_all=Field1" json:"field_6"`
}{
Field3: map[string]string{"key": "val"},
Field4: "test",
Field5: "test",
}
errs = validate.Struct(test2)
if errs == nil {
t.Fatalf("failed Error: %s", errs)
}
}

Loading…
Cancel
Save