Skip to content

Commit

Permalink
added: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rezakhademix committed Feb 27, 2024
1 parent 0704f55 commit facfb1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
27 changes: 9 additions & 18 deletions required.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package validator

import (
"fmt"
"strings"
)
import "strings"

const (
// RequiredMethod represents the method name used for finding the default error message.
RequiredMethod = "required"
// RequiredErrorMessage is the default error message format for required fields.
RequiredErrorMessage = "%s is required"
// Required represents the rule name which will be used to find the default error message.
Required = "required"
// RequiredMsg is the default error message format for required fields.
RequiredMsg = "%s is required"
)

// RequiredString checks if a string value is empty or not.
func (v *Validator) RequiredString(value, field string, msg ...string) *Validator {
func (v *Validator) RequiredString(value, field string, msg string) *Validator {
if strings.TrimSpace(value) == "" {
if msg[0] == "" {
msg[0] = fmt.Sprintf(RequiredErrorMessage, field)
}

v.addError(field, msg[0])
v.addError(field, v.msg(Required, field, msg))
}

return v
}

// RequiredInt checks if a integer value is provided or not.
func (v *Validator) RequiredInt(value int, field string, msg ...any) *Validator {
func (v *Validator) RequiredInt(value int, field string, msg string) *Validator {
if value == 0 {
msg := v.errMsg(RequiredMethod, field, msg)

v.addError(field, msg)
v.addError(field, v.msg(Required, field, msg))
}

return v
Expand Down
16 changes: 6 additions & 10 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (

// methodToErrorMessage contains each validation method and its corresponding error message.
methodToErrorMessage = map[string]string{
RequiredMethod: RequiredErrorMessage,
Required: RequiredMsg,
}

// ErrMethodMessageNotFound is the default message when a method does not have any error message on methodToErrorMessage.
Expand Down Expand Up @@ -68,19 +68,15 @@ func (v *Validator) addError(field, msg string) {

// ErrMsg return error message and check if custom error message is set return formatted custom message
// otherwise return rule default message
func (v *Validator) errMsg(method, field string, msgs ...any) string {
if len(msgs) == 1 {
return msgs[0].(string)
func (v *Validator) msg(method, field string, msg string) string {
if msg != "" {
return msg
}

if len(msgs) > 1 {
return fmt.Sprintf(msgs[0].(string), msgs[1:])
}

format, ok := methodToErrorMessage[method]
defaultMsg, ok := methodToErrorMessage[method]
if !ok {
panic(ErrMethodMessageNotFound)
}

return fmt.Sprintf(format, field)
return fmt.Sprintf(defaultMsg, field)
}

0 comments on commit facfb1d

Please sign in to comment.