-
Notifications
You must be signed in to change notification settings - Fork 10
/
validators.go
488 lines (456 loc) · 12.7 KB
/
validators.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package govalidators
import (
// "errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"unicode/utf8"
)
//验证接口
type Validator interface {
Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)
}
//验证函数
type ValidatorF func(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)
func (f ValidatorF) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
return f(params, val, args...)
}
type Range struct {
Min string
Max string
min string
max string
RangeEMsg map[string]string //keys: lessThan,equal,atLeast,between
}
//将structTag中的min和max解析到结构体中
func (self *Range) InitRangeNum(eParamsMap map[string]string, args ...string) error {
self.min, self.max = self.Min, self.Max
argsL := len(args)
if (self.Min == "" && argsL == 0) || argsL > 2 {
return formatError("[name] validator range error", eParamsMap)
}
if argsL == 1 {
self.min = args[0]
} else if argsL == 2 {
self.min = args[0]
self.max = args[1]
}
return nil
}
func (self *Range) CompareFloat(valNum float64, eParamsMap map[string]string, errorMap map[string]string) error {
if self.min == "" ||
(self.min != VALIDATOR_IGNORE_SIGN && !regexp.MustCompile(FLOAT_REG).MatchString(self.min)) ||
(self.max != VALIDATOR_IGNORE_SIGN && !regexp.MustCompile(FLOAT_REG).MatchString(self.max) && self.max != "") {
return formatError("[name] validator range error", eParamsMap)
}
if self.min == VALIDATOR_IGNORE_SIGN && (self.max == VALIDATOR_IGNORE_SIGN || self.max == "") {
return nil
}
var min, max float64
var ok bool
var errKey, errStr string
if self.min == VALIDATOR_IGNORE_SIGN {
max, _ = strconv.ParseFloat(self.max, 64)
if valNum > max {
errKey = "lessThan"
eParamsMap["max"] = self.max
}
} else if self.max == VALIDATOR_IGNORE_SIGN {
min, _ = strconv.ParseFloat(self.min, 64)
if valNum < min {
errKey = "atLeast"
eParamsMap["min"] = self.min
}
} else if self.max == "" {
min, _ = strconv.ParseFloat(self.min, 64)
if valNum != min {
errKey = "equal"
eParamsMap["min"] = self.min
}
} else {
max, _ = strconv.ParseFloat(self.max, 64)
min, _ = strconv.ParseFloat(self.min, 64)
if valNum < min || valNum > max {
errKey = "between"
eParamsMap["min"] = self.min
eParamsMap["max"] = self.max
}
}
if errKey == "" {
return nil
}
if errStr, ok = self.RangeEMsg[errKey]; !ok {
errStr = errorMap[errKey]
}
return formatError(errStr, eParamsMap)
}
func (self *Range) CompareInteger(valNum int64, eParamsMap map[string]string, errorMap map[string]string) error {
if self.min == "" ||
(self.min != VALIDATOR_IGNORE_SIGN && !regexp.MustCompile(INTEGER_REG).MatchString(self.min)) ||
(self.max != VALIDATOR_IGNORE_SIGN && !regexp.MustCompile(INTEGER_REG).MatchString(self.max) && self.max != "") {
return formatError("[name] validator range error", eParamsMap)
}
if self.min == VALIDATOR_IGNORE_SIGN && (self.max == VALIDATOR_IGNORE_SIGN || self.max == "") {
return nil
}
var min, max int64
var ok bool
var errKey, errStr string
if self.min == VALIDATOR_IGNORE_SIGN {
max, _ = strconv.ParseInt(self.max, 10, 64)
if valNum > max {
errKey = "lessThan"
eParamsMap["max"] = self.max
}
} else if self.max == VALIDATOR_IGNORE_SIGN {
min, _ = strconv.ParseInt(self.min, 10, 64)
if valNum < min {
errKey = "atLeast"
eParamsMap["min"] = self.min
}
} else if self.max == "" {
min, _ = strconv.ParseInt(self.min, 10, 64)
if valNum != min {
errKey = "equal"
eParamsMap["min"] = self.min
}
} else {
max, _ = strconv.ParseInt(self.max, 10, 64)
min, _ = strconv.ParseInt(self.min, 10, 64)
if min >= max {
return formatError("[name] validator range error", eParamsMap)
}
if valNum < min || valNum > max {
errKey = "between"
eParamsMap["min"] = self.min
eParamsMap["max"] = self.max
}
}
if errKey == "" {
return nil
}
if errStr, ok = self.RangeEMsg[errKey]; !ok {
errStr = errorMap[errKey]
}
return formatError(errStr, eParamsMap)
}
type RequiredValidator struct {
EMsg string
}
func (self *RequiredValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is must required"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if isZeroValue(val) {
return false, formatError(eMsg, eParamsMap)
}
return true, nil
}
/**
* 当只有 Min 或者 Max 的值,另一个值为 nil 时,验证器为等于有值的对应值
* 当只有 Min 或者 Max 的值,另一个值为 _ 时,验证器为忽略带 _ 的值
* 栗子
* string=1,2 表示 Min=1,Max=2,就是说 1 <= len(str) <= 2
* string=1 表示 Min=1,Max=nil,就是说 len(str) = 1
* string=1,_ 表示 Min=1,Max=_,就是说 1 <= len(str)
*/
type StringValidator struct {
EMsg string
Range
}
func (self *StringValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not a string"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if !checkString(val.Kind()) {
return false, formatError(eMsg, eParamsMap)
}
//后边不接参数,表示只判断类型
if len(args) == 0 {
return true, nil
}
err := self.InitRangeNum(eParamsMap, args...)
if err != nil {
return false, err
}
strNum := utf8.RuneCountInString(val.String())
err = self.CompareInteger(int64(strNum), eParamsMap, stringErrorMap)
if err != nil {
return false, err
}
return true, nil
}
/**
* 当只有 Min 或者 Max 的值,另一个值为 nil 时,验证器为等于有值的对应值
* 当只有 Min 或者 Max 的值,另一个值为 _ 时,验证器为忽略带 _ 的值
* 栗子
* integer=1,2 表示 Min=1,Max=2,就是说 1 <= num <= 2
* integer=1 表示 Min=1,Max=nil,就是说 num = 1
* integer=1,_ 表示 Min=1,Max=_,就是说 1 <= num
*/
type IntegerValidator struct {
EMsg string
Range
}
func (self *IntegerValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not a integer"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if !checkNumber(val.Kind(), INTEGER_KIND) {
return false, formatError(eMsg, eParamsMap)
}
//后边不接参数,表示只判断类型
if len(args) == 0 {
return true, nil
}
err := self.InitRangeNum(eParamsMap, args...)
if err != nil {
return false, err
}
err = self.CompareInteger(val.Int(), eParamsMap, numberErrorMap)
if err != nil {
return false, err
}
return true, nil
}
/**
* 当只有 Min 或者 Max 的值,另一个值为 nil 时,验证器为等于有值的对应值
* 当只有 Min 或者 Max 的值,另一个值为 _ 时,验证器为忽略带 _ 的值
* 栗子
* string=1,2 表示 Min=1,Max=2,就是说 1 <= len(array) <= 2
* string=1 表示 Min=1,Max=nil,就是说 len(array) = 1
* string=1,_ 表示 Min=1,Max=_,就是说 1 <= len(array)
*/
type ArrayValidator struct {
EMsg string
Range
}
func (self *ArrayValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not a array/map/slice"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if !checkArray(val.Kind()) {
return false, formatError(eMsg, eParamsMap)
}
//后边不接参数,表示只判断类型
if len(args) == 0 {
return true, nil
}
err := self.InitRangeNum(eParamsMap, args...)
err = self.CompareInteger(int64(val.Len()), eParamsMap, arrayErrorMap)
if err != nil {
return false, err
}
return true, nil
}
/**
* 仅支持 string、float、int、bool 类型
* 或值类型为 string、float、int、bool 类型的array、slice、map
*/
type InValidator struct {
EMsg string
TypeEMsg string
}
func (self *InValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not in params [args]"
typeEMsg := "[name] type invalid"
eParamsMap := map[string]string{
"name": params["name"].(string),
"args": fmt.Sprintf("%v", args),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if self.TypeEMsg != "" {
typeEMsg = self.TypeEMsg
}
var valsI []reflect.Value
var argsI []interface{}
kind := val.Kind()
switch kind {
case reflect.Slice, reflect.Array:
kind = val.Type().Elem().Kind()
arrLen := val.Len()
for i := 0; i < arrLen; i++ {
valsI = append(valsI, val.Index(i))
}
case reflect.Map:
kind = val.Type().Elem().Kind()
keys := val.MapKeys()
for _, key := range keys {
valsI = append(valsI, val.MapIndex(key))
}
default:
valsI = append(valsI, val)
}
if !checkBool(kind) && !checkNumber(kind) && !checkString(kind) {
return false, formatError(typeEMsg, eParamsMap)
}
if len(valsI) == 0 {
return false, formatError(eMsg, eParamsMap)
}
//根据 val 类型将 args 转为对应格式
for _, arg := range args {
tmpArg, err := parseStr(arg, kind)
if err != nil {
return false, formatError(eMsg, eParamsMap)
}
argsI = append(argsI, tmpArg)
}
for _, valI := range valsI {
if !InArray(parseReflectV(valI, kind), argsI) {
return false, formatError(eMsg, eParamsMap)
}
}
return true, nil
}
type EmailValidator struct {
EMsg string
Reg string
}
func (self *EmailValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not a email address"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if !checkString(val.Kind()) {
return false, formatError(eMsg, eParamsMap)
}
reg := MAIL_REG
if self.Reg != "" {
reg = self.Reg
}
if !regexp.MustCompile(reg).MatchString(val.String()) {
return false, formatError(eMsg, eParamsMap)
}
return true, nil
}
type UrlValidator struct {
EMsg string
Reg string
}
func (self *UrlValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not a url"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if !checkString(val.Kind()) {
return false, formatError(eMsg, eParamsMap)
}
reg := URL_REG
if self.Reg != "" {
reg = self.Reg
}
if !regexp.MustCompile(reg).MatchString(val.String()) {
return false, formatError(eMsg, eParamsMap)
}
return true, nil
}
type DateTimeValidator struct {
EMsg string
FmtStr string
}
func (self *DateTimeValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not a date time"
fmtStr := "Y-m-d H:i:s"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
if self.FmtStr != "" {
fmtStr = self.FmtStr
}
if len(args) != 0 {
fmtStr = args[0]
}
if !checkString(val.Kind()) {
return false, formatError(eMsg, eParamsMap)
}
//拼接
replaceArr := []string{
"Y", YEAR_REG, "m", MONTH_REF, "d", DAY_REF, "H", HOUR_REF, "i", MINUTE_REF, "s", SECOND_REF,
}
replacer := strings.NewReplacer(replaceArr...)
reg := `^` + replacer.Replace(fmtStr) + `$`
if !regexp.MustCompile(reg).MatchString(val.String()) {
return false, formatError(eMsg, eParamsMap)
}
return true, nil
}
/**
* 仅支持 string、float、int、bool 类型
* 或值类型为 string、float、int、bool 类型的array、slice、map
*/
type UniqueValidator struct {
EMsg string
}
func (self *UniqueValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
eMsg := "[name] is not unique"
typeEMsg := "[name] type invalid"
eParamsMap := map[string]string{
"name": params["name"].(string),
}
if self.EMsg != "" {
eMsg = self.EMsg
}
allKey := params["allKey"].(string)
syncMap := params["syncMap"].(*sync.Map)
kind := val.Kind()
switch kind {
case reflect.Slice, reflect.Array:
kind = val.Type().Elem().Kind()
if !checkBool(kind) && !checkNumber(kind) && !checkString(kind) {
return false, formatError(typeEMsg, eParamsMap)
}
arrLen := val.Len()
for i := 0; i < arrLen; i++ {
tmpV := val.Index(i)
tmpK := fmt.Sprintf("%v_%v", allKey, tmpV)
//fmt.Println("------->", tmpK)
_, ok := syncMap.Load(tmpK)
if ok {
return false, formatError(eMsg, eParamsMap)
}
syncMap.Store(tmpK, true)
}
default:
if !checkBool(kind) && !checkNumber(kind) && !checkString(kind) {
return false, formatError(typeEMsg, eParamsMap)
}
tmpK := fmt.Sprintf("%v_%v", allKey, val)
_, ok := syncMap.Load(tmpK)
//fmt.Println("=====>", syncMap)
if ok {
return false, formatError(eMsg, eParamsMap)
}
syncMap.Store(tmpK, true)
}
return true, nil
}