-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator.go
1147 lines (1007 loc) · 34.6 KB
/
validator.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mango
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
// ValidationFailure holds details about a validation failure. Code will
// contain the validator type and Message a user friendly description of
// the reason for the failure.
type ValidationFailure struct {
Code string `json:"code"`
Message string `json:"message"`
}
// Validator is the interface that wraps the basic Validate method.
// Validators are used to validate models in addition to sections of a URL path
// which match the pattern of an entry in the routing tree.
type Validator interface {
// Validate tests if val matches the validation rules. The validation test
// may involve constraint specific args.
Validate(val interface{}, args []string) bool
// Type returns the constraint name used in routing patterns.
// ValidationHandler will use this to locate the correct validator.
Type() string
// FailureMsg returns a string with a readable message about the validation failure.
FailureMsg() string
}
// ValidationHandler is the interface for a collection of Validators.
// The IsValid method can be used to validate a model property or checking a
// route parameter value against its constraint.
// New validators can be added individually using AddValidator or as a
// collection using AddValidators.
type ValidationHandler interface {
AddValidator(v Validator)
AddValidators(validators []Validator)
IsValid(val interface{}, constraints string) ([]ValidationFailure, bool)
ParseConstraints(constraints string) map[string][]string
}
type elementValidationHandler struct {
validators map[string]Validator
}
func (r *elementValidationHandler) AddValidator(v Validator) {
if _, ok := r.validators[v.Type()]; ok {
panic("conflicting constraint type: " + v.Type())
}
r.validators[v.Type()] = v
}
func (r *elementValidationHandler) AddValidators(validators []Validator) {
for _, v := range validators {
r.AddValidator(v)
}
}
func (r *elementValidationHandler) IsValid(val interface{}, constraints string) (fails []ValidationFailure, ok bool) {
// Split constraints at commas, but need to consider some may
// have parameters which also have commas, e.g. range(3,8).
tests := r.ParseConstraints(constraints)
for name, args := range tests {
// ignorecontents is a special case instruction rather than constraint
if name == "ignorecontents" {
continue
}
var v Validator
v, ok = r.validators[name]
if !ok {
panic(fmt.Sprintf("unknown constraint: %s", name))
}
if !v.Validate(val, args) {
if len(args) > 0 {
name += "(" + strings.Join(args, ",") + ")"
}
fails = append(fails, ValidationFailure{name, v.FailureMsg()})
}
}
ok = len(fails) == 0
return
}
func (r *elementValidationHandler) ParseConstraints(constraints string) map[string][]string {
results := make(map[string][]string)
brace := 0
args := []string{}
buf := make([]byte, len(constraints))
b := 0
name := ""
for i := 0; i < len(constraints); i++ {
if constraints[i] == '(' {
brace++
}
if constraints[i] == ')' {
brace--
if brace < 0 {
panic(fmt.Sprintf("illegal constraint format: %s", constraints))
}
continue
}
if constraints[i] == ',' || constraints[i] == '(' {
arg := strings.TrimSpace(string(buf[:b]))
if name == "" {
if arg == "" {
panic(fmt.Sprintf("illegal constraint format: %s", constraints))
}
name = arg
} else {
args = append(args, arg)
}
b = 0
if brace == 0 {
// Must be between constraints.
// Add what we have and reset.
results[name] = args
name = ""
args = []string{}
}
continue
}
buf[b] = constraints[i]
b++
}
arg := strings.TrimSpace(string(buf[:b]))
if name == "" {
name = arg
} else {
args = append(args, arg)
}
if name != "" {
results[name] = args
}
if brace != 0 {
panic(fmt.Sprintf("illegal constraint format: %s", constraints))
}
return results
}
func newValidationHandler() ValidationHandler {
v := elementValidationHandler{}
v.validators = make(map[string]Validator)
v.AddValidators(getDefaultValidators())
return &v
}
// EmptyValidator is the default validator used to validate parameters where
// no constraint has been stipulated. It returns true in all cases
type EmptyValidator struct{}
// Validate returns true in all cases. This is the default validator.
func (v *EmptyValidator) Validate(val interface{}, args []string) bool {
return true
}
// Type returns the constraint name. This is an empty string to
// ensure this validator is selected when no constraint has been
// specified in the route pattern parameter.
func (v *EmptyValidator) Type() string {
return ""
}
// FailureMsg returns a string with a readable message about the validation failure.
// As this validator never fails, this method just returns an empty string.
func (v *EmptyValidator) FailureMsg() string {
return ""
}
// Int32Validator tests for 32 bit integer values.
type Int32Validator struct{}
// Validate tests for 32 bit integer values.
// Returns true if val is a string containing an integer in the range -2147483648 to 2147483647
// Validate panics if val is not a string.
func (v *Int32Validator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("int32 validator can only validate strings not, %T", val))
}
_, err := strconv.ParseInt(s, 10, 32)
return err == nil
}
// Type returns the constraint name (int32).
func (v *Int32Validator) Type() string {
return "int32"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *Int32Validator) FailureMsg() string {
return "must be a 32 bit integer."
}
// Int64Validator tests for 64 bit integer values.
type Int64Validator struct{}
// Validate tests for 64 bit integer values.
// Returns true if val is a string containing an integer in the range -9223372036854775808 to 9223372036854775807
// Validate panics if val is not a string.
func (v *Int64Validator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("int64 validator can only validate strings not, %T", val))
}
_, err := strconv.ParseInt(s, 10, 64)
return err == nil
}
// Type returns the constraint name (int64).
func (v *Int64Validator) Type() string {
return "int64"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *Int64Validator) FailureMsg() string {
return "must be a 64 bit integer."
}
// AlphaValidator tests for a sequence containing only alpha characters.
type AlphaValidator struct{}
// Validate tests for alpha values.
// Returns true if val is a string containing only characters in the ranges a-z or A-Z.
// Validate panics if val is not a string.
func (v *AlphaValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("alpha validator can only validate strings not, %T", val))
}
re := regexp.MustCompile(`^[a-zA-Z]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (alpha).
func (v *AlphaValidator) Type() string {
return "alpha"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *AlphaValidator) FailureMsg() string {
return "must contain only alpha characters."
}
// DigitsValidator tests for a sequence of digits.
type DigitsValidator struct{}
// Validate tests for digit values.
// Returns true if val is a string containing only digits 0-9.
// Validate panics if val is not a string.
func (v *DigitsValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("digits validator can only validate strings not, %T", val))
}
re := regexp.MustCompile(`^\d+$`)
return re.MatchString(s)
}
// Type returns the constraint name (digits).
func (v *DigitsValidator) Type() string {
return "digits"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *DigitsValidator) FailureMsg() string {
return "must contain only digit characters."
}
// Hex32Validator tests for 32 bit hex values.
type Hex32Validator struct{}
// Validate tests for 32 bit hex values.
// Returns true if val is a hexadecimal string in the range -80000000 to 7FFFFFFF.
// The test is not case sensitive, i.e. 3ef42bc7 and 3EF42BC7 will both return true.
// Validate panics if val is not a string.
func (v *Hex32Validator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("hex32 validator can only validate strings not, %T", val))
}
_, err := strconv.ParseInt(s, 16, 32)
return err == nil
}
// Type returns the constraint name (hex32).
func (v *Hex32Validator) Type() string {
return "hex32"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *Hex32Validator) FailureMsg() string {
return "must be a 32 bit hexadecimal value."
}
// Hex64Validator tests for 64 bit hex values.
type Hex64Validator struct{}
// Validate tests for 64 bit hex values.
// Returns true if val is a hexadecimal string in the range -8000000000000000 to 7FFFFFFFFFFFFFFF.
// The test is not case sensitive, i.e. 3ef42bc7 and 3EF42BC7 will both return true.
// Validate panics if val is not a string.
func (v *Hex64Validator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("hex64 validator can only validate strings not, %T", val))
}
_, err := strconv.ParseInt(s, 16, 64)
return err == nil
}
// Type returns the constraint name (hex64).
func (v *Hex64Validator) Type() string {
return "hex64"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *Hex64Validator) FailureMsg() string {
return "must be a 64 bit hexadecimal value."
}
// HexValidator tests for a sequence of hexadecimal characters.
type HexValidator struct{}
// Validate tests for hex values.
// Returns true if if val is a string containing only hex characters, (i.e. 0-9, a-e, A-F).
// Validate panics if val is not a string.
func (v *HexValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("hex validator can only validate strings not, %T", val))
}
re := regexp.MustCompile(`^[0-9a-fA-F]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (hex).
func (v *HexValidator) Type() string {
return "hex"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *HexValidator) FailureMsg() string {
return "must contain only hexadecimal characters."
}
// UUIDValidator tests for UUIDs.
type UUIDValidator struct{}
// Validate tests for UUID values.
// Returns true if val is a string in one of the following formats:
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
// (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
// (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
// XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
// (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// {XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}
// (XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX)
//
// where X and x represent upper and lowercase hexadecimal values respectively.
//
// Valid UUID examples:
// {58D5E212-165B-4CA0-909B-C86B9CEE0111}
// {58d5e212-165b-4ca0-909b-c86b9cee0111}
// 58D5E212165B4CA0909BC86B9CEE0111
//
// Validate panics if val is not a string.
func (v *UUIDValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("uuid validator can only validate strings not, %T", val))
}
str := `^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$`
re := regexp.MustCompile(str)
if !re.MatchString(s) {
return false
}
// ensure if we start or finish with a bookend, there is a matching one
switch s[0] {
case '{':
return s[len(s)-1] == '}'
case '(':
return s[len(s)-1] == ')'
}
return s[len(s)-1] != ')' && s[len(s)-1] != '}'
}
// Type returns the constraint name (uuid).
func (v *UUIDValidator) Type() string {
return "uuid"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *UUIDValidator) FailureMsg() string {
return "must be a valid UUID."
}
// AlphaNumValidator tests for a sequence containing only alphanumeric characters.
type AlphaNumValidator struct{}
// Validate tests for alphanumeric values.
// Returns true if val is a string containing only characters in the ranges a-z, A-Z or 0-9.
// Validate panics if val is not a string.
func (v *AlphaNumValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("alphanum validator can only validate strings not, %T", val))
}
re := regexp.MustCompile(`^[a-zA-Z0-9]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (alphanum).
func (v *AlphaNumValidator) Type() string {
return "alphanum"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *AlphaNumValidator) FailureMsg() string {
return "must contain only alphanumeric characters."
}
// SentenceValidator tests for a sequence containing only characters normally found
// in a sentence.
type SentenceValidator struct{}
// Validate tests for characters normally found in a sentence.
// Returns true if val is a string containing only characters in the ranges a-z,
// A-Z or 0-9, plus spaces and punctuation characters ,.:;!? single and double quotes,
// braces and hyphens. Note that underscores are not included in the permissble set.
// Validate panics if val is not a string.
func (v *SentenceValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("sentence validator can only validate strings not, %T", val))
}
re := regexp.MustCompile(`^[a-zA-Z0-9 ,.()'":;!?-]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (sentence).
func (v *SentenceValidator) Type() string {
return "sentence"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *SentenceValidator) FailureMsg() string {
return "must contain only characters found in a sentence."
}
// NotWhitespaceValidator tests that a string does not comprise only whitespace characters.
type NotWhitespaceValidator struct{}
// Validate tests for whitespace.
// Returns true if val is not a string containing only whitespace characters.
// Validate panics if val is not a string.
func (v *NotWhitespaceValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("notwhitespace validator can only validate strings not, %T", val))
}
return len(s) == 0 || len(strings.Fields(s)) > 0
}
// Type returns the constraint name (notwhitespace).
func (v *NotWhitespaceValidator) Type() string {
return "notwhitespace"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *NotWhitespaceValidator) FailureMsg() string {
return "must not contain only whitespace characters."
}
// NotEmptyOrWhitespaceValidator tests that a string is not empty and does not comprise only whitespace characters.
type NotEmptyOrWhitespaceValidator struct{}
// Validate tests for whitespace or empty string.
// Returns true if val is not an empty string or a string containing only whitespace characters.
// Validate panics if val is not a string.
func (v *NotEmptyOrWhitespaceValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("notemptyorwhitespace validator can only validate strings not, %T", val))
}
return len(strings.Fields(s)) > 0
}
// Type returns the constraint name (notemptyorwhitespace).
func (v *NotEmptyOrWhitespaceValidator) Type() string {
return "notemptyorwhitespace"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *NotEmptyOrWhitespaceValidator) FailureMsg() string {
return "must contain at least one non-whitespace character."
}
// PhoneValidator tests for a telephone number.
type PhoneValidator struct{}
// Validate tests for phone number.
// Returns true if val is a string containing only characters found in a telephone
// number. e.g. +44 1752 123456, (1752) 123456 or 01752 123456
// Validate panics if val is not a string.
func (v *PhoneValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("phone validator can only validate strings not, %T", val))
}
re := regexp.MustCompile(`^(\+[0-9]+)?\s?((\([0-9]+\))|([0-9]+))?\s?[0-9]+\s?[0-9]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (phone).
func (v *PhoneValidator) Type() string {
return "phone"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *PhoneValidator) FailureMsg() string {
return "must contain a telephone number."
}
// PrefixValidator tests for a specified prefix.
type PrefixValidator struct {
prefix string
}
// Validate tests for a prefix.
// Returns true if val is a string starting with the prefix specified in params.
// Validate panics if val is not a string.
func (v *PrefixValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("prefix validator can only validate strings not, %T", val))
}
v.prefix = ""
if len(params) > 0 {
v.prefix = params[0]
}
return strings.HasPrefix(s, v.prefix)
}
// Type returns the constraint name (prefix).
func (v *PrefixValidator) Type() string {
return "prefix"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *PrefixValidator) FailureMsg() string {
return fmt.Sprintf("must have the prefix %q.", v.prefix)
}
// SuffixValidator tests for a specified suffix.
type SuffixValidator struct {
suffix string
}
// Validate tests for a suffix.
// Returns true if val is a string ending with the suffix specified in params.
// Validate panics if val is not a string.
func (v *SuffixValidator) Validate(val interface{}, params []string) bool {
s, ok := val.(string)
if !ok {
panic(fmt.Sprintf("suffix validator can only validate strings not, %T", val))
}
v.suffix = ""
if len(params) > 0 {
v.suffix = params[0]
}
return strings.HasSuffix(s, v.suffix)
}
// Type returns the constraint name (suffix).
func (v *SuffixValidator) Type() string {
return "suffix"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *SuffixValidator) FailureMsg() string {
return fmt.Sprintf("must have the suffix %q.", v.suffix)
}
// MinValidator tests for a minumum numeric value.
type MinValidator struct {
min string
}
// Validate tests for a minimum numerical value.
// Returns true if val is a number greater or equal to the value specified in params.
// Validate panics if val is not a number or supplied params argument is not a number.
func (v *MinValidator) Validate(val interface{}, params []string) bool {
number, ok := normalizeNumber(val)
if !ok {
panic(fmt.Sprintf("min validator can only validate numbers not, %T", val))
}
v.min = ""
if len(params) == 0 {
panic("missing parameter for MinValidator")
}
v.min = params[0]
switch reflect.TypeOf(number).Kind() {
case reflect.Int64:
p, err := strconv.ParseInt(params[0], 10, 64)
if err == nil {
return number.(int64) >= p
}
case reflect.Uint64:
p, err := strconv.ParseUint(params[0], 10, 64)
if err == nil {
return number.(uint64) >= p
}
case reflect.Float64:
p, err := strconv.ParseFloat(params[0], 64)
if err == nil {
return number.(float64) >= p
}
}
panic("non-numerical parameter used in MinValidator")
}
// Type returns the constraint name (min).
func (v *MinValidator) Type() string {
return "min"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *MinValidator) FailureMsg() string {
return fmt.Sprintf("must not be less than %v.", v.min)
}
// MaxValidator tests for a maxumum numeric value.
type MaxValidator struct {
max string
}
// Validate tests for a maximum numerical value.
// Returns true if val is a number lower or equal to the value specified in params.
// Validate panics if val is not a number or supplied params argument is not a number.
func (v *MaxValidator) Validate(val interface{}, params []string) bool {
number, ok := normalizeNumber(val)
if !ok {
panic(fmt.Sprintf("max validator can only validate numbers not, %T", val))
}
v.max = ""
if len(params) == 0 {
panic("missing parameter for MaxValidator")
}
v.max = params[0]
switch reflect.TypeOf(number).Kind() {
case reflect.Int64:
p, err := strconv.ParseInt(params[0], 10, 64)
if err == nil {
return number.(int64) <= p
}
case reflect.Uint64:
p, err := strconv.ParseUint(params[0], 10, 64)
if err == nil {
return number.(uint64) <= p
}
case reflect.Float64:
p, err := strconv.ParseFloat(params[0], 64)
if err == nil {
return number.(float64) <= p
}
}
panic("non-numerical parameter used in MaxValidator")
}
// Type returns the constraint name (max).
func (v *MaxValidator) Type() string {
return "max"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *MaxValidator) FailureMsg() string {
return fmt.Sprintf("must not be greater than %v.", v.max)
}
// RangeValidator tests for a numerical value in a given range.
type RangeValidator struct {
min string
max string
}
// Validate tests for a numerical value in a given range.
// Returns true if val is a number between the lower and upper limits specified in params.
// Example: range(2,4) - returns true if input is between 2 and 4 (inclusive).
// RangeValidator accepts all numeric types, e.g. range(3.123, 23456.89).
// Validate panics if val is not a number or supplied params arguments are not a numbers.
func (v *RangeValidator) Validate(val interface{}, params []string) bool {
number, ok := normalizeNumber(val)
if !ok {
panic(fmt.Sprintf("range validator can only validate numbers not, %T", val))
}
v.min = ""
v.max = ""
if len(params) != 2 {
panic("missing parameters for RangeValidator")
}
v.min = params[0]
v.max = params[1]
switch reflect.TypeOf(number).Kind() {
case reflect.Int64:
l, errl := strconv.ParseInt(params[0], 10, 64)
u, erru := strconv.ParseInt(params[1], 10, 64)
if errl != nil || erru != nil {
break
}
return number.(int64) >= l && number.(int64) <= u
case reflect.Uint64:
l, errl := strconv.ParseUint(params[0], 10, 64)
u, erru := strconv.ParseUint(params[1], 10, 64)
if errl != nil || erru != nil {
break
}
return number.(uint64) >= l && number.(uint64) <= u
case reflect.Float64:
l, errl := strconv.ParseFloat(params[0], 64)
u, erru := strconv.ParseFloat(params[1], 64)
if errl != nil || erru != nil {
break
}
return number.(float64) >= l && number.(float64) <= u
}
panic("non-numerical parameters used in RangeValidator")
}
// Type returns the constraint name (range).
func (v *RangeValidator) Type() string {
return "range"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *RangeValidator) FailureMsg() string {
return fmt.Sprintf("must be between %v and %v.", v.min, v.max)
}
// LenMinValidator tests for a minimim length of String, Array, Slice or Map.
type LenMinValidator struct {
min string
}
// Validate tests for a minimim length.
// Returns true if length of val is greater or equal to the value specified in params.
// Validate panics if val is not a String, Array, Slice or Map, or if supplied params argument is not an integer.
func (v *LenMinValidator) Validate(val interface{}, params []string) bool {
if len(params) == 0 {
panic("missing parameter for LenMinValidator")
}
v.min = params[0]
l, err := strconv.Atoi(params[0])
if err != nil {
panic("non-integer parameter used in LenMinValidator")
}
switch reflect.TypeOf(val).Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return reflect.ValueOf(val).Len() >= l
default:
panic(fmt.Sprintf("lenmin validator can only validate strings, arrays, slices and maps, not %T", val))
}
}
// Type returns the constraint name (lenmin).
func (v *LenMinValidator) Type() string {
return "lenmin"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *LenMinValidator) FailureMsg() string {
return fmt.Sprintf("must contain at least %v elements.", v.min)
}
// LenMaxValidator tests for a maximum length of String, Array, Slice or Map.
type LenMaxValidator struct {
max string
}
// Validate tests for a maximum length.
// Returns true if length of val is lower or equal to the value specified in params.
// Validate panics if val is not a String, Array, Slice or Map, or if supplied params argument is not an integer.
func (v *LenMaxValidator) Validate(val interface{}, params []string) bool {
if len(params) == 0 {
panic("missing parameter for LenMaxValidator")
}
v.max = params[0]
u, err := strconv.Atoi(params[0])
if err != nil {
panic("non-integer parameter used in LenMaxValidator")
}
switch reflect.TypeOf(val).Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return reflect.ValueOf(val).Len() <= u
default:
panic(fmt.Sprintf("lenmax validator can only validate strings, arrays, slices and maps, not %T", val))
}
}
// Type returns the constraint name (lenmax).
func (v *LenMaxValidator) Type() string {
return "lenmax"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *LenMaxValidator) FailureMsg() string {
return fmt.Sprintf("must not contain more than %v elements.", v.max)
}
// LenRangeValidator tests for length of String, Array, Slice or Map, in a given range.
type LenRangeValidator struct {
min string
max string
}
// Validate tests for a length in a given range.
// Returns true if length of val is between the lower and upper limits specified in params.
// Validate panics if val is not a String, Array, Slice or Map, or if supplied params arguments are not an integer.
func (v *LenRangeValidator) Validate(val interface{}, params []string) bool {
if len(params) != 2 {
panic("missing parameters for LenRangeValidator")
}
v.min = params[0]
v.max = params[1]
l, errl := strconv.Atoi(params[0])
u, erru := strconv.Atoi(params[1])
if errl != nil || erru != nil {
panic("non-integer parameters used in LenRangeValidator")
}
switch reflect.TypeOf(val).Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
lenval := reflect.ValueOf(val).Len()
return lenval >= l && lenval <= u
default:
panic(fmt.Sprintf("lenrange validator can only validate strings, arrays, slices and maps, not %T", val))
}
}
// Type returns the constraint name (lenrange).
func (v *LenRangeValidator) Type() string {
return "lenrange"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *LenRangeValidator) FailureMsg() string {
return fmt.Sprintf("must contain between %v and %v elements.", v.min, v.max)
}
// ContainsValidator tests whether a container holds a specific string.
type ContainsValidator struct {
required string
}
// Validate tests for a existence of a string within another string, Array, Slice
// or Map (keys).
// Returns true if val is a String, Array, Slice or Map containing the string
// specified in params. Contains is case-sensitive.
// Validate panics if val is not a String, Array, Slice or Map.
func (v *ContainsValidator) Validate(val interface{}, params []string) bool {
v.required = ""
if len(params) > 0 {
v.required = params[0]
}
rv := reflect.ValueOf(val)
switch rv.Kind() {
case reflect.Map:
for _, key := range rv.MapKeys() {
el := reflect.ValueOf(key.Interface())
if el.Kind() == reflect.Ptr {
el = el.Elem()
}
if el.Kind() != reflect.String {
panic(fmt.Sprintf("contains validator can only validate maps with keys of string, not %T", key.Interface()))
}
if el.String() == v.required {
return true
}
}
return false
case reflect.Array, reflect.Slice:
for i := 0; i < rv.Len(); i++ {
el := rv.Index(i)
if el.Kind() == reflect.Ptr {
el = el.Elem()
}
if el.Kind() != reflect.String {
panic(fmt.Sprintf("contains validator can only validate arrays and slices of string, not %T", el.Interface()))
}
if el.String() == v.required {
return true
}
}
return false
case reflect.String:
return strings.Contains(val.(string), v.required)
default:
panic(fmt.Sprintf("contains validator can only validate strings, arrays, slices and maps, not %T", val))
}
}
// Type returns the constraint name (contains).
func (v *ContainsValidator) Type() string {
return "contains"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *ContainsValidator) FailureMsg() string {
return fmt.Sprintf("must contain %q.", v.required)
}
// InSetValidator tests a value is in a set.
type InSetValidator struct {
set string
}
// Validate tests for a value within a set of values.
// Returns true if val is a string or int within the set specified in params.
// Validate panics if val is not a string or int.
func (v *InSetValidator) Validate(val interface{}, params []string) bool {
v.set = strings.Join(params, ", ")
switch reflect.TypeOf(val).Kind() {
case reflect.String:
for _, p := range params {
s := strings.TrimSpace(p)
if val.(string) == s {
return true
}
}
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
v := reflect.ValueOf(val).Int()
for _, p := range params {
s := strings.TrimSpace(p)
i, err := strconv.ParseInt(s, 10, 64) //strconv.Atoi(s)
if err != nil {
panic("non-integer parameter used in InSetValidator")
}
if v == i {
return true
}
}
default:
panic(fmt.Sprintf("inset validator can only validate strings and ints, not %T", val))
}
return false
}
// Type returns the constraint name (inset).
func (v *InSetValidator) Type() string {
return "inset"
}
// FailureMsg returns a string with a readable message about the validation failure.
func (v *InSetValidator) FailureMsg() string {
return fmt.Sprintf("must be in the set [%s].", v.set)
}
// NotEmptyValidator tests for an empty String, Array, Slice or Map.
type NotEmptyValidator struct{}
// Validate tests for an empty String, Array, Slice or Map.
// Returns true if val is String, Array, Slice or Map with elements.
// Equivlent to (and shorthand for) minlen(1).
// Validate panics if val is not a String, Array, Slice or Map.
func (v *NotEmptyValidator) Validate(val interface{}, params []string) bool {
switch reflect.TypeOf(val).Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return reflect.ValueOf(val).Len() > 0
default:
panic(fmt.Sprintf("notempty validator can only validate strings, arrays, slices and maps, not %T", val))
}
}
// Type returns the constraint name (notempty).
func (v *NotEmptyValidator) Type() string {
return "notempty"