#908|Add Support of SkipUnless

pull/976/head
t0250361 3 years ago
parent 9e2ea40380
commit c12e80a0b5
  1. 17
      baked_in.go
  2. 4
      validator_instance.go

@ -71,6 +71,7 @@ var (
"required": hasValue, "required": hasValue,
"required_if": requiredIf, "required_if": requiredIf,
"required_unless": requiredUnless, "required_unless": requiredUnless,
"skip_unless": skipUnless,
"required_with": requiredWith, "required_with": requiredWith,
"required_with_all": requiredWithAll, "required_with_all": requiredWithAll,
"required_without": requiredWithout, "required_without": requiredWithout,
@ -1609,6 +1610,22 @@ func requiredUnless(fl FieldLevel) bool {
return hasValue(fl) return hasValue(fl)
} }
// skipUnless is the validation function
// The field under validation must be present and not empty only unless all the other specified fields are equal to the value following with the specified field.
func skipUnless(fl FieldLevel) bool {
params := parseOneOfParam2(fl.Param())
if len(params)%2 != 0 {
panic(fmt.Sprintf("Bad param number for skip_unless %s", fl.FieldName()))
}
for i := 0; i < len(params); i += 2 {
if requireCheckFieldValue(fl, params[i], params[i+1], false) {
return true
}
}
return hasValue(fl)
}
// excludedUnless is the validation function // excludedUnless is the validation function
// The field under validation must not be present or is empty unless all the other specified fields are equal to the value following with the specified field. // The field under validation must not be present or is empty unless all the other specified fields are equal to the value following with the specified field.
func excludedUnless(fl FieldLevel) bool { func excludedUnless(fl FieldLevel) bool {

@ -29,6 +29,7 @@ const (
requiredWithAllTag = "required_with_all" requiredWithAllTag = "required_with_all"
requiredIfTag = "required_if" requiredIfTag = "required_if"
requiredUnlessTag = "required_unless" requiredUnlessTag = "required_unless"
skipUnlessTag = "skip_unless"
excludedWithoutAllTag = "excluded_without_all" excludedWithoutAllTag = "excluded_without_all"
excludedWithoutTag = "excluded_without" excludedWithoutTag = "excluded_without"
excludedWithTag = "excluded_with" excludedWithTag = "excluded_with"
@ -123,7 +124,8 @@ func New() *Validate {
switch k { switch k {
// these require that even if the value is nil that the validation should run, omitempty still overrides this behaviour // 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,
excludedIfTag, excludedUnlessTag, excludedWithTag, excludedWithAllTag, excludedWithoutTag, excludedWithoutAllTag: excludedIfTag, excludedUnlessTag, excludedWithTag, excludedWithAllTag, excludedWithoutTag, excludedWithoutAllTag,
skipUnlessTag:
_ = v.registerValidation(k, wrapFunc(val), true, true) _ = v.registerValidation(k, wrapFunc(val), true, true)
default: default:
// no need to error check here, baked in will always be valid // no need to error check here, baked in will always be valid

Loading…
Cancel
Save