-
Notifications
You must be signed in to change notification settings - Fork 1
/
validators.go
283 lines (248 loc) · 6.83 KB
/
validators.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
package goformkeeper
import (
"errors"
"fmt"
"net/mail"
"net/url"
"regexp"
"unicode/utf8"
)
type Constraint struct {
Type string
Message string
Criteria map[string]interface{}
}
type Criteria struct {
values map[string]interface{}
}
type Validator interface {
Validate(string, *Criteria) (bool, error)
}
func (c *Criteria) Has(key string) bool {
_, found := c.values[key]
return found
}
func (c *Criteria) StringArray(key string) ([]string, error) {
value, found := c.values[key]
if found {
vs, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("Couldn't cast to []string '%s'", key)
}
results := make([]string, len(vs))
for i, v := range vs {
result, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Couldn't cast to []string '%s'", key)
}
results[i] = result
}
return results, nil
} else {
return nil, fmt.Errorf("Param not found '%s'", key)
}
}
func (c *Criteria) Bool(key string) (bool, error) {
value, found := c.values[key]
if found {
v, ok := value.(bool)
if ok {
return v, nil
} else {
return false, fmt.Errorf("Couldn't cast to bool '%s'", key)
}
} else {
return false, fmt.Errorf("Param not found '%s'", key)
}
}
func (c *Criteria) Int(key string) (int, error) {
value, found := c.values[key]
if found {
v, ok := value.(int)
if ok {
return v, nil
} else {
return 0, fmt.Errorf("Couldn't cast to int '%s'", key)
}
} else {
return 0, fmt.Errorf("Param not found '%s'", key)
}
}
func (c *Criteria) String(key string) (string, error) {
value, found := c.values[key]
if found {
v, ok := value.(string)
if ok {
return v, nil
} else {
return "", fmt.Errorf("Couldn't cast to string '%s'", key)
}
} else {
return "", fmt.Errorf("Param not found '%s'", key)
}
}
type RegExpValidator struct{}
func (v *RegExpValidator) Validate(value string, criteria *Criteria) (bool, error) {
if criteria != nil && criteria.Has("regex") {
regex, err := criteria.String("regex")
if err != nil {
return false, err
}
if m, _ := regexp.MatchString(regex, value); !m {
return false, nil
}
return true, nil
} else {
return false, errors.New("Criteria for 'regex' not enough")
}
}
type LooseEmailAddressValidator struct{}
func (v *LooseEmailAddressValidator) Validate(value string, criteria *Criteria) (bool, error) {
// https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
if m, _ := regexp.MatchString("(^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$)", value); !m {
return false, nil
}
return true, nil
}
type EmailAddressValidator struct{}
func (v *EmailAddressValidator) Validate(value string, criteria *Criteria) (bool, error) {
_, err := mail.ParseAddress(value)
if err != nil {
return false, nil
}
return true, nil
}
type URLValidator struct{}
func (v *URLValidator) Validate(value string, criteria *Criteria) (bool, error) {
_, err := url.ParseRequestURI(value)
if err != nil {
return false, nil
}
return true, nil
}
type AlphabetValidator struct{}
func (v *AlphabetValidator) Validate(value string, criteria *Criteria) (bool, error) {
if m, _ := regexp.MatchString("^[a-zA-Z]+$", value); !m {
return false, nil
}
return true, nil
}
type AlphabetAndNumberValidator struct{}
func (v *AlphabetAndNumberValidator) Validate(value string, criteria *Criteria) (bool, error) {
if m, _ := regexp.MatchString("^[0-9a-zA-Z]+$", value); !m {
return false, nil
}
return true, nil
}
type AsciiValidator struct{}
func (v *AsciiValidator) Validate(value string, criteria *Criteria) (bool, error) {
if m, _ := regexp.MatchString("^[\\x20-\\x7E]+$", value); !m {
return false, nil
}
return true, nil
}
type AsciiWithoutSpaceValidator struct{}
func (v *AsciiWithoutSpaceValidator) Validate(value string, criteria *Criteria) (bool, error) {
if m, _ := regexp.MatchString("^[\\x21-\\x7E]+$", value); !m {
return false, nil
}
return true, nil
}
type RuneCountValidator struct{}
func (v *RuneCountValidator) Validate(value string, criteria *Criteria) (bool, error) {
if criteria == nil {
return false, errors.New("Criteria for 'rune_count' not enough")
}
if criteria.Has("eq") {
eq, err := criteria.Int("eq")
if err != nil {
return false, err
}
return utf8.RuneCount([]byte(value)) == eq, nil
} else if criteria.Has("to") && criteria.Has("from") {
to, err := criteria.Int("to")
if err != nil {
return false, err
}
from, err := criteria.Int("from")
if err != nil {
return false, err
}
count := utf8.RuneCount([]byte(value))
return count >= from && count <= to, nil
} else {
return false, errors.New("Criteria for 'rune_count' not enough")
}
}
type IncludedValidator struct{}
func (v *IncludedValidator) Validate(value string, criteria *Criteria) (bool, error) {
if criteria == nil {
return false, errors.New("Criteria for 'included' not enough")
}
if criteria.Has("in") {
params, err := criteria.StringArray("in")
if err != nil {
return false, err
}
for _, v := range params {
if value == v {
return true, nil
}
}
return false, nil
} else {
return false, errors.New("Criteria for 'included' not enough")
}
}
type LengthValidator struct{}
func (v *LengthValidator) Validate(value string, criteria *Criteria) (bool, error) {
if criteria == nil {
return false, errors.New("Criteria for 'length' not enough")
}
if criteria.Has("eq") {
eq, err := criteria.Int("eq")
if err != nil {
return false, err
}
return len(value) == eq, nil
} else if criteria.Has("to") && criteria.Has("from") {
to, err := criteria.Int("to")
if err != nil {
return false, err
}
from, err := criteria.Int("from")
if err != nil {
return false, err
}
return len(value) >= from && len(value) <= to, nil
} else {
return false, errors.New("Criteria for 'length' not enough")
}
}
var validators map[string]Validator
func init() {
validators = make(map[string]Validator)
AddValidator("length", &LengthValidator{})
AddValidator("rune_count", &RuneCountValidator{})
AddValidator("alphabet", &AlphabetValidator{})
AddValidator("alnum", &AlphabetAndNumberValidator{})
AddValidator("ascii", &AsciiValidator{})
AddValidator("ascii_without_space", &AsciiWithoutSpaceValidator{})
AddValidator("regex", &RegExpValidator{})
AddValidator("url", &URLValidator{})
AddValidator("email", &EmailAddressValidator{})
AddValidator("loose_email", &LooseEmailAddressValidator{})
AddValidator("included", &IncludedValidator{})
}
func AddValidator(name string, validator Validator) {
validators[name] = validator
}
func validate(value string, constraint *Constraint) (bool, error) {
validator, found := validators[constraint.Type]
if !found {
return false, fmt.Errorf("Validator not found: %s", constraint.Type)
}
criteria := &Criteria{constraint.Criteria}
ok, err := validator.Validate(value, criteria)
return ok, err
}