-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.go
149 lines (126 loc) · 3.21 KB
/
validate.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package common
import (
"reflect"
"strings"
"github.com/creasty/defaults"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
)
type ErrorDetailElement struct {
Tag string `json:"tag"`
Field string `json:"field"`
Kind reflect.Kind `json:"-"`
Value any `json:"value"`
Param string `json:"param"`
StructField string `json:"struct_field"`
Message string `json:"message"`
}
func (e *ErrorDetailElement) Error() string {
if e.Message != "" {
return e.Message
}
switch e.Tag {
case "min":
if e.Kind == reflect.String {
e.Message = e.Field + "至少" + e.Param + "字符"
} else {
e.Message = e.Field + "至少为" + e.Param
}
case "max":
if e.Kind == reflect.String {
e.Message = e.Field + "限长" + e.Param + "字符"
} else {
e.Message = e.Field + "至多为" + e.Param
}
case "required":
e.Message = e.Field + "不能为空"
case "email":
e.Message = "邮箱格式不正确"
default:
e.Message = e.StructField + "格式不正确"
}
return e.Message
}
type ErrorDetail []*ErrorDetailElement
func (e ErrorDetail) Error() string {
if len(e) == 0 {
return "Validation Error"
}
if len(e) == 1 {
return e[0].Error()
}
var stringBuilder strings.Builder
stringBuilder.WriteString(e[0].Error())
for _, err := range e[1:] {
stringBuilder.WriteString(", ")
stringBuilder.WriteString(err.Error())
}
return stringBuilder.String()
}
var Validate = validator.New()
func init() {
Validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
}
func ValidateStruct(model any) error {
errors := Validate.Struct(model)
if errors != nil {
var errorDetail ErrorDetail
for _, err := range errors.(validator.ValidationErrors) {
detail := ErrorDetailElement{
Field: err.Field(),
Tag: err.Tag(),
Param: err.Param(),
Kind: err.Kind(),
Value: err.Value(),
StructField: err.StructField(),
}
errorDetail = append(errorDetail, &detail)
}
return &errorDetail
}
return nil
}
// ValidateQuery parse, set default and validate query into model
func ValidateQuery(c *fiber.Ctx, model any) error {
// parse query into struct
// see https://docs.gofiber.io/api/ctx/#queryparser
err := c.QueryParser(model)
if err != nil {
return BadRequest(err.Error())
}
// set default value
err = defaults.Set(model)
if err != nil {
return err
}
// Validate
return ValidateStruct(model)
}
// ValidateBody parse, set default and validate body based on Content-Type.
// It supports json, xml and form only when struct tag exists; if empty, using defaults.
func ValidateBody(c *fiber.Ctx, model any) error {
body := c.Body()
// empty request body, return default value
if len(body) == 0 {
return defaults.Set(model)
}
// parse json, xml and form by fiber.BodyParser into struct
// see https://docs.gofiber.io/api/ctx/#bodyparser
err := c.BodyParser(model)
if err != nil {
return BadRequest(err.Error())
}
// set default value
err = defaults.Set(model)
if err != nil {
return err
}
// Validate
return ValidateStruct(model)
}