Adding Support for SkipUnless validator and testing the same

pull/976/head
Jahanvi Aggarwal 2 years ago
parent 8cf4f8c311
commit dd3b594f69
  1. 22
      baked_in.go

@ -1610,35 +1610,35 @@ func requiredUnless(fl FieldLevel) bool {
return hasValue(fl) return hasValue(fl)
} }
// skipUnless is the validation function // excludedUnless 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. // 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 skipUnless(fl FieldLevel) bool { func excludedUnless(fl FieldLevel) bool {
params := parseOneOfParam2(fl.Param()) params := parseOneOfParam2(fl.Param())
if len(params)%2 != 0 { if len(params)%2 != 0 {
panic(fmt.Sprintf("Bad param number for skip_unless %s", fl.FieldName())) panic(fmt.Sprintf("Bad param number for excluded_unless %s", fl.FieldName()))
} }
for i := 0; i < len(params); i += 2 { for i := 0; i < len(params); i += 2 {
if !requireCheckFieldValue(fl, params[i], params[i+1], false) { if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
return true return true
} }
} }
return hasValue(fl) return !hasValue(fl)
} }
// excludedUnless is the validation function // skipUnless 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 be present and not empty only unless all the other specified fields are equal to the value following with the specified field.
func excludedUnless(fl FieldLevel) bool { func skipUnless(fl FieldLevel) bool {
params := parseOneOfParam2(fl.Param()) params := parseOneOfParam2(fl.Param())
if len(params)%2 != 0 { if len(params)%2 != 0 {
panic(fmt.Sprintf("Bad param number for excluded_unless %s", fl.FieldName())) panic(fmt.Sprintf("Bad param number for skip_unless %s", fl.FieldName()))
} }
for i := 0; i < len(params); i += 2 { for i := 0; i < len(params); i += 2 {
if !requireCheckFieldValue(fl, params[i], params[i+1], false) { if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
return true return true
} }
} }
return !hasValue(fl) return hasValue(fl)
} }
// excludedWith is the validation function // excludedWith is the validation function

Loading…
Cancel
Save