|
1
|
|
|
package it |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"github.com/muonsoft/validation" |
|
5
|
|
|
"github.com/muonsoft/validation/code" |
|
6
|
|
|
"github.com/muonsoft/validation/message" |
|
7
|
|
|
|
|
8
|
|
|
"regexp" |
|
9
|
|
|
"strconv" |
|
10
|
|
|
"unicode/utf8" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
// LengthConstraint checks that a given string length is between some minimum and maximum value. |
|
14
|
|
|
type LengthConstraint struct { |
|
15
|
|
|
isIgnored bool |
|
16
|
|
|
checkMin bool |
|
17
|
|
|
checkMax bool |
|
18
|
|
|
min int |
|
19
|
|
|
max int |
|
20
|
|
|
minMessageTemplate string |
|
21
|
|
|
maxMessageTemplate string |
|
22
|
|
|
exactMessageTemplate string |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
func newLengthConstraint(min int, max int, checkMin bool, checkMax bool) LengthConstraint { |
|
26
|
1 |
|
return LengthConstraint{ |
|
27
|
|
|
min: min, |
|
28
|
|
|
max: max, |
|
29
|
|
|
checkMin: checkMin, |
|
30
|
|
|
checkMax: checkMax, |
|
31
|
|
|
minMessageTemplate: message.LengthTooFew, |
|
32
|
|
|
maxMessageTemplate: message.LengthTooMany, |
|
33
|
|
|
exactMessageTemplate: message.LengthExact, |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
func HasMinLength(min int) LengthConstraint { |
|
38
|
1 |
|
return newLengthConstraint(min, 0, true, false) |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
func HasMaxLength(max int) LengthConstraint { |
|
42
|
1 |
|
return newLengthConstraint(0, max, false, true) |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
func HasLengthBetween(min int, max int) LengthConstraint { |
|
46
|
1 |
|
return newLengthConstraint(min, max, true, true) |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
func HasExactLength(count int) LengthConstraint { |
|
50
|
1 |
|
return newLengthConstraint(count, count, true, true) |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
func (c LengthConstraint) SetUp(scope *validation.Scope) error { |
|
54
|
1 |
|
return nil |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
func (c LengthConstraint) Name() string { |
|
58
|
|
|
return "LengthConstraint" |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
func (c LengthConstraint) When(condition bool) LengthConstraint { |
|
62
|
1 |
|
c.isIgnored = !condition |
|
63
|
1 |
|
return c |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
func (c LengthConstraint) MinMessage(message string) LengthConstraint { |
|
67
|
1 |
|
c.minMessageTemplate = message |
|
68
|
1 |
|
return c |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
func (c LengthConstraint) MaxMessage(message string) LengthConstraint { |
|
72
|
1 |
|
c.maxMessageTemplate = message |
|
73
|
1 |
|
return c |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
func (c LengthConstraint) ExactMessage(message string) LengthConstraint { |
|
77
|
1 |
|
c.exactMessageTemplate = message |
|
78
|
1 |
|
return c |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
func (c LengthConstraint) ValidateString(value *string, scope validation.Scope) error { |
|
82
|
1 |
|
if c.isIgnored || value == nil || *value == "" { |
|
83
|
1 |
|
return nil |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
count := utf8.RuneCountInString(*value) |
|
87
|
|
|
|
|
88
|
1 |
|
if c.checkMax && count > c.max { |
|
89
|
1 |
|
return c.newViolation(count, c.max, code.LengthTooMany, c.maxMessageTemplate, scope) |
|
90
|
|
|
} |
|
91
|
1 |
|
if c.checkMin && count < c.min { |
|
92
|
1 |
|
return c.newViolation(count, c.min, code.LengthTooFew, c.minMessageTemplate, scope) |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return nil |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
func (c LengthConstraint) newViolation( |
|
99
|
|
|
count, limit int, |
|
100
|
|
|
violationCode, message string, |
|
101
|
|
|
scope validation.Scope, |
|
102
|
|
|
) validation.Violation { |
|
103
|
1 |
|
if c.checkMin && c.checkMax && c.min == c.max { |
|
104
|
1 |
|
message = c.exactMessageTemplate |
|
105
|
1 |
|
violationCode = code.LengthExact |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return scope.BuildViolation(violationCode, message). |
|
109
|
|
|
SetPluralCount(limit). |
|
110
|
|
|
SetParameters(map[string]string{ |
|
111
|
|
|
"{{ count }}": strconv.Itoa(count), |
|
112
|
|
|
"{{ limit }}": strconv.Itoa(limit), |
|
113
|
|
|
}). |
|
114
|
|
|
GetViolation() |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// RegexConstraint is used to ensure that the given value corresponds to regex pattern. |
|
118
|
|
|
type RegexConstraint struct { |
|
119
|
|
|
isIgnored bool |
|
120
|
|
|
messageTemplate string |
|
121
|
|
|
pattern string |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// IsRegexMatch creates a RegexConstraint for checking whether a value match. |
|
125
|
|
|
// |
|
126
|
|
|
// Example |
|
127
|
|
|
// err := validator.ValidateString(&s, it.IsRegexMatch("^[a-z]+$")) |
|
128
|
|
|
func IsRegexMatch(pattern string) RegexConstraint { |
|
129
|
1 |
|
return RegexConstraint{ |
|
130
|
|
|
pattern: pattern, |
|
131
|
|
|
messageTemplate: message.Regex, |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// SetUp will return an error if the pattern is empty. |
|
136
|
|
|
func (c RegexConstraint) SetUp(scope *validation.Scope) error { |
|
137
|
1 |
|
if c.pattern == "" { |
|
138
|
1 |
|
return errEmptyPattern |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
return nil |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// Name is the constraint name. |
|
145
|
|
|
func (c RegexConstraint) Name() string { |
|
146
|
1 |
|
return "RegexConstraint" |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
// Message sets the violation message template. You can use template parameters. |
|
150
|
|
|
func (c RegexConstraint) Message(message string) RegexConstraint { |
|
151
|
1 |
|
c.messageTemplate = message |
|
152
|
1 |
|
return c |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
|
156
|
|
|
// then the constraint will be ignored. |
|
157
|
|
|
func (c RegexConstraint) When(condition bool) RegexConstraint { |
|
158
|
1 |
|
c.isIgnored = !condition |
|
159
|
1 |
|
return c |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
func (c RegexConstraint) ValidateString(value *string, scope validation.Scope) error { |
|
163
|
1 |
|
if c.isIgnored || value == nil || *value == "" { |
|
164
|
1 |
|
return nil |
|
165
|
|
|
} |
|
166
|
1 |
|
if match, _ := regexp.MatchString(c.pattern, *value); match { |
|
167
|
1 |
|
return nil |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
return scope. |
|
171
|
|
|
BuildViolation(code.Regex, c.messageTemplate). |
|
172
|
|
|
GetViolation() |
|
173
|
|
|
} |
|
174
|
|
|
|