-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
validator.go
74 lines (61 loc) 路 1.84 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package validator
import (
"fmt"
"reflect"
"regexp"
"strings"
"github.com/go-playground/validator/v10"
)
const (
alphaSpaceRegexString string = "^[a-zA-Z ]+$"
)
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
})
validate.RegisterValidation("alpha_space", isAlphaSpace)
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())
case "alpha_space":
resp.Errors[i] = fmt.Sprintf("%s can only contain alphabetic and space characters", err.Field())
case "datetime":
if err.Param() == "2006-01-02" {
resp.Errors[i] = fmt.Sprintf("%s must be a valid date", err.Field())
} else {
resp.Errors[i] = fmt.Sprintf("%s must follow %s format", err.Field(), err.Param())
}
default:
resp.Errors[i] = fmt.Sprintf("something wrong on %s; %s", err.Field(), err.Tag())
}
}
return &resp
}
return nil
}
func isAlphaSpace(fl validator.FieldLevel) bool {
reg := regexp.MustCompile(alphaSpaceRegexString)
return reg.MatchString(fl.Field().String())
}