forked from lerity-yao/param-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
202 lines (181 loc) · 7.34 KB
/
Copy pathparse.go
File metadata and controls
202 lines (181 loc) · 7.34 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package params_validator
import (
"errors"
"fmt"
"github.com/go-playground/validator/v10"
"github.com/zeromicro/go-zero/core/logx"
"net/http"
"strings"
)
// HttpxParseValidator 构建 httpx Validator
type HttpxParseValidator struct {
Validator
}
// MustNewHttpxParseValidator 构建 httpx Validator 实例,捕捉错误
func MustNewHttpxParseValidator(conf Conf) *HttpxParseValidator {
vd := MustNewValidate(conf)
h := &HttpxParseValidator{*vd}
err := h.InitRegisterValidation()
logx.Must(err)
return h
}
// Validate 构建 httpx Validator的 Validate
func (v *HttpxParseValidator) Validate(r *http.Request, data any) (err error) {
defer func() {
if r := recover(); r != nil {
// 捕获 panic 并将其转换为 error
err = fmt.Errorf("validation panic: %v", r)
}
}()
err = v.Validator.Validator.Struct(data)
if err != nil {
var errMsg []string
for _, e := range err.(validator.ValidationErrors) {
errMsg = append(errMsg, e.Translate(v.Validator.Trans))
}
return errors.New(strings.Join(errMsg, ","))
}
return nil
}
func (v *HttpxParseValidator) InitRegisterValidation() (err error) {
if err = v.xPhone(); err != nil {
return err
}
if err = v.xPassword(); err != nil {
return err
}
if err = v.xStr(); err != nil {
return err
}
if err = v.xStrWithoutSpec(); err != nil {
return err
}
if err = v.xStrWithoutZh(); err != nil {
return err
}
if err = v.xStrWithoutSpecAndSpace(); err != nil {
return err
}
if err = v.xStrWithoutZhAndSpec(); err != nil {
return err
}
//只能是中文,并且二侧不能有空格
if err = v.xStrZhWithoutSpace(); err != nil {
return err
}
return nil
}
// xPhone 注册自定义 xPhone 方法
// xPhone 为手机号校验规则, 校验其是否为1开头,长度为11位的数字字符串
func (v *HttpxParseValidator) xPhone() error {
if err := v.Validator.RegisterValidation("xPhone", xPhone); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xPhone", "{0}必须为手机号,1开头,长度为11位", false); err != nil {
return err
}
return nil
}
// xPassword 注册自定义 xPassword 方法
// xPassword 为密码校验规则, 使用方法为 validate:"xPassword=8-15" 代表 字符串长度为 8到15位。左右都是闭区间
// xPassword 为密码校验规则, 校验其长度位,需由字母(同时要大小和写)、数字、特殊字符串三种组成,不能使用空格、中文
func (v *HttpxParseValidator) xPassword() error {
if err := v.Validator.RegisterValidation("xPassword", xPassword); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xPassword", "{0}长度{1},需由字母(区分大小写)、数字、特殊字符串三种组成,不能使用空格、中文", false); err != nil {
return err
}
return nil
}
// xStr 注册自定义 xStr 方法
// xStr 为字符串校验方法,使用方法为 validate:"xStr=1-300" 代表字符串长度为 1-300位,左右都为闭区间
// xStr 长度自定义
// xStr 首尾不能有空格,中间可以有空格
// xStr 允许中文,特殊字符串,英文,数字
func (v *HttpxParseValidator) xStr() error {
if err := v.Validator.RegisterValidation("xStr", xStr); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xStr", "{0}长度{1},首尾不能有空格", false); err != nil {
return err
}
return nil
}
// xStrWithoutZh 注册自定义 xStrWithoutZh 方法
// xStrWithoutZh 为字符串校验方法,使用方法为 validate:"xStrWithoutZh=1-300" 代表字符串长度为 1-300位,左右都为闭区间
// xStrWithoutZh 长度自定义
// xStrWithoutZh 首尾不能有空格,中间可以有空格
// xStrWithoutZh 不能包含中文
// xStrWithoutZh 允许特殊字符串,英文,数字
func (v *HttpxParseValidator) xStrWithoutZh() error {
if err := v.Validator.RegisterValidation("xStrWithoutZh", xStrWithoutZh); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xStrWithoutZh", "{0}长度{1},首尾不能有空格,不能包含中文", false); err != nil {
return err
}
return nil
}
// xStrWithoutZhAndSpec
// xStrWithoutZhAndSpec 注册自定义 xStrWithoutZhAndSpec 方法
// xStrWithoutZhAndSpec 为字符串校验方法,使用方法为 validate:"xStrWithoutZhAndSpec=1-300" 代表字符串长度为 1-300位,左右都为闭区间
// xStrWithoutZhAndSpec 长度自定义
// xStrWithoutZhAndSpec 首尾不能有空格,中间可以有空格
// xStrWithoutZhAndSpec 不能包含中文
// xStrWithoutZhAndSpec 不能包含特殊字符 !@#$%^&*()_+\-=[]{};':"\\|,.<>/?等
// xStrWithoutZhAndSpec 允许字母和数字
func (v *HttpxParseValidator) xStrWithoutZhAndSpec() error {
if err := v.Validator.RegisterValidation("xStrWithoutZhAndSpec", xStrWithoutZhAndSpec); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xStrWithoutZhAndSpec", "{0}长度{1},首尾不能有空格,不能包含中文和特殊字符串", false); err != nil {
return err
}
return nil
}
// xStrWithoutSpec 注册自定义 xStrWithoutSpec 方法
// xStrWithoutSpec 为字符串校验方法,使用方法为 validate:"xStrWithoutSpec=1-300" 代表字符串长度为 1-300位,左右都为闭区间
// xStrWithoutSpec 长度自定义
// xStrWithoutSpec 首尾不能有空格,中间可以有空格
// xStrWithoutSpec 不能包含特殊字符 !@#$%^&*()_+\-=[]{};':"\\|,.<>/?等
// xStrWithoutSpec 可以包含其他字符(如字母、数字、空格、中文等)。
func (v *HttpxParseValidator) xStrWithoutSpec() error {
if err := v.Validator.RegisterValidation("xStrWithoutSpec", xStrWithoutSpec); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xStrWithoutSpec", "{0}长度{1},首尾不能有空格,不能包含特殊字符串", false); err != nil {
return err
}
return nil
}
// xStrWithoutSpecAndSpace 注册自定义 xStrWithoutSpecAndSpace 方法
// xStrWithoutSpecAndSpace 为字符串校验方法,使用方法为 validate:"xStrWithoutSpecAndSpace=1-300" 代表字符串长度为 1-300位,左右都为闭区间
// xStrWithoutSpecAndSpace 长度自定义
// xStrWithoutSpecAndSpace 不能有空格
// xStrWithoutSpecAndSpace 不能包含特殊字符 !@#$%^&*()_+\-=[]{};':"\\|,.<>/?等
// xStrWithoutSpecAndSpace 可以包含其他字符(如字母、数字、中文等)。
func (v *HttpxParseValidator) xStrWithoutSpecAndSpace() error {
if err := v.Validator.RegisterValidation("xStrWithoutSpecAndSpace", xStrWithoutSpecAndSpace); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xStrWithoutSpecAndSpace", "{0}长度{1},不能有空格,不能包含特殊字符串", false); err != nil {
return err
}
return nil
}
// xStrZhWithoutSpace 注册自定义 xStrZhWithoutSpace 方法
// xStrZhWithoutSpace 为字符串校验方法,使用方法为 validate:"xStrZhWithoutSpace=1-300" 代表字符串长度为 1-300位,左右都为闭区间
// xStrZhWithoutSpace 长度自定义
// xStrZhWithoutSpace 不能有空格
// xStrZhWithoutSpace 不能包含特殊字符 !@#$%^&*()_+\-=[]{};':"\\|,.<>/?等
// xStrZhWithoutSpace 只包含中文
func (v *HttpxParseValidator) xStrZhWithoutSpace() error {
if err := v.Validator.RegisterValidation("xStrZhWithoutSpace", xStrZhWithoutSpace); err != nil {
return err
}
if err := v.Validator.RegisterTranslation("xStrZhWithoutSpace", "{0}长度{1},不能有空格,只能是中文", false); err != nil {
return err
}
return nil
}