simplify required check field kind

pull/469/head
A Mashmooli 6 years ago
parent d89801fc29
commit 8c221696bb
  1. 117
      baked_in.go

@ -1301,18 +1301,22 @@ 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()
} }
} }
@ -1321,33 +1325,11 @@ func hasValue(fl FieldLevel) bool {
// The field under validation must be present and not empty only if any of the other specified fields are present. // 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 { func requiredWith(fl FieldLevel) bool {
field := fl.Field()
params := parseOneOfParam2(fl.Param()) params := parseOneOfParam2(fl.Param())
for _, param := range params { for _, param := range params {
isParamFieldPresent := false
paramField := fl.Parent().FieldByName(param)
switch paramField.Kind() { if requireCheckFieldKind(fl, param) {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func: return requireCheckFieldKind(fl, "")
isParamFieldPresent = !paramField.IsNil()
default:
if fl.(*validate).fldIsPointer && paramField.Interface() != nil {
isParamFieldPresent = true
}
isParamFieldPresent = paramField.IsValid() && paramField.Interface() != reflect.Zero(field.Type()).Interface()
}
if isParamFieldPresent {
switch field.Kind() {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
return !field.IsNil()
default:
if fl.(*validate).fldIsPointer && field.Interface() != nil {
return true
}
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
}
} }
} }
@ -1358,38 +1340,17 @@ func requiredWith(fl FieldLevel) bool {
// The field under validation must be present and not empty only if all of the other specified fields are present. // 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 { func requiredWithAll(fl FieldLevel) bool {
field := fl.Field()
isValidateCurrentField := true isValidateCurrentField := true
params := parseOneOfParam2(fl.Param()) params := parseOneOfParam2(fl.Param())
for _, param := range params { for _, param := range params {
isParamFieldPresent := false
paramField := fl.Parent().FieldByName(param)
switch paramField.Kind() { if !requireCheckFieldKind(fl, param) {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func: isValidateCurrentField = false
isParamFieldPresent = !paramField.IsNil()
default:
if fl.(*validate).fldIsPointer && paramField.Interface() != nil {
isParamFieldPresent = true
}
isParamFieldPresent = paramField.IsValid() && paramField.Interface() != reflect.Zero(field.Type()).Interface()
}
if !isParamFieldPresent {
isValidateCurrentField = isParamFieldPresent
} }
} }
if isValidateCurrentField { if isValidateCurrentField {
switch field.Kind() { return requireCheckFieldKind(fl, "")
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
return !field.IsNil()
default:
if fl.(*validate).fldIsPointer && field.Interface() != nil {
return true
}
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
}
} }
return true return true
@ -1399,38 +1360,17 @@ func requiredWithAll(fl FieldLevel) bool {
// The field under validation must be present and not empty only when any of the other specified fields are not present. // 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 { func requiredWithout(fl FieldLevel) bool {
field := fl.Field()
isValidateCurrentField := false isValidateCurrentField := false
params := parseOneOfParam2(fl.Param()) params := parseOneOfParam2(fl.Param())
for _, param := range params { for _, param := range params {
isParamFieldPresent := false
paramField := fl.Parent().FieldByName(param)
switch paramField.Kind() { if requireCheckFieldKind(fl, param) {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func: isValidateCurrentField = true
isParamFieldPresent = !paramField.IsNil()
default:
if fl.(*validate).fldIsPointer && paramField.Interface() != nil {
isParamFieldPresent = true
}
isParamFieldPresent = paramField.IsValid() && paramField.Interface() != reflect.Zero(field.Type()).Interface()
}
if isParamFieldPresent {
isValidateCurrentField = isParamFieldPresent
} }
} }
if !isValidateCurrentField { if !isValidateCurrentField {
switch field.Kind() { return requireCheckFieldKind(fl, "")
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
return !field.IsNil()
default:
if fl.(*validate).fldIsPointer && field.Interface() != nil {
return true
}
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
}
} }
return true return true
@ -1440,38 +1380,17 @@ func requiredWithout(fl FieldLevel) bool {
// The field under validation must be present and not empty only when all of the other specified fields are not present. // 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 { func requiredWithoutAll(fl FieldLevel) bool {
field := fl.Field()
isValidateCurrentField := true isValidateCurrentField := true
params := parseOneOfParam2(fl.Param()) params := parseOneOfParam2(fl.Param())
for _, param := range params { for _, param := range params {
isParamFieldPresent := false
paramField := fl.Parent().FieldByName(param) if requireCheckFieldKind(fl, param) {
isValidateCurrentField = false
switch paramField.Kind() {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
isParamFieldPresent = !paramField.IsNil()
default:
if fl.(*validate).fldIsPointer && paramField.Interface() != nil {
isParamFieldPresent = true
}
isParamFieldPresent = paramField.IsValid() && paramField.Interface() != reflect.Zero(field.Type()).Interface()
}
if isParamFieldPresent {
isValidateCurrentField = !isParamFieldPresent
} }
} }
if isValidateCurrentField { if isValidateCurrentField {
switch field.Kind() { return requireCheckFieldKind(fl, "")
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
return !field.IsNil()
default:
if fl.(*validate).fldIsPointer && field.Interface() != nil {
return true
}
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
}
} }
return true return true

Loading…
Cancel
Save