Repository navigation
Expand file tree
/
Copy pathgvalid_check.go
More file actions
488 lines (449 loc) · 13.1 KB
/
Copy pathgvalid_check.go
File metadata and controls
488 lines (449 loc) · 13.1 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
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
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gvalid
import (
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/net/gipv4"
"github.com/gogf/gf/net/gipv6"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/util/gconv"
"regexp"
"strconv"
"strings"
)
const (
// regular expression pattern for single validation rule.
gSINGLE_RULE_PATTERN = `^([\w-]+):{0,1}(.*)`
)
var (
// regular expression object for single rule
// which is compiled just once and of repeatable usage.
ruleRegex, _ = regexp.Compile(gSINGLE_RULE_PATTERN)
// mustCheckRulesEvenValueEmpty specifies some rules that must be validated
// even the value is empty (nil or empty).
mustCheckRulesEvenValueEmpty = map[string]struct{}{
"required": {},
"required-if": {},
"required-unless": {},
"required-with": {},
"required-with-all": {},
"required-without": {},
"required-without-all": {},
"same": {},
"different": {},
"in": {},
"not-in": {},
"regex": {},
}
// allSupportedRules defines all supported rules that is used for quick checks.
allSupportedRules = map[string]struct{}{
"required": {},
"required-if": {},
"required-unless": {},
"required-with": {},
"required-with-all": {},
"required-without": {},
"required-without-all": {},
"date": {},
"date-format": {},
"email": {},
"phone": {},
"telephone": {},
"passport": {},
"password": {},
"password2": {},
"password3": {},
"postcode": {},
"resident-id": {},
"bank-card": {},
"qq": {},
"ip": {},
"ipv4": {},
"ipv6": {},
"mac": {},
"url": {},
"domain": {},
"length": {},
"min-length": {},
"max-length": {},
"between": {},
"min": {},
"max": {},
"json": {},
"integer": {},
"float": {},
"boolean": {},
"same": {},
"different": {},
"in": {},
"not-in": {},
"regex": {},
}
// boolMap defines the boolean values.
boolMap = map[string]struct{}{
"1": {},
"true": {},
"on": {},
"yes": {},
"": {},
"0": {},
"false": {},
"off": {},
"no": {},
}
)
// Check checks single value with specified rules.
// It returns nil if successful validation.
//
// The parameter <value> can be any type of variable, which will be converted to string
// for validation.
// The parameter <rules> can be one or more rules, multiple rules joined using char '|'.
// The parameter <messages> specifies the custom error messages, which can be type of:
// string/map/struct/*struct.
// The optional parameter <params> specifies the extra validation parameters for some rules
// like: required-*、same、different, etc.
func Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
return doCheck("", value, rules, messages, params...)
}
// doCheck does the really rules validation for single key-value.
func doCheck(key string, value interface{}, rules string, messages interface{}, params ...interface{}) *Error {
if rules == "" {
return nil
}
// It converts value to string and then does the validation.
var (
val = strings.TrimSpace(gconv.String(value))
data = make(map[string]string)
errorMsgs = make(map[string]string)
)
if len(params) > 0 {
for k, v := range gconv.Map(params[0]) {
data[k] = gconv.String(v)
}
}
// Custom error messages handling.
var (
msgArray = make([]string, 0)
customMsgMap = make(map[string]string)
)
switch v := messages.(type) {
case string:
msgArray = strings.Split(v, "|")
default:
for k, v := range gconv.Map(messages) {
customMsgMap[k] = gconv.String(v)
}
}
// Handle the char '|' in the rule,
// which makes this rule separated into multiple rules.
ruleItems := strings.Split(strings.TrimSpace(rules), "|")
for i := 0; ; {
if _, ok := allSupportedRules[array[0]]; !ok {
if i > 0 && ruleItems[i-1][:5] == "regex" {
ruleItems[i-1] += "|" + ruleItems[i]
ruleItems = append(ruleItems[:i], ruleItems[i+1:]...)
} else {
return newErrorStr(
"invalid_rules",
"invalid rules: "+rules,
)
}
} else {
i++
}
if i == len(ruleItems) {
break
}
}
for index := 0; index < len(ruleItems); {
var (
item = ruleItems[index]
match = false
results = ruleRegex.FindStringSubmatch(item)
ruleKey = strings.TrimSpace(results[1])
ruleVal = strings.TrimSpace(results[2])
)
if len(msgArray) > index {
customMsgMap[ruleKey] = strings.TrimSpace(msgArray[index])
}
switch ruleKey {
// Required rules.
case
"required",
"required-if",
"required-unless",
"required-with",
"required-with-all",
"required-without",
"required-without-all":
match = checkRequired(val, ruleKey, ruleVal, data)
// Length rules.
// It also supports length of unicode string.
case
"length",
"min-length",
"max-length":
if msg := checkLength(val, ruleKey, ruleVal, customMsgMap); msg != "" {
errorMsgs[ruleKey] = msg
} else {
match = true
}
// Range rules.
case
"min",
"max",
"between":
if msg := checkRange(val, ruleKey, ruleVal, customMsgMap); msg != "" {
errorMsgs[ruleKey] = msg
} else {
match = true
}
// Custom regular expression.
case "regex":
// It here should check the rule as there might be special char '|' in it.
for i := index + 1; i < len(ruleItems); i++ {
if !gregex.IsMatchString(gSINGLE_RULE_PATTERN, ruleItems[i]) {
ruleVal += "|" + ruleItems[i]
index++
}
}
match = gregex.IsMatchString(ruleVal, val)
// Date rules.
case "date":
// Standard date string, which must contain char '-' or '.'.
if _, err := gtime.StrToTime(val); err == nil {
match = true
break
}
// Date that not contains char '-' or '.'.
if _, err := gtime.StrToTime(val, "Ymd"); err == nil {
match = true
break
}
// Date rule with specified format.
case "date-format":
if _, err := gtime.StrToTimeFormat(val, ruleVal); err == nil {
match = true
} else {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":format", ruleVal, -1)
errorMsgs[ruleKey] = msg
}
// Values of two fields should be equal as string.
case "same":
if v, ok := data[ruleVal]; ok {
if strings.Compare(val, v) == 0 {
match = true
}
}
if !match {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":field", ruleVal, -1)
errorMsgs[ruleKey] = msg
}
// Values of two fields should not be equal as string.
case "different":
match = true
if v, ok := data[ruleVal]; ok {
if strings.Compare(val, v) == 0 {
match = false
}
}
if !match {
var msg string
msg = getErrorMessageByRule(ruleKey, customMsgMap)
msg = strings.Replace(msg, ":field", ruleVal, -1)
errorMsgs[ruleKey] = msg
}
// Field value should be in range of.
case "in":
array := strings.Split(ruleVal, ",")
for _, v := range array {
if strings.Compare(val, strings.TrimSpace(v)) == 0 {
match = true
break
}
}
// Field value should not be in range of.
case "not-in":
match = true
array := strings.Split(ruleVal, ",")
for _, v := range array {
if strings.Compare(val, strings.TrimSpace(v)) == 0 {
match = false
break
}
}
// Phone format validation.
// 1. China Mobile:
// 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188,
// 178(4G), 147(Net);
//
// 2. China Unicom:
// 130, 131, 132, 155, 156, 185, 186 ,176(4G), 145(Net), 175
//
// 3. China Telecom:
// 133, 153, 180, 181, 189, 177(4G)
//
// 4. Satelite:
// 1349
//
// 5. Virtual:
// 170, 173
//
// 6. 2018:
// 16x, 19x
case "phone":
match = gregex.IsMatchString(`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[\d]{9}$|^17[0,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[\d]{9}$`, val)
// Telephone number:
// "XXXX-XXXXXXX"
// "XXXX-XXXXXXXX"
// "XXX-XXXXXXX"
// "XXX-XXXXXXXX"
// "XXXXXXX"
// "XXXXXXXX"
case "telephone":
match = gregex.IsMatchString(`^((\d{3,4})|\d{3,4}-)?\d{7,8}$`, val)
// QQ number: from 10000.
case "qq":
match = gregex.IsMatchString(`^[1-9][0-9]{4,}$`, val)
// Postcode number.
case "postcode":
match = gregex.IsMatchString(`^\d{6}$`, val)
// China resident id number.
//
// xxxxxx yyyy MM dd 375 0 十八位
// xxxxxx yy MM dd 75 0 十五位
//
// 地区: [1-9]\d{5}
// 年的前两位:(18|19|([23]\d)) 1800-2399
// 年的后两位:\d{2}
// 月份: ((0[1-9])|(10|11|12))
// 天数: (([0-2][1-9])|10|20|30|31) 闰年不能禁止29+
//
// 三位顺序码:\d{3}
// 两位顺序码:\d{2}
// 校验码: [0-9Xx]
//
// 十八位:^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
// 十五位:^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$
//
// 总:
// (^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)
case "resident-id":
match = checkResidentId(val)
// Bank card number using LUHN algorithm.
case "bank-card":
match = checkLuHn(val)
// Universal passport format rule:
// Starting with letter, containing only numbers or underscores, length between 6 and 18.
case "passport":
match = gregex.IsMatchString(`^[a-zA-Z]{1}\w{5,17}$`, val)
// Universal password format rule1:
// Containing any visible chars, length between 6 and 18.
case "password":
match = gregex.IsMatchString(`^[\w\S]{6,18}$`, val)
// Universal password format rule2:
// Must meet password rule1, must contain lower and upper letters and numbers.
case "password2":
if gregex.IsMatchString(`^[\w\S]{6,18}$`, val) &&
gregex.IsMatchString(`[a-z]+`, val) &&
gregex.IsMatchString(`[A-Z]+`, val) &&
gregex.IsMatchString(`\d+`, val) {
match = true
}
// Universal password format rule3:
// Must meet password rule1, must contain lower and upper letters, numbers and special chars.
case "password3":
if gregex.IsMatchString(`^[\w\S]{6,18}$`, val) &&
gregex.IsMatchString(`[a-z]+`, val) &&
gregex.IsMatchString(`[A-Z]+`, val) &&
gregex.IsMatchString(`\d+`, val) &&
gregex.IsMatchString(`[^a-zA-Z0-9]+`, val) {
match = true
}
// Json.
case "json":
if _, err := gjson.Decode([]byte(val)); err == nil {
match = true
}
// Integer.
case "integer":
if _, err := strconv.Atoi(val); err == nil {
match = true
}
// Float.
case "float":
if _, err := strconv.ParseFloat(val, 10); err == nil {
match = true
}
// Boolean(1,true,on,yes:true | 0,false,off,no,"":false).
case "boolean":
match = false
if _, ok := boolMap[strings.ToLower(val)]; ok {
match = true
}
// Email.
case "email":
match = gregex.IsMatchString(`^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+$`, val)
// URL
case "url":
match = gregex.IsMatchString(`(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`, val)
// Domain
case "domain":
match = gregex.IsMatchString(`^([0-9a-zA-Z][0-9a-zA-Z\-]{0,62}\.)+([a-zA-Z]{0,62})$`, val)
// IP(IPv4/IPv6).
case "ip":
match = gipv4.Validate(val) || gipv6.Validate(val)
// IPv4.
case "ipv4":
match = gipv4.Validate(val)
// IPv6.
case "ipv6":
match = gipv6.Validate(val)
// MAC.
case "mac":
match = gregex.IsMatchString(`^([0-9A-Fa-f]{2}[\-:]){5}[0-9A-Fa-f]{2}$`, val)
default:
if f, ok := customRuleFuncMap[ruleKey]; ok {
var (
dataMap map[string]interface{}
message = getErrorMessageByRule(ruleKey, customMsgMap)
)
if len(params) > 0 {
dataMap = gconv.Map(params[0])
}
if err := f(value, message, dataMap); err != nil {
match = false
errorMsgs[ruleKey] = err.Error()
} else {
match = true
}
} else {
errorMsgs[ruleKey] = "Invalid rule name: " + ruleKey
}
}
// Error message handling.
if !match {
// It does nothing if the error message for this rule
// is already set in previous validation.
if _, ok := errorMsgs[ruleKey]; !ok {
errorMsgs[ruleKey] = getErrorMessageByRule(ruleKey, customMsgMap)
}
}
index++
}
if len(errorMsgs) > 0 {
return newError([]string{rules}, ErrorMap{
key: errorMsgs,
})
}
return nil
}