/
rules.go
578 lines (489 loc) · 14.4 KB
/
rules.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package validate
import (
"fmt"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/text/language"
)
var (
// InvalidTime can be set on a time.Time field to indicate parsing
// the timestamp failed due to invalid formatting.
InvalidTime = time.Unix(0, 0)
validGenders = []string{"male", "female", "genderqueer"}
regexpAz = regexp.MustCompile(`^[a-z][a-z_]*$`)
regexpAZ09 = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_]*$`)
regexpName = regexp.MustCompile(`^[\p{L},.'-][\p{L} ,.'-]*[\p{L},.'-]$`)
regexpEmail = regexp.MustCompile("^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:\\(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22)))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$") //nolint
regexpResourceName = regexp.MustCompile("^mtx:[a-z0-9-/]+(:[a-z0-9-/]+)*$")
regexpResourcePattern = regexp.MustCompile("^mtx:[a-z0-9-*/]+(:[a-z0-9-*/]+)*$")
StandardRules = []ValidationRule{
{
Tag: "required",
Checker: Required,
ErrorFunc: RequiredErr,
},
{
Tag: "optional",
Checker: Optional,
ErrorFunc: nil, // No error causes validation to stop
},
{
Tag: "gte",
Checker: GTE,
ErrorFunc: GTEErr,
},
{
Tag: "lte",
Checker: LTE,
ErrorFunc: LTEErr,
},
{
Tag: "gender",
Checker: Gender,
ErrorFunc: GenderErr,
},
{
Tag: "isodate",
Checker: ISODate,
ErrorFunc: ISODateErr,
},
{
Tag: "mindate",
Checker: MinDate,
ErrorFunc: MinDateErr,
},
{
Tag: "maxdate",
Checker: MaxDate,
ErrorFunc: MaxDateErr,
},
{
Tag: "name",
Checker: Name,
ErrorFunc: NameErr,
},
{
Tag: "az_",
Checker: Az,
ErrorFunc: AzErr,
},
{
Tag: "aZ09_",
Checker: AZ09,
ErrorFunc: AZ09Err,
},
{
Tag: "zoneinfo",
Checker: Zoneinfo,
ErrorFunc: ZoneinfoErr,
},
{
Tag: "locale",
Checker: Locale,
ErrorFunc: LocaleErr,
},
{
Tag: "url",
Checker: URL,
ErrorFunc: URLErr,
},
{
Tag: "email",
Checker: Email,
ErrorFunc: EmailErr,
},
{
Tag: "resourcename",
Checker: ResourceName,
ErrorFunc: ResourceNameErr,
},
{
Tag: "resourcepattern",
Checker: ResourcePattern,
ErrorFunc: ResourcePatternErr,
},
}
StandardAliases = map[string]string{
"username": "aZ09_,gte=4,lte=20",
"birthdate": "isodate,mindate=1900-01-01,maxdate=now",
}
)
// Required tests whether a variable is non-zero as defined by
// the golang spec.
//
// You're advised not to use this validation for booleans and numbers,
// since golang defaults empty numbers to 0 and empty booleans to false.
func Required(v interface{}, _ string) bool { //nolint:cyclop
st := reflect.ValueOf(v)
switch st.Kind() {
case reflect.String:
return len(st.String()) != 0
case reflect.Ptr, reflect.Interface:
return !st.IsNil()
case reflect.Slice, reflect.Map, reflect.Array:
return st.Len() != 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return st.Int() != 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return st.Uint() != 0
case reflect.Float32, reflect.Float64:
return st.Float() != 0
case reflect.Bool:
return st.Bool()
case reflect.Invalid:
return false // always invalid
case reflect.Struct:
return true // always valid since only nil pointers are empty
default:
return false
}
}
func RequiredErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s is required", field)
}
// Optional tests whether a variable is zero as defined by
// the golang spec.
func Optional(v interface{}, _ string) bool {
// It's the same as "required", causing an error when the value
// is empty. However in this case no error will be defined and
// the validation pipeline simply stops.
return Required(v, "")
}
// GTE tests whether a variable value is larger or equal to a given
// number. For number types, it's a simple greater-than test; for strings
// it tests the number of characters whereas for maps and slices it tests
// the number of items.
func GTE(v interface{}, param string) bool {
st := reflect.ValueOf(v)
switch st.Kind() {
case reflect.String:
return int64(len(st.String())) >= asInt(param)
case reflect.Slice, reflect.Map, reflect.Array:
return int64(st.Len()) >= asInt(param)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return st.Int() >= asInt(param)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return st.Uint() >= asUint(param)
case reflect.Float32, reflect.Float64:
return st.Float() >= asFloat(param)
default:
panic("invalid type for gte tag")
}
}
func GTEErr(field string, v interface{}, t Tag) string {
st := reflect.ValueOf(v)
switch st.Kind() {
case reflect.Slice, reflect.Map, reflect.Array:
return fmt.Sprintf("%s must contain at least %s elements", field, t.Param)
case reflect.String:
return fmt.Sprintf("%s must be at least %s characters long", field, t.Param)
default:
return fmt.Sprintf("%s must be at least %s", field, t.Param)
}
}
// LTE tests whether a variable value is smaller or equal to a given
// number. For number types, it's a simple lesser-than test; for strings
// it tests the number of characters whereas for maps and slices it tests
// the number of items.
func LTE(v interface{}, param string) bool {
st := reflect.ValueOf(v)
switch st.Kind() {
case reflect.String:
return int64(len(st.String())) <= asInt(param)
case reflect.Slice, reflect.Map, reflect.Array:
return int64(st.Len()) <= asInt(param)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return st.Int() <= asInt(param)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return st.Uint() <= asUint(param)
case reflect.Float32, reflect.Float64:
return st.Float() <= asFloat(param)
default:
panic("invalid type for lte tag")
}
}
func LTEErr(field string, v interface{}, t Tag) string {
st := reflect.ValueOf(v)
switch st.Kind() {
case reflect.Slice, reflect.Map, reflect.Array:
return fmt.Sprintf("%s may not contain more than %s elements", field, t.Param)
case reflect.String:
return fmt.Sprintf("%s must be at most %s characters long", field, t.Param)
default:
return fmt.Sprintf("%s maximum value is %s", field, t.Param)
}
}
func Gender(v interface{}, _ string) bool {
val, ok := v.(string)
if !ok {
panic("invalid type for gender tag")
}
if val == "" {
return true
}
for _, gender := range validGenders {
if gender == val {
return true
}
}
return false
}
func GenderErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s must be either %s", field, strings.Join(validGenders, ", "))
}
func ISODate(v interface{}, _ string) bool {
st := reflect.ValueOf(v)
if st.Kind() == reflect.Ptr {
if st.IsNil() {
return true
}
st = st.Elem()
}
switch st.Kind() {
case reflect.String:
if st.String() == "" {
return true
}
t, err := time.Parse("2006-01-02", st.String())
if err != nil {
return false
}
res := isWholeDate(t)
return res
case reflect.Struct:
if t, ok := v.(time.Time); ok {
if t.Equal(InvalidTime) {
return false
}
return isWholeDate(t)
}
return false
default:
panic("invalid type for isodate tag")
}
}
func isWholeDate(t time.Time) bool {
return t.Hour() == 0 && t.Minute() == 0 && t.Second() == 0 && t.Nanosecond() == 0
}
func ISODateErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s is not a valid date (YYYY-MM-DD)", field)
}
func MinDate(v interface{}, param string) bool { //nolint:cyclop
st := reflect.ValueOf(v)
if st.Kind() == reflect.Ptr {
if st.IsNil() {
return true
}
st = st.Elem()
}
switch st.Kind() {
case reflect.String:
if st.String() == "" {
return true
}
t, err := time.Parse("2006-01-02", st.String())
if err != nil {
return false
}
minDate := parseDate(param)
return t.After(minDate) || t.Equal(minDate)
case reflect.Struct:
if t, ok := v.(time.Time); ok {
minDate := parseDate(param)
return t.After(minDate) || t.Equal(minDate)
}
return false
default:
panic("invalid type for mindate tag")
}
}
func MinDateErr(field string, _ interface{}, t Tag) string {
return fmt.Sprintf("%s minimum date is %s", field, nowToDateString(t.Param))
}
func MaxDate(v interface{}, param string) bool { //nolint:cyclop
st := reflect.ValueOf(v)
if st.Kind() == reflect.Ptr {
if st.IsNil() {
return true
}
st = st.Elem()
}
switch st.Kind() {
case reflect.String:
if st.String() == "" {
return true
}
t, err := time.Parse("2006-01-02", st.String())
if err != nil {
return false
}
maxDate := parseDate(param)
return t.Before(maxDate) || t.Equal(maxDate)
case reflect.Struct:
if t, ok := v.(time.Time); ok {
maxDate := parseDate(param)
return t.Before(maxDate) || t.Equal(maxDate)
}
return false
default:
panic("invalid type for maxdate tag")
}
}
func MaxDateErr(field string, _ interface{}, t Tag) string {
return fmt.Sprintf("%s maximum date is %s", field, nowToDateString(t.Param))
}
func parseDate(date string) time.Time {
if date == "now" {
return time.Now().UTC()
}
d, err := time.Parse("2006-01-02", date)
if err != nil {
panic(err) // This is a coding error in the tag value
}
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}
func nowToDateString(date string) string {
if date == "now" {
return time.Now().UTC().Format("2006-01-02")
}
return date
}
func Az(v interface{}, _ string) bool {
return RegexChecker("az_", regexpAz, v)
}
func AzErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s must contain a-z, _ and not start with a _", field)
}
func AZ09(v interface{}, _ string) bool {
return RegexChecker("aZ09_", regexpAZ09, v)
}
func AZ09Err(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s must contain 0-9, A-Z, _ and not start with a _", field)
}
func Name(v interface{}, _ string) bool {
return RegexChecker("name", regexpName, v)
}
func NameErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s must contain unicode letters -,.' and not start or end with a space", field)
}
func Zoneinfo(v interface{}, _ string) bool {
val, ok := v.(string)
if !ok {
panic("invalid type for zoneinfo tag")
}
if val != "" {
_, err := time.LoadLocation(val)
if err != nil {
return false
}
}
return true
}
func ZoneinfoErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s is not a valid zoneinfo string (example: 'Europe/Amsterdam')", field)
}
func Locale(v interface{}, _ string) bool {
val, ok := v.(string)
if !ok {
panic("invalid type for locale tag")
}
if val != "" {
tags := strings.Split(val, " ")
for _, s := range tags {
_, err := language.Parse(s)
if err != nil {
return false
}
}
}
return true
}
func LocaleErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s must contain BCP47 language tags separated by spaces", field)
}
func URL(v interface{}, _ string) bool {
val, ok := v.(string)
if !ok {
panic("invalid type for url tag")
}
if val == "" {
return true
}
// Strip # prior to validation
var i int
if i = strings.Index(val, "#"); i > -1 {
val = val[:i]
}
parsedURL, err := url.ParseRequestURI(val)
if err != nil || parsedURL.Scheme == "" {
return false
}
return true
}
func URLErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s is not a valid url", field)
}
func Email(v interface{}, _ string) bool {
return RegexChecker("email", regexpEmail, v)
}
func EmailErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s is not a valid email", field)
}
func ResourceName(v interface{}, _ string) bool {
return RegexChecker("resourcename", regexpResourceName, v)
}
func ResourceNameErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s must start with 'mtx:' and may contain: a-z, 0-9, -, /, and :", field)
}
func ResourcePattern(v interface{}, _ string) bool {
return RegexChecker("resourcepattern", regexpResourcePattern, v)
}
func ResourcePatternErr(field string, _ interface{}, _ Tag) string {
return fmt.Sprintf("%s must start with 'mtx:' and may contain: a-z, 0-9, -, /, *, and :", field)
}
func RegexChecker(tagName string, match *regexp.Regexp, v interface{}) bool {
st := reflect.ValueOf(v)
switch st.Kind() {
case reflect.Array:
fallthrough
case reflect.Slice:
for i := 0; i < st.Len(); i++ {
if !RegexChecker(tagName, match, st.Index(i).Interface()) {
return false
}
}
return true
case reflect.String:
if st.String() == "" {
return true
}
return match.MatchString(st.String())
default:
panic(fmt.Sprintf("invalid type for %s tag", tagName))
}
}
func asInt(param string) int64 {
i, err := strconv.ParseInt(param, 0, 64) //nolint:gomnd
if err != nil {
panic(fmt.Sprintf("cannot cast %q to int", param))
}
return i
}
func asUint(param string) uint64 {
i, err := strconv.ParseUint(param, 0, 64) //nolint:gomnd
if err != nil {
panic(fmt.Sprintf("cannot cast %q to uint", param))
}
return i
}
func asFloat(param string) float64 {
i, err := strconv.ParseFloat(param, 64) //nolint:gomnd
if err != nil {
panic(fmt.Sprintf("cannot cast %q to float", param))
}
return i
}