-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
400 lines (346 loc) · 8.48 KB
/
checker.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package govalid
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"golang.org/x/text/language"
)
// CheckFunc is the type of checker function.
// It returns an error context if error occurred.
type CheckFunc func(ctx CheckerContext) *ErrContext
// CheckerContext is the context of checker,
// which can contains the rule of the checker and the value of the current struct field.
type CheckerContext struct {
StructValue reflect.Value
FieldName string
FieldType reflect.Type
FieldValue interface{}
FieldLabel string
TemplateLanguage language.Tag
Rule *rule
}
// Checkers is the function list of checkers.
var Checkers = map[string]CheckFunc{
"required": required,
"min": min,
"max": max,
"minlen": minlen,
"maxlen": maxlen,
"alpha": alpha,
"alphanumeric": alphaNumeric,
"alphadash": alphaDash,
"username": userName,
"email": email,
"ipv4": ipv4,
"mobile": mobile,
"tel": tel,
"phone": phone,
"idcard": idCard,
"equal": equal,
"list": list,
}
func required(c CheckerContext) *ErrContext {
errCtx := NewErrorContext(c)
if c.FieldValue == nil {
return errCtx
}
// If field is a slice, then check if it is empty.
if c.FieldType.Kind() == reflect.Slice {
if reflect.ValueOf(c.FieldValue).Len() == 0 {
return errCtx
}
return nil
}
zeroValue := reflect.Zero(c.FieldType)
if zeroValue.Interface() == c.FieldValue {
return errCtx
}
return nil
}
func min(c CheckerContext) *ErrContext {
return minOrMax(c, "min")
}
func max(c CheckerContext) *ErrContext {
return minOrMax(c, "max")
}
func minOrMax(c CheckerContext, flag string) *ErrContext {
ctx := NewErrorContext(c)
if len(c.Rule.params) != 1 {
return MakeCheckerParamError(c)
}
limitStr := c.Rule.params[0]
switch reflect.TypeOf(c.FieldValue).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
limit, err := strconv.ParseInt(limitStr, 10, 64)
if err != nil {
return MakeCheckerParamError(c)
}
ctx.SetFieldLimitValue(limit)
value := reflect.ValueOf(c.FieldValue).Int()
if flag == "min" {
if value < limit {
return ctx
}
} else {
if value > limit {
return ctx
}
}
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
limit, err := strconv.ParseUint(limitStr, 10, 64)
if err != nil {
return MakeCheckerParamError(c)
}
ctx.SetFieldLimitValue(limit)
value := reflect.ValueOf(c.FieldValue).Uint()
if flag == "min" {
if value < limit {
return ctx
}
} else {
if value > limit {
return ctx
}
}
return nil
case reflect.Float32, reflect.Float64:
limit, err := strconv.ParseFloat(limitStr, 64)
if err != nil {
return MakeCheckerParamError(c)
}
ctx.SetFieldLimitValue(limit)
value64, ok := c.FieldValue.(float64)
if !ok {
value32, ok := c.FieldValue.(float32)
if !ok {
return MakeValueTypeError(c)
}
value64 = float64(value32)
}
if flag == "min" {
if value64 < limit {
return ctx
}
} else {
if value64 > limit {
return ctx
}
}
return nil
}
return nil
}
func minlen(c CheckerContext) *ErrContext {
return minOrMaxLen(c, "min")
}
func maxlen(c CheckerContext) *ErrContext {
return minOrMaxLen(c, "max")
}
func minOrMaxLen(c CheckerContext, flag string) *ErrContext {
ctx := NewErrorContext(c)
if len(c.Rule.params) != 1 {
return MakeCheckerParamError(c)
}
limit, err := strconv.ParseInt(c.Rule.params[0], 10, 64)
if err != nil {
return MakeCheckerParamError(c)
}
ctx.SetFieldLimitValue(limit)
if c.FieldValue == "" {
return nil
}
value := fmt.Sprintf("%s", c.FieldValue)
length := utf8.RuneCountInString(value)
if flag == "min" {
if int64(length) < limit {
return ctx
}
} else {
if int64(length) > limit {
return ctx
}
}
return nil
}
func alpha(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.ValueOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
for _, v := range c.FieldValue.(string) {
if v < 'A' || (v > 'Z' && v < 'a') || v > 'z' {
return ctx
}
}
return nil
}
func alphaNumeric(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
for _, v := range c.FieldValue.(string) {
if ('Z' < v || v < 'A') && ('z' < v || v < 'a') && ('9' < v || v < '0') {
return ctx
}
}
return nil
}
func alphaDash(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
value := c.FieldValue.(string)
if value == "" {
return nil
}
if !regexp.MustCompile(`^\w+$`).MatchString(value) {
return ctx
}
return nil
}
func userName(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
value := c.FieldValue.(string)
if value == "" {
return nil
}
// is alpha dash.
if ctx := alphaDash(c); ctx != nil {
ctx.SetTemplate("firstCharAlpha")
return ctx
}
// first char must be a alpha.
tmp := c
tmp.FieldValue = fmt.Sprintf("%c", c.FieldValue.(string)[0])
if ctx := alpha(tmp); ctx != nil {
ctx.SetTemplate("firstCharAlpha")
return ctx
}
// last char can't be dash.
if strings.HasSuffix(c.FieldValue.(string), "_") {
ctx.SetTemplate("lastUnderline")
return ctx
}
return nil
}
func email(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
value := ctx.FieldValue.(string)
if value == "" {
return nil
}
emailPattern := `^[\w!#$%&'*+/=?^_` + "`" + `{|}~-]+(?:\.[\w!#$%&'*+/=?^_` + "`" + `{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[a-zA-Z0-9](?:[\w-]*[\w])?$`
if !regexp.MustCompile(emailPattern).MatchString(value) {
return ctx
}
return nil
}
func ipv4(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
value := ctx.FieldValue.(string)
if value == "" {
return nil
}
ipv4Pattern := regexp.MustCompile(`^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$`)
if !ipv4Pattern.MatchString(value) {
return ctx
}
return nil
}
// MobilePattern is used to check the mobile phone.
// Refer to https://github.com/VincentSit/ChinaMobilePhoneNumberRegex
var MobilePattern = regexp.MustCompile(`^(?:\+?86)?1(?:3\d{3}|5[^4\D]\d{2}|8\d{3}|7(?:[0-35-9]\d{2}|4(?:0\d|1[0-2]|9\d))|9[0-35-9]\d{2}|6[2567]\d{2}|4(?:(?:10|4[01])\d{3}|[68]\d{4}|[579]\d{2}))\d{6}$`)
func mobile(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
value := ctx.FieldValue.(string)
if value == "" {
return nil
}
if !MobilePattern.MatchString(value) {
return ctx
}
return nil
}
func tel(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
value := ctx.FieldValue.(string)
if value == "" {
return nil
}
if !regexp.MustCompile(`^(0\d{2,3}(\-)?)?\d{7,8}$`).MatchString(value) {
return ctx
}
return nil
}
func phone(c CheckerContext) *ErrContext {
telErrCtx := tel(c)
mobileErrCtx := mobile(c)
if telErrCtx == nil || mobileErrCtx == nil {
return nil
}
if telErrCtx != nil {
return telErrCtx
}
return mobileErrCtx
}
func idCard(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
if reflect.TypeOf(c.FieldValue).Kind() != reflect.String {
return MakeValueTypeError(c)
}
value := ctx.FieldValue.(string)
if value == "" {
return nil
}
if !regexp.MustCompile(`(^\d{15}$)|(^\d{17}([0-9X])$)`).MatchString(value) {
return ctx
}
return nil
}
func equal(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
value := fmt.Sprintf("%v", ctx.FieldValue)
equalField := c.Rule.params[0]
for i := 0; i < c.StructValue.Type().NumField(); i++ {
if c.StructValue.Type().Field(i).Name == equalField {
equalFieldValue := fmt.Sprintf("%v", c.StructValue.Field(i).Interface())
if value != equalFieldValue {
return ctx
}
return nil
}
}
return MakeFieldNotFoundError(c)
}
func list(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
value := fmt.Sprintf("%v", ctx.FieldValue)
allowValues := c.Rule.params
for _, v := range allowValues {
if value == v {
return nil
}
}
return ctx
}