added IsEqIgnoreCase and IsNeIgnoreCase (#1076)

pull/1081/head
sgallizia 1 year ago committed by GitHub
parent c0b3430b1f
commit 72a3e75460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      README.md
  2. 32
      baked_in.go
  3. 35
      validator_test.go

@ -207,11 +207,13 @@ Baked-in Validations
| Tag | Description |
| - | - |
| eq | Equals |
| eqIgnoreCase | Equals ignoring case |
| gt | Greater than|
| gte | Greater than or equal |
| lt | Less Than |
| lte | Less Than or Equal |
| ne | Not Equal |
| neIgnoreCase | Not Equal ignoring case |
### Other:
| Tag | Description |

@ -86,7 +86,9 @@ var (
"min": hasMinOf,
"max": hasMaxOf,
"eq": isEq,
"eqIgnoreCase": isEqIgnoreCase,
"ne": isNe,
"neIgnoreCase": isNeIgnoreCase,
"lt": isLt,
"lte": isLte,
"gt": isGt,
@ -892,6 +894,12 @@ func isNe(fl FieldLevel) bool {
return !isEq(fl)
}
// isNe is the validation function for validating that the field's string value does not equal the
// provided param value. The comparison is case-insensitive
func isNeIgnoreCase(fl FieldLevel) bool {
return !isEqIgnoreCase(fl)
}
// isLteCrossStructField is the validation function for validating if the current field's value is less than or equal to the field, within a separate struct, specified by the param's value.
func isLteCrossStructField(fl FieldLevel) bool {
field := fl.Field()
@ -1263,6 +1271,22 @@ func isEq(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
// isEqIgnoreCase is the validation function for validating if the current field's string value is
//equal to the param's value.
// The comparison is case-insensitive.
func isEqIgnoreCase(fl FieldLevel) bool {
field := fl.Field()
param := fl.Param()
switch field.Kind() {
case reflect.String:
return strings.EqualFold(field.String(), param)
}
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
// isPostcodeByIso3166Alpha2 validates by value which is country code in iso 3166 alpha 2
// example: `postcode_iso3166_alpha2=US`
func isPostcodeByIso3166Alpha2(fl FieldLevel) bool {
@ -1542,7 +1566,9 @@ func requireCheckFieldKind(fl FieldLevel, param string, defaultNotFoundValue boo
}
// requireCheckFieldValue is a func for check field value
func requireCheckFieldValue(fl FieldLevel, param string, value string, defaultNotFoundValue bool) bool {
func requireCheckFieldValue(
fl FieldLevel, param string, value string, defaultNotFoundValue bool,
) bool {
field, kind, _, found := fl.GetStructFieldOKAdvanced2(fl.Parent(), param)
if !found {
return defaultNotFoundValue
@ -2319,7 +2345,9 @@ func isHostnamePort(fl FieldLevel) bool {
return false
}
// Port must be a iny <= 65535.
if portNum, err := strconv.ParseInt(port, 10, 32); err != nil || portNum > 65535 || portNum < 1 {
if portNum, err := strconv.ParseInt(
port, 10, 32,
); err != nil || portNum > 65535 || portNum < 1 {
return false
}

@ -5207,6 +5207,24 @@ func TestIsNeValidation(t *testing.T) {
Equal(t, errs, nil)
}
func TestIsNeIgnoreCaseValidation(t *testing.T) {
var errs error
validate := New()
s := "abcd"
now := time.Now()
errs = validate.Var(s, "neIgnoreCase=efgh")
Equal(t, errs, nil)
errs = validate.Var(s, "neIgnoreCase=AbCd")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "neIgnoreCase")
PanicMatches(
t, func() { _ = validate.Var(now, "eqIgnoreCase=abcd") }, "Bad field type time.Time",
)
}
func TestIsEqFieldValidation(t *testing.T) {
var errs error
validate := New()
@ -5484,6 +5502,23 @@ func TestIsEqValidation(t *testing.T) {
Equal(t, errs, nil)
}
func TestIsEqIgnoreCaseValidation(t *testing.T) {
var errs error
validate := New()
s := "abcd"
now := time.Now()
errs = validate.Var(s, "eqIgnoreCase=abcd")
Equal(t, errs, nil)
errs = validate.Var(s, "eqIgnoreCase=AbCd")
Equal(t, errs, nil)
PanicMatches(
t, func() { _ = validate.Var(now, "eqIgnoreCase=abcd") }, "Bad field type time.Time",
)
}
func TestOneOfValidation(t *testing.T) {
validate := New()

Loading…
Cancel
Save