-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconstraints_string.go
More file actions
809 lines (735 loc) · 29.1 KB
/
constraints_string.go
File metadata and controls
809 lines (735 loc) · 29.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
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
package valix
import (
"encoding/json"
"regexp"
"strings"
"unicode"
)
// StringCharacters constraint to check that a string contains only allowable characters (and does not contain any disallowed characters)
type StringCharacters struct {
// the ranges of characters (runes) that are allowed - if this slice is non-empty, each character must be in at least one of these
AllowRanges []unicode.RangeTable
// the named ranges of characters (runes) that are allowed - if this slice is non-empty, each character must be in at least one of these
//
// The named ranges can be:
//
// * "BMP", "SMP" or "SIP" (Basic Multilingual Plane, Supplementary Multilingual Plane or Supplementary Ideographic Plane respectively)
//
// * any name from unicode.Categories prefixed with "Category-"
//
// * any name from unicode.Scripts prefixed with "Script-"
//
// * any name from unicode.Properties prefixed with "Property-"
//
// * any name from unicode.FoldCategory prefixed with "FoldCategory-"
//
// * any name from unicode.FoldScript prefixed with "FoldScript-"
NamedAllowRanges []string
// the ranges of characters (runes) that are not allowed - if any character
// is in any of these ranges then the constraint is violated
DisallowRanges []unicode.RangeTable
// the named ranges of characters (runes) that are not allowed - if any character
// is in any of these ranges then the constraint is violated
//
// The named ranges can be:
//
// * "BMP", "SMP" or "SIP" (Basic Multilingual Plane, Supplementary Multilingual Plane or Supplementary Ideographic Plane respectively)
//
// * any name from unicode.Categories prefixed with "Category-"
//
// * any name from unicode.Scripts prefixed with "Script-"
//
// * any name from unicode.Properties prefixed with "Property-"
//
// * any name from unicode.FoldCategory prefixed with "FoldCategory-"
//
// * any name from unicode.FoldScript prefixed with "FoldScript-"
NamedDisallowRanges []string
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringCharacters) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringCharacters) checkString(str string, vcx *ValidatorContext) bool {
la := len(c.AllowRanges) > 0 || len(c.NamedAllowRanges) > 0
ld := len(c.DisallowRanges) > 0 || len(c.NamedDisallowRanges) > 0
if la || ld {
runes := []rune(str)
for _, r := range runes {
for _, dr := range c.DisallowRanges {
if unicode.Is(&dr, r) {
return false
}
}
for _, dn := range c.NamedDisallowRanges {
if dr, ok := lookupRangeTableName(dn); !ok || (ok && unicode.Is(dr, r)) {
return false
}
}
if la && !c.inAllowed(r) {
return false
}
}
}
return true
}
func (c *StringCharacters) inAllowed(r rune) bool {
for _, ar := range c.AllowRanges {
if unicode.Is(&ar, r) {
return true
}
}
for _, an := range c.NamedAllowRanges {
if ar, ok := lookupRangeTableName(an); ok && unicode.Is(ar, r) {
return true
}
}
return false
}
// GetMessage implements the Constraint.GetMessage
func (c *StringCharacters) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgInvalidCharacters)
}
// StringContains constraint to check that a string contains with a given value
type StringContains struct {
// the value to check that the string contains
Value string `v8n:"default"`
// multiple additional values that the string may contain
Values []string
// whether the check is case-insensitive (by default, the check is case-sensitive)
CaseInsensitive bool
// whether the check is NOT-ed (i.e. checks that the string does not contain)
Not bool
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringContains) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringContains) checkString(str string, vcx *ValidatorContext) bool {
ckStr := caseInsensitive(str, c.CaseInsensitive)
contains := c.Value != "" && strings.Contains(ckStr, caseInsensitive(c.Value, c.CaseInsensitive))
if !contains {
for _, s := range c.Values {
contains = s != "" && strings.Contains(ckStr, caseInsensitive(s, c.CaseInsensitive))
if contains {
break
}
}
}
return (!c.Not && contains) || (c.Not && !contains)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringContains) GetMessage(tcx I18nContext) string {
possibles := make([]string, 0)
if c.Value != "" {
possibles = append(possibles, "'"+c.Value+"'")
}
for _, s := range c.Values {
if s != "" {
possibles = append(possibles, "'"+s+"'")
}
}
possiblesStr := strings.Join(possibles, ",")
if c.Not {
return defaultMessage(tcx, c.Message, fmtMsgStringNotContains, possiblesStr)
}
return defaultMessage(tcx, c.Message, fmtMsgStringContains, possiblesStr)
}
// StringEndsWith constraint to check that a string ends with a given suffix
type StringEndsWith struct {
// the value to check that the string ends with
Value string `v8n:"default"`
// multiple additional values that the string may end with
Values []string
// whether the check is case-insensitive (by default, the check is case-sensitive)
CaseInsensitive bool
// whether the check is NOT-ed (i.e. checks that the string does not end with)
Not bool
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringEndsWith) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringEndsWith) checkString(str string, vcx *ValidatorContext) bool {
ckStr := caseInsensitive(str, c.CaseInsensitive)
endsWith := c.Value != "" && strings.HasSuffix(ckStr, caseInsensitive(c.Value, c.CaseInsensitive))
if !endsWith {
for _, s := range c.Values {
endsWith = s != "" && strings.HasSuffix(ckStr, caseInsensitive(s, c.CaseInsensitive))
if endsWith {
break
}
}
}
return (!c.Not && endsWith) || (c.Not && !endsWith)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringEndsWith) GetMessage(tcx I18nContext) string {
possibles := make([]string, 0)
if c.Value != "" {
possibles = append(possibles, "'"+c.Value+"'")
}
for _, s := range c.Values {
if s != "" {
possibles = append(possibles, "'"+s+"'")
}
}
possiblesStr := strings.Join(possibles, ",")
if c.Not {
return defaultMessage(tcx, c.Message, fmtMsgStringNotEndsWith, possiblesStr)
}
return defaultMessage(tcx, c.Message, fmtMsgStringEndsWith, possiblesStr)
}
// StringExactLength constraint to check that a string has an exact length
type StringExactLength struct {
// the exact length expected
Value int `v8n:"default"`
// if set to true, uses the rune length (true Unicode length) to check length of string
UseRuneLen bool
// is the optional unicode normalisation form to be used prior to checking length (no unicode normalisation is performed if this is empty or unknown form)
NormalisationForm string
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringExactLength) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringExactLength) checkString(str string, vcx *ValidatorContext) bool {
if frm, ok := getUnicodeNormalisationForm(c.NormalisationForm); ok {
str = frm.String(str)
}
l := len(str)
if c.UseRuneLen {
l = len([]rune(str))
}
return l == c.Value
}
// GetMessage implements the Constraint.GetMessage
func (c *StringExactLength) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, fmtMsgStringExactLen, c.Value)
}
// StringLength constraint to check that a string has a minimum and maximum length
type StringLength struct {
// the minimum length
Minimum int
// the maximum length (only checked if this value is > 0)
Maximum int
// if set to true, ExclusiveMin specifies the minimum value is exclusive
ExclusiveMin bool
// if set to true, ExclusiveMax specifies the maximum value is exclusive
ExclusiveMax bool
// if set to true, uses the rune length (true Unicode length) to check length of string
UseRuneLen bool
// is the optional unicode normalisation form to be used prior to checking length (no unicode normalisation is performed if this is empty or unknown form)
NormalisationForm string
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringLength) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringLength) checkString(str string, vcx *ValidatorContext) bool {
if frm, ok := getUnicodeNormalisationForm(c.NormalisationForm); ok {
str = frm.String(str)
}
l := len(str)
if c.UseRuneLen {
l = len([]rune(str))
}
return (l > c.Minimum || (!c.ExclusiveMin && l == c.Minimum)) &&
(c.Maximum <= 0 || l < c.Maximum || (!c.ExclusiveMax && l == c.Maximum))
}
// GetMessage implements the Constraint.GetMessage
func (c *StringLength) GetMessage(tcx I18nContext) string {
if c.Maximum > 0 {
return defaultMessage(tcx, c.Message, fmtMsgStringMinMaxLen, c.Minimum, incExc(tcx, c.ExclusiveMin), c.Maximum, incExc(tcx, c.ExclusiveMax))
} else if c.ExclusiveMin {
return defaultMessage(tcx, c.Message, fmtMsgStringMinLenExc, c.Minimum)
}
return defaultMessage(tcx, c.Message, fmtMsgStringMinLen, c.Minimum)
}
// StringLowercase constraint to check that a string has only lowercase letters
type StringLowercase struct {
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string `v8n:"default"`
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringLowercase) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringLowercase) checkString(str string, vcx *ValidatorContext) bool {
return str == strings.ToLower(str)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringLowercase) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgStringLowercase)
}
// StringMaxLength constraint to check that a string has a maximum length
type StringMaxLength struct {
// the maximum length value
Value int `v8n:"default"`
// when set to true, ExclusiveMax specifies the maximum value is exclusive
ExclusiveMax bool
// if set to true, uses the rune length (true Unicode length) to check length of string
UseRuneLen bool
// is the optional unicode normalisation form to be used prior to checking length (no unicode normalisation is performed if this is empty or unknown form)
NormalisationForm string
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringMaxLength) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringMaxLength) checkString(str string, vcx *ValidatorContext) bool {
if frm, ok := getUnicodeNormalisationForm(c.NormalisationForm); ok {
str = frm.String(str)
}
l := len(str)
if c.UseRuneLen {
l = len([]rune(str))
}
return l < c.Value || (!c.ExclusiveMax && l == c.Value)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringMaxLength) GetMessage(tcx I18nContext) string {
if c.ExclusiveMax {
return defaultMessage(tcx, c.Message, fmtMsgStringMaxLenExc, c.Value)
}
return defaultMessage(tcx, c.Message, fmtMsgStringMaxLen, c.Value)
}
// StringMinLength constraint to check that a string has a minimum length
type StringMinLength struct {
// the minimum length value
Value int `v8n:"default"`
// when set to true, ExclusiveMin specifies the minimum value is exclusive
ExclusiveMin bool
// if set to true, uses the rune length (true Unicode length) to check length of string
UseRuneLen bool
// is the optional unicode normalisation form to be used prior to checking length (no unicode normalisation is performed if this is empty or unknown form)
NormalisationForm string
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringMinLength) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringMinLength) checkString(str string, vcx *ValidatorContext) bool {
if frm, ok := getUnicodeNormalisationForm(c.NormalisationForm); ok {
str = frm.String(str)
}
l := len(str)
if c.UseRuneLen {
l = len([]rune(str))
}
return l > c.Value || (!c.ExclusiveMin && l == c.Value)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringMinLength) GetMessage(tcx I18nContext) string {
if c.ExclusiveMin {
return defaultMessage(tcx, c.Message, fmtMsgStringMinLenExc, c.Value)
}
return defaultMessage(tcx, c.Message, fmtMsgStringMinLen, c.Value)
}
// StringNoControlCharacters constraint to check that a string does not contain any control characters (i.e. chars < 32)
type StringNoControlCharacters struct {
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string `v8n:"default"`
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringNoControlCharacters) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringNoControlCharacters) checkString(str string, vcx *ValidatorContext) bool {
for _, ch := range str {
if ch < 32 {
return false
}
}
return true
}
// GetMessage implements the Constraint.GetMessage
func (c *StringNoControlCharacters) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgNoControlChars)
}
// StringNotBlank constraint to check that string value is not blank (i.e. that after removing leading and
// trailing whitespace the value is not an empty string)
type StringNotBlank struct {
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string `v8n:"default"`
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringNotBlank) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringNotBlank) checkString(str string, vcx *ValidatorContext) bool {
return len(strings.Trim(str, " \t\n\r")) != 0
}
// GetMessage implements the Constraint.GetMessage
func (c *StringNotBlank) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgNotBlankString)
}
// StringNotEmpty constraint to check that string value is not empty (i.e. not "")
type StringNotEmpty struct {
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string `v8n:"default"`
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringNotEmpty) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringNotEmpty) checkString(str string, vcx *ValidatorContext) bool {
return len(str) > 0
}
// GetMessage implements the Constraint.GetMessage
func (c *StringNotEmpty) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgNotEmptyString)
}
// StringPattern constraint to check that a string matches a given regexp pattern
type StringPattern struct {
// the regexp pattern that the string value must match
Regexp regexp.Regexp `v8n:"default"`
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringPattern) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringPattern) checkString(str string, vcx *ValidatorContext) bool {
return c.Regexp.MatchString(str)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringPattern) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgValidPattern)
}
// StringPresetPattern constraint to check that a string matches a given preset pattern
//
// Preset patterns are defined in PatternPresets (add your own where required)
//
// Messages for the preset patterns are defined in PatternPresetMessages
//
// If the preset pattern requires some extra validation beyond the regexp match, then add
// a checker to the PatternPresetPostPatternChecks variable
type StringPresetPattern struct {
// the preset token (which must exist in the PatternPresets map)
//
// If the specified preset token does not exist - the constraint fails!
Preset string `v8n:"default"`
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringPresetPattern) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
if str, ok := v.(string); ok {
if p, ok := presetsRegistry.get(c.Preset); ok {
if !p.Check(str) {
vcx.CeaseFurtherIf(c.Stop)
return false, c.getMessage(vcx, p.GetMessage())
}
} else {
vcx.CeaseFurtherIf(c.Stop)
return false, vcx.TranslateFormat(fmtMsgUnknownPresetPattern, c.Preset)
}
} else if c.Strict {
if p, ok := presetsRegistry.get(c.Preset); ok {
vcx.CeaseFurtherIf(c.Stop)
return false, c.getMessage(vcx, p.GetMessage())
} else {
vcx.CeaseFurtherIf(c.Stop)
return false, vcx.TranslateFormat(fmtMsgUnknownPresetPattern, c.Preset)
}
}
return true, ""
}
func (c *StringPresetPattern) getMessage(tcx I18nContext, msg string) string {
if c.Message != "" {
return obtainI18nContext(tcx).TranslateMessage(c.Message)
} else if msg != "" {
return obtainI18nContext(tcx).TranslateMessage(msg)
}
// still an empty message...
return obtainI18nContext(tcx).TranslateMessage(msgValidPattern)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringPresetPattern) GetMessage(tcx I18nContext) string {
if c.Message != "" {
return obtainI18nContext(tcx).TranslateMessage(c.Message)
} else if p, ok := presetsRegistry.get(c.Preset); ok {
if msg := p.GetMessage(); msg != "" {
return obtainI18nContext(tcx).TranslateMessage(msg)
}
}
return obtainI18nContext(tcx).TranslateMessage(msgValidPattern)
}
// StringStartsWith constraint to check that a string starts with a given prefix
type StringStartsWith struct {
// the value to check that the string starts with
Value string `v8n:"default"`
// multiple additional values that the string may start with
Values []string
// whether the check is case-insensitive (by default, the check is case-sensitive)
CaseInsensitive bool
// whether the check is NOT-ed (i.e. checks that the string does not start with)
Not bool
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringStartsWith) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringStartsWith) checkString(str string, vcx *ValidatorContext) bool {
ckStr := caseInsensitive(str, c.CaseInsensitive)
startsWith := c.Value != "" && strings.HasPrefix(ckStr, caseInsensitive(c.Value, c.CaseInsensitive))
if !startsWith {
for _, s := range c.Values {
startsWith = s != "" && strings.HasPrefix(ckStr, caseInsensitive(s, c.CaseInsensitive))
if startsWith {
break
}
}
}
return (!c.Not && startsWith) || (c.Not && !startsWith)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringStartsWith) GetMessage(tcx I18nContext) string {
possibles := make([]string, 0)
if c.Value != "" {
possibles = append(possibles, "'"+c.Value+"'")
}
for _, s := range c.Values {
if s != "" {
possibles = append(possibles, "'"+s+"'")
}
}
possiblesStr := strings.Join(possibles, ",")
if c.Not {
return defaultMessage(tcx, c.Message, fmtMsgStringNotStartsWith, possiblesStr)
}
return defaultMessage(tcx, c.Message, fmtMsgStringStartsWith, possiblesStr)
}
// StringUppercase constraint to check that a string has only uppercase letters
type StringUppercase struct {
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string `v8n:"default"`
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringUppercase) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringUppercase) checkString(str string, vcx *ValidatorContext) bool {
return str == strings.ToUpper(str)
}
// GetMessage implements the Constraint.GetMessage
func (c *StringUppercase) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgStringUppercase)
}
// StringValidJson constraint checks that a string is valid json
type StringValidJson struct {
DisallowNullJson bool
DisallowValue bool
DisallowArray bool
DisallowObject bool
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string `v8n:"default"`
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringValidJson) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringValidJson) checkString(str string, vcx *ValidatorContext) bool {
var v interface{}
fail := true
if err := json.Unmarshal([]byte(str), &v); err == nil {
switch v.(type) {
case map[string]interface{}:
fail = c.DisallowObject
case []interface{}:
fail = c.DisallowArray
case nil:
fail = c.DisallowNullJson
default:
fail = c.DisallowValue
}
}
return !fail
}
// GetMessage implements the Constraint.GetMessage
func (c *StringValidJson) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, msgStringValidJson)
}
// StringValidToken constraint checks that a string matches one of a pre-defined list of tokens
type StringValidToken struct {
// the set of allowed tokens for the string
Tokens []string `v8n:"default"`
// set to true to make the token check case in-sensitive
IgnoreCase bool
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringValidToken) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringValidToken) checkString(str string, vcx *ValidatorContext) bool {
lstr := strings.ToLower(str)
for _, t := range c.Tokens {
if str == t || (c.IgnoreCase && lstr == strings.ToLower(t)) {
return true
}
}
return false
}
// GetMessage implements the Constraint.GetMessage
func (c *StringValidToken) GetMessage(tcx I18nContext) string {
return defaultMessage(tcx, c.Message, fmtMsgValidToken, strings.Join(c.Tokens, "\",\""))
}
// StringValidUnicodeNormalization constraint to check that a string has the correct Unicode normalization form
type StringValidUnicodeNormalization struct {
// the normalization form required - i.e. "NFC", "NFKC", "NFD" or "NFKD"
//
// (from package "golang.org/x/text/unicode/norm")
Form string `v8n:"default"`
// the violation message to be used if the constraint fails (see Violation.Message)
//
// (if the Message is an empty string then the default violation message is used)
Message string
// when set to true, Stop prevents further validation checks on the property if this constraint fails
Stop bool
// when set to true, fails if the value being checked is not a correct type
Strict bool
}
// Check implements Constraint.Check
func (c *StringValidUnicodeNormalization) Check(v interface{}, vcx *ValidatorContext) (bool, string) {
return checkStringConstraint(v, vcx, c, c.Strict, c.Stop)
}
func (c *StringValidUnicodeNormalization) checkString(str string, vcx *ValidatorContext) bool {
if f, ok := getUnicodeNormalisationForm(c.Form); ok {
return f.IsNormalString(str)
}
return false
}
// GetMessage implements the Constraint.GetMessage
func (c *StringValidUnicodeNormalization) GetMessage(tcx I18nContext) string {
switch strings.ToUpper(c.Form) {
case "NFKC":
return defaultMessage(tcx, c.Message, msgUnicodeNormalizationNFKC)
case "NFD":
return defaultMessage(tcx, c.Message, msgUnicodeNormalizationNFD)
case "NFKD":
return defaultMessage(tcx, c.Message, msgUnicodeNormalizationNFKD)
}
return defaultMessage(tcx, c.Message, msgUnicodeNormalizationNFC)
}