Created
August 17, 2019 14:50
-
-
Save dumindu/16c730242a5083062091eb9d6f176ab4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package validator | |
import ( | |
"fmt" | |
"reflect" | |
"strings" | |
"gopkg.in/go-playground/validator.v9" | |
) | |
type ErrResponse struct { | |
Errors []string `json:"errors"` | |
} | |
func New() *validator.Validate { | |
validate := validator.New() | |
validate.SetTagName("form") | |
// Using the names which have been specified for JSON representations of structs, rather than normal Go field names | |
validate.RegisterTagNameFunc(func(fld reflect.StructField) string { | |
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0] | |
if name == "-" { | |
return "" | |
} | |
return name | |
}) | |
return validate | |
} | |
func ToErrResponse(err error) *ErrResponse { | |
if fieldErrors, ok := err.(validator.ValidationErrors); ok { | |
resp := ErrResponse{ | |
Errors: make([]string, len(fieldErrors)), | |
} | |
for i, err := range fieldErrors { | |
switch err.Tag() { | |
case "required": | |
resp.Errors[i] = fmt.Sprintf("%s is a required field", err.Field()) | |
case "max": | |
resp.Errors[i] = fmt.Sprintf("%s must be a maximum of %s in length", err.Field(), err.Param()) | |
case "url": | |
resp.Errors[i] = fmt.Sprintf("%s must be a valid URL", err.Field()) | |
default: | |
resp.Errors[i] = fmt.Sprintf("something wrong on %s; %s", err.Field(), err.Tag()) | |
} | |
} | |
return &resp | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment