Skip to content

Commit

Permalink
added IsEqIgnoreCase and IsNeIgnoreCase (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgallizia committed Mar 19, 2023
1 parent c0b3430 commit 72a3e75
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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 |
Expand Down
32 changes: 30 additions & 2 deletions baked_in.go
Expand Up @@ -86,7 +86,9 @@ var (
"min": hasMinOf,
"max": hasMaxOf,
"eq": isEq,
"eqIgnoreCase": isEqIgnoreCase,
"ne": isNe,
"neIgnoreCase": isNeIgnoreCase,
"lt": isLt,
"lte": isLte,
"gt": isGt,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
35 changes: 35 additions & 0 deletions validator_test.go
Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 72a3e75

Please sign in to comment.