-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidate.go
694 lines (610 loc) · 15.8 KB
/
validate.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// Package validate provides simple validation for Go.
//
// Basic usage example:
//
// v := validate.New()
// v.Required("firstName", customer.FirstName)
// if v.HasErrors() {
// fmt.Println("Had the following validation errors:")
// for key, errors := range v.Errors {
// fmt.Printf(" %s: %s", key, strings.Join(errors))
// }
// }
//
// All validators treat the input's zero type (empty string, 0, nil, etc.) as
// valid. Use the Required() validator if you want to make a parameter required.
//
// All validators optionally accept a custom message as the last parameter:
//
// v.Required("key", value, "you really need to set this")
//
// The error text only includes a simple human description such as "must be set"
// or "must be a valid email". When adding new validations, make sure that they
// can be displayed properly when joined with commas. A text such as "Error:
// this field must be higher than 42" would look weird:
//
// must be set, Error: this field must be higher than 42
//
// You can set your own errors with v.Append():
//
// if !condition {
// v.Append("key", "must be a valid foo")
// }
//
// Some validators return the parsed value, which makes it easier both validate
// and get a useful value at the same time:
//
// v := validate.New()
// id := v.Integer("id", c.Param("id"))
// if v.HasErrors() {
// return v
// }
// user := getUserByID(id)
package validate // import "github.com/teamwork/validate"
import (
"encoding/json"
"fmt"
"net"
"net/url"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/teamwork/mailaddress"
)
// Validator hold the validation errors.
//
// Typically you shouldn't create this directly but use the New() function.
type Validator struct {
Errors map[string][]string `json:"errors"`
}
// New makes a new Validator and ensures that it is properly initialized.
func New() Validator {
v := Validator{}
v.Errors = make(map[string][]string)
return v
}
// Error interface.
func (v Validator) Error() string { return v.String() }
// Code returns the HTTP status code for the error. Satisfies the guru.coder
// interface in github.com/teamwork/guru.
func (v Validator) Code() int { return 400 }
// ErrorJSON for reporting errors as JSON.
func (v Validator) ErrorJSON() ([]byte, error) { return json.Marshal(v) }
// Append a new error to the error list for this key.
func (v *Validator) Append(key, value string, format ...interface{}) {
v.Errors[key] = append(v.Errors[key], fmt.Sprintf(value, format...))
}
// HasErrors reports if this validation has any errors.
func (v *Validator) HasErrors() bool {
return len(v.Errors) > 0
}
// ErrorOrNil returns nil if there are no errors, or the Validator object if there are.
//
// This makes it a bit more elegant to return from a function:
//
// if v.HasErrors() {
// return v
// }
// return nil
//
// Can now be:
//
// return v.ErrorOrNil()
func (v *Validator) ErrorOrNil() error {
if v.HasErrors() {
return v
}
return nil
}
// Sub allows to specific sub-validations.
//
// Errors from the subvalidation are merged with the top-level one, the keys are
// added as "top.sub" or "top[n].sub".
//
// If the error is not a Validator the text will be added as just the key name
// without subkey (i.e. the same as v.Append("key", "msg")).
//
// For example:
//
// v := validate.New()
// v.Required("name", customer.Name)
//
// // e.g. "settings.domain"
// v.Sub("settings", -1, customer.Settings.Validate())
//
// // e.g. "addresses[1].city"
// for i, a := range customer.Addresses {
// a.Sub("addresses", i, c.Validate())
// }
func (v *Validator) Sub(key, subKey string, err error) {
if err == nil {
return
}
if subKey != "" {
key = fmt.Sprintf("%s[%s]", key, subKey)
}
sub, ok := err.(*Validator)
if !ok {
ss, ok := err.(Validator)
if !ok {
v.Append(key, err.Error())
return
}
sub = &ss
}
if !sub.HasErrors() {
return
}
for k, val := range sub.Errors {
mk := fmt.Sprintf("%s.%s", key, k)
v.Errors[mk] = append(v.Errors[mk], val...)
}
}
// Merge errors from another validator in to this one.
func (v *Validator) Merge(other Validator) {
for k, val := range other.Errors {
v.Errors[k] = append(v.Errors[k], val...)
}
}
// Strings representation shows either all errors or "<no errors>" if there are
// no errors.
func (v *Validator) String() string {
if !v.HasErrors() {
return "<no errors>"
}
// Make sure the order is always the same.
keys := make([]string, len(v.Errors))
i := 0
for k := range v.Errors {
keys[i] = k
i++
}
sort.Strings(keys)
var b strings.Builder
for _, k := range keys {
s := fmt.Sprintf("%s: %s.\n", k, strings.Join(v.Errors[k], ", "))
b.WriteString(s)
}
return b.String()
}
// Required indicates that this value must not be the type's zero value.
//
// Currently supported types are string, int, int64, uint, and uint64. It will
// panic if the type is not supported.
func (v *Validator) Required(key string, value interface{}, message ...string) {
msg := getMessage(message, MessageRequired)
switch val := value.(type) {
case string:
if strings.TrimSpace(val) == "" {
v.Append(key, msg)
}
case *string:
if val == nil || strings.TrimSpace(*val) == "" {
v.Append(key, msg)
}
case int:
if val == int(0) {
v.Append(key, msg)
}
case int64:
if val == int64(0) {
v.Append(key, msg)
}
case uint:
if val == uint(0) {
v.Append(key, msg)
}
case uint64:
if val == uint64(0) {
v.Append(key, msg)
}
case bool:
if !val {
v.Append(key, msg)
}
case mailaddress.Address:
if val.Address == "" {
v.Append(key, msg)
}
case mailaddress.List:
if len(val) == 0 {
v.Append(key, msg)
}
case []int64:
if len(val) == 0 {
v.Append(key, msg)
}
default:
vv := reflect.ValueOf(value)
if vv.Kind() == reflect.Ptr {
if value == reflect.Zero(vv.Type()).Interface() {
v.Append(key, msg)
}
return
}
if vv.Kind() == reflect.Slice {
if vv.Len() == 0 {
v.Append(key, msg)
return
}
for i := 0; i < vv.Len(); i++ {
if !vv.Index(i).IsZero() && !(vv.Kind() == reflect.Ptr && vv.Index(i).IsNil()) {
return
}
}
v.Append(key, msg)
return
}
panic(fmt.Sprintf("validate: not a supported type: %T", value))
}
}
// ExcludeInt64 validates that the value is not in the exclude list.
func (v *Validator) ExcludeInt64(key string, value int64, exclude []int64, message ...string) {
msg := getMessage(message, "")
for _, e := range exclude {
if e == value {
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageExclude, strconv.FormatInt(e, 10)))
}
return
}
}
}
// IncludeInt64 validates that the value is in the include list.
func (v *Validator) IncludeInt64(key string, value int64, include []int64, message ...string) {
if len(include) == 0 {
return
}
for _, e := range include {
if e == value {
return
}
}
msg := getMessage(message, "")
if msg != "" {
v.Append(key, msg)
} else {
var intStr []string
for _, e := range include {
intStr = append(intStr, strconv.FormatInt(e, 10))
}
v.Append(key, fmt.Sprintf(MessageInclude, strings.Join(intStr, ", ")))
}
}
// Exclude validates that the value is not in the exclude list.
//
// This list is matched case-insensitive.
func (v *Validator) Exclude(key, value string, exclude []string, message ...string) {
msg := getMessage(message, "")
value = strings.TrimSpace(strings.ToLower(value))
for _, e := range exclude {
if strings.ToLower(e) == value {
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageExclude, e))
}
return
}
}
}
// ExcludeWithSanitization sanitizes value using fs before validating that the value is not in the exclude list.
//
// This list is matched case-insensitive.
func (v *Validator) ExcludeWithSanitization(
key, value string,
exclude []string, message string,
fs ...func(string) string,
) {
for _, f := range fs {
value = f(value)
}
for _, e := range exclude {
if strings.EqualFold(e, value) {
if message != "" {
v.Append(key, message)
} else {
v.Append(key, fmt.Sprintf(MessageExclude, e))
}
return
}
}
}
// Include validates that the value is in the include list.
//
// This list is matched case-insensitive.
func (v *Validator) Include(key, value string, include []string, message ...string) {
if len(include) == 0 {
return
}
value = strings.TrimSpace(strings.ToLower(value))
for _, e := range include {
if strings.EqualFold(e, value) {
return
}
}
msg := getMessage(message, "")
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageInclude, strings.Join(include, ", ")))
}
}
// IncludeWithSanitization sanitizes value using fs before validating that the value is in the include list.
//
// This list is matched case-insensitive.
func (v *Validator) IncludeWithSanitization(
key, value string,
include []string, message string,
fs ...func(string) string,
) {
if len(include) == 0 {
return
}
for _, f := range fs {
value = f(value)
}
for _, e := range include {
if strings.EqualFold(e, value) {
return
}
}
if message != "" {
v.Append(key, message)
} else {
v.Append(key, fmt.Sprintf(MessageInclude, strings.Join(include, ", ")))
}
}
// Domain validates that the domain is valid.
//
// A domain must consist of at least two labels. So "com" or "localhost" – while
// technically valid domain names – are not accepted, whereas "example.com" or
// "me.localhost" are. For the overwhelming majority of applications this makes
// the most sense.
//
// This works for internationalized domain names (IDN), either as UTF-8
// characters or as punycode.
//
// Limitation: the RFC limits domain labels to 63 bytes, but this validation
// accepts labels up to 63 *characters*.
func (v *Validator) Domain(key, value string, message ...string) {
if value == "" {
return
}
msg := getMessage(message, MessageDomain)
if !validDomain(value) {
v.Append(key, msg)
}
}
var reValidDomain = regexp.MustCompile(`` +
// Anchor
`^` +
// See RFC 1034, section 3.1, RFC 1035, secion 2.3.1
//
// - Only allow letters, numbers
// - Max size of a single label is 63 characters (RFC specifies bytes, but that's
// not so easy to check AFAIK).
// - Need at least two labels
`[\p{L}\d-]{1,63}` + // Label
`(\.[\p{L}\d-]{1,63})+` + // More labels
// Anchor
`$`,
)
func validDomain(v string) bool {
return reValidDomain.MatchString(v)
}
// URL validates that the string contains a valid URL.
//
// The URL may consist of a scheme, host, path, and query parameters. Only the
// host is required.
//
// The host is validated with the Domain() validation.
//
// If the scheme is not given "http" will be prepended.
func (v *Validator) URL(key, value string, message ...string) *url.URL {
if value == "" {
return nil
}
msg := getMessage(message, MessageURL)
u, err := url.Parse(value)
if err != nil && u == nil {
v.Append(key, "%s: %s", msg, err)
return nil
}
// If we don't have a scheme the parse may or may not fail according to the
// go docs. "Trying to parse a hostname and path without a scheme is invalid
// but may not necessarily return an error, due to parsing ambiguities."
if u.Scheme == "" {
u.Scheme = "http"
u, err = url.Parse(u.String())
}
if err != nil {
v.Append(key, "%s: %s", msg, err)
return nil
}
if u.Host == "" {
v.Append(key, msg)
return nil
}
host := u.Host
if h, _, err := net.SplitHostPort(u.Host); err == nil {
host = h
}
if !validDomain(host) {
v.Append(key, msg)
return nil
}
return u
}
// Email validates if this email looks like a valid email address.
func (v *Validator) Email(key, value string, message ...string) mailaddress.Address {
if value == "" {
return mailaddress.Address{}
}
msg := getMessage(message, MessageEmail)
addr, err := mailaddress.Parse(value)
if err != nil {
v.Append(key, msg)
}
return addr
}
// IPv4 validates that a string is a valid IPv4 address.
func (v *Validator) IPv4(key, value string, message ...string) net.IP {
if value == "" {
return net.IP{}
}
msg := getMessage(message, MessageIPv4)
ip := net.ParseIP(value)
if ip == nil || ip.To4() == nil {
v.Append(key, msg)
}
return ip
}
var reValidHexColor = regexp.MustCompile(`(?i)^#[0-9a-f]{3,6}$`)
// HexColor validates if the string looks like a color as a hex triplet (e.g.
// #ffffff or #fff).
func (v *Validator) HexColor(key, value string, message ...string) {
if value == "" {
return
}
msg := getMessage(message, MessageHexColor)
if !reValidHexColor.MatchString(value) {
v.Append(key, msg)
}
}
// Len sets the minimum and maximum length for a string in characters, not in
// bytes.
//
// A maximum of 0 indicates there is no upper limit.
func (v *Validator) Len(key, value string, min, max int, message ...string) {
msg := getMessage(message, "")
length := utf8.RuneCountInString(value)
switch {
case length < min:
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageLenLonger, min))
}
case max > 0 && length > max:
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageLenShorter, max))
}
}
}
// Integer checks if this looks like an integer (i.e. a whole number).
func (v *Validator) Integer(key, value string, message ...string) int64 {
if value == "" {
return 0
}
i, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
if err != nil {
v.Append(key, getMessage(message, MessageInteger))
}
return i
}
// Boolean checks if this looks like a boolean value.
func (v *Validator) Boolean(key, value string, message ...string) bool {
if value == "" {
return false
}
switch strings.ToLower(value) {
case "1", "y", "yes", "t", "true":
return true
case "0", "n", "no", "f", "false":
return false
}
v.Append(key, getMessage(message, MessageBool))
return false
}
// Date checks if the string looks like a date in the given layout.
func (v *Validator) Date(key, value, layout string, message ...string) {
msg := getMessage(message, "")
_, err := time.Parse(layout, value)
if err != nil {
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageDate, layout))
}
}
}
var rePhone = regexp.MustCompile(`^[0123456789+\-() .]{5,20}$`)
// Phone checks if the string looks like a valid phone number.
//
// There are a great amount of writing conventions for phone numbers:
// https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers
//
// This merely checks a field contains 5 to 20 characters "0123456789+\-() .",
// which is not very strict but should cover all conventions.
func (v *Validator) Phone(key, value string, message ...string) {
if value == "" {
return
}
msg := getMessage(message, MessagePhone)
if !rePhone.MatchString(value) {
v.Append(key, msg)
}
}
// Range sets the minimum and maximum value of a integer.
//
// A maximum of 0 indicates there is no upper limit.
func (v *Validator) Range(key string, value, min, max int64, message ...string) {
msg := getMessage(message, "")
if value < min {
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageRangeHigher, min))
}
}
if max > 0 && value > max {
if msg != "" {
v.Append(key, msg)
} else {
v.Append(key, fmt.Sprintf(MessageRangeLower, max))
}
}
}
// Equal returns whether two validators are equal.
func (v *Validator) Equal(o *Validator) bool {
if v == nil && o == nil {
return true
}
if v == nil && o != nil {
return len(o.Errors) == 0
}
if v != nil && o == nil {
return len(v.Errors) == 0
}
if len(v.Errors) != len(o.Errors) {
return false
}
for k, vmsgs := range v.Errors {
omsgs, ok := o.Errors[k]
if !ok {
return false
}
if len(vmsgs) != len(omsgs) {
return false
}
NEXT:
for _, vmsg := range vmsgs {
for _, omsg := range omsgs {
if vmsg == omsg {
continue NEXT
}
}
return false
}
}
return true
}