Skip to content

Commit

Permalink
fix: errorMap
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkabwe committed May 12, 2024
1 parent e9f20ba commit 72217c1
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ func (v *validator) IsIP(input string) (ok bool, err error) {
return true, nil
}

func (v *validator) isInt(input any) (ok bool, err error) {
switch input := input.(type) {
func (v *validator) isInt(input any,fieldName string) (ok bool, err error) {
switch input.(type) {
case int:
return true, nil
default:
return false, fmt.Errorf("expected int, got %T", input)
return false, fmt.Errorf("%v is not an integer", fieldName)
}
}

Expand Down Expand Up @@ -141,21 +141,21 @@ func (v *validator) handleStructValidation(input []string, fieldName string, fie
switch field {
case "required":
if fieldValue == "" {
v.errors = errorMap{fieldName: fmt.Errorf("%s is required", fieldName)}
v.errors[fieldName] = fmt.Errorf("%s is required", fieldName)
}
case "int":
if ok, err := v.isInt(fieldValue); !ok {
v.errors = errorMap{fieldName: err}
if ok, err := v.isInt(fieldValue, fieldName); !ok {
v.errors[fieldName] = err
}
case "email":
if ok, err := v.IsEmail(fieldValue.(string)); !ok {
v.errors = errorMap{fieldName: err}
v.errors[fieldName] = err
}

case "url":
ok, err := v.IsURL(fieldValue.(string))
if !ok {
v.errors = errorMap{fieldName: err}
v.errors[fieldName] = err
}
}
}
Expand Down

0 comments on commit 72217c1

Please sign in to comment.