forked from spaceweasel/mango
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute_validator.go
More file actions
333 lines (280 loc) · 9.66 KB
/
route_validator.go
File metadata and controls
333 lines (280 loc) · 9.66 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
package mango
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// ParamValidator is the interface describing a route parameter validator.
// ParamValidators are used to validate sections of a URL path which match
// the pattern of an entry in the routing tree.
type ParamValidator interface {
// Validate tests if s matches the validation rules. The validation test
// may involve constraint specific args.
Validate(s string, args []string) bool
// Type returns the constraint name used in routing patterns.
// RouteParamValidators will use this to locate the correct validator.
Type() string
}
// RouteParamValidators is the interface for a collection of ParamValidators.
// A route parameter value can be checked against its constraint using the
// IsValid method. New validators can be added individually using AddValidator
// or as a collection using AddValidators.
type RouteParamValidators interface {
AddValidator(v ParamValidator)
AddValidators(validators []ParamValidator)
IsValid(s, constraint string) bool
}
type parameterValidators struct {
validators map[string]ParamValidator
}
func (r *parameterValidators) AddValidator(v ParamValidator) {
if _, ok := r.validators[v.Type()]; ok {
panic("conflicting constraint type: " + v.Type())
}
r.validators[v.Type()] = v
}
func (r *parameterValidators) AddValidators(validators []ParamValidator) {
for _, v := range validators {
r.AddValidator(v)
}
}
func (r *parameterValidators) IsValid(s, constraint string) bool {
var args []string
if strings.HasSuffix(constraint, ")") {
i := strings.IndexByte(constraint, byte('('))
if i < 1 {
panic(fmt.Sprintf("illegal constraint format: %s", constraint))
}
args = strings.Split(constraint[i+1:len(constraint)-1], ",")
for i, p := range args {
args[i] = strings.TrimSpace(p)
}
constraint = constraint[:i]
} else if strings.IndexByte(constraint, byte('(')) > -1 {
panic(fmt.Sprintf("illegal constraint format: %s", constraint))
}
constraint = strings.TrimSpace(constraint)
v, ok := r.validators[constraint]
if !ok {
panic(fmt.Sprintf("unknown constraint: %s", constraint))
}
return v.Validate(s, args)
}
func newParameterValidators() RouteParamValidators {
v := parameterValidators{}
v.validators = make(map[string]ParamValidator)
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 (EmptyValidator) Validate(s string, args []string) bool {
return true
}
// Type returns the constraint name. This is an empty string to
// ensure this valiadtor is selected when no constraint has been
// specified in the route pattern parameter.
func (EmptyValidator) Type() string {
return ""
}
// Int32Validator tests for 32 bit integer values.
type Int32Validator struct{}
// Validate tests for 32 bit integer values.
// Returns true if s is an integer in the range -2147483648 to 2147483647
func (Int32Validator) Validate(s string, params []string) bool {
_, err := strconv.ParseInt(s, 10, 32)
return err == nil
}
// Type returns the constraint name (int32).
func (Int32Validator) Type() string {
return "int32"
}
// Int64Validator tests for 64 bit integer values.
type Int64Validator struct{}
// Validate tests for 64 bit integer values.
// Returns true if s is an integer in the range -9223372036854775808 to 9223372036854775807
func (Int64Validator) Validate(s string, params []string) bool {
_, err := strconv.ParseInt(s, 10, 64)
return err == nil
}
// Type returns the constraint name (int64).
func (Int64Validator) Type() string {
return "int64"
}
// AlphaValidator tests for a sequence containing only alpha characters.
type AlphaValidator struct{}
// Validate tests for alpha values.
// Returns true if s contains only characters in the ranges a-z or A-Z.
func (AlphaValidator) Validate(s string, params []string) bool {
re := regexp.MustCompile(`^[a-zA-Z]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (alpha).
func (AlphaValidator) Type() string {
return "alpha"
}
// DigitsValidator tests for a sequence of digits.
type DigitsValidator struct{}
// Validate tests for digit values.
// Returns true if s contains only digits 0-9.
func (DigitsValidator) Validate(s string, params []string) bool {
re := regexp.MustCompile(`^\d+$`)
return re.MatchString(s)
}
// Type returns the constraint name (digits).
func (DigitsValidator) Type() string {
return "digits"
}
// Hex32Validator tests for 32 bit hex values.
type Hex32Validator struct{}
// Validate tests for 32 bit hex values.
// Returns true if s is hexadecimal in the range -80000000 to 7FFFFFFF.
// The test is not case sensitive, i.e. 3ef42bc7 and 3EF42BC7 will both return true.
func (Hex32Validator) Validate(s string, params []string) bool {
_, err := strconv.ParseInt(s, 16, 32)
return err == nil
}
// Type returns the constraint name (hex32).
func (Hex32Validator) Type() string {
return "hex32"
}
// Hex64Validator tests for 64 bit hex values.
type Hex64Validator struct{}
// Validate tests for 64 bit hex values.
// Returns true if s is hexadecimal in the range -8000000000000000 to 7FFFFFFFFFFFFFFF.
// The test is not case sensitive, i.e. 3ef42bc7 and 3EF42BC7 will both return true.
func (Hex64Validator) Validate(s string, params []string) bool {
_, err := strconv.ParseInt(s, 16, 64)
return err == nil
}
// Type returns the constraint name (hex64).
func (Hex64Validator) Type() string {
return "hex64"
}
// HexValidator tests for a sequence of hexadecimal characters.
type HexValidator struct{}
// Validate tests for hex values.
// Returns true if s contains only hex characters, (i.e. 0-9, a-e, A-F).
func (HexValidator) Validate(s string, params []string) bool {
re := regexp.MustCompile(`^[0-9a-fA-F]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (hex).
func (HexValidator) Type() string {
return "hex"
}
// UUIDValidator tests for UUIDs.
type UUIDValidator struct{}
// Validate tests for UUID values.
// Returns true if s is 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
func (UUIDValidator) Validate(s string, params []string) bool {
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 (UUIDValidator) Type() string {
return "uuid"
}
// AlphaNumValidator tests for a sequence containing only alphanumeric characters.
type AlphaNumValidator struct{}
// Validate tests for alphanumeric values.
// Returns true if s contains only characters in the ranges a-z, A-Z or 0-9.
func (AlphaNumValidator) Validate(s string, params []string) bool {
re := regexp.MustCompile(`^[a-zA-Z0-9]+$`)
return re.MatchString(s)
}
// Type returns the constraint name (alphanum).
func (AlphaNumValidator) Type() string {
return "alphanum"
}
// PrefixValidator tests for a specified prefix
type PrefixValidator struct{}
// Validate tests for a prefix.
// Returns true if s starts with the prefix specified in args.
func (PrefixValidator) Validate(s string, params []string) bool {
pf := ""
if len(params) > 0 {
pf = params[0]
}
return strings.HasPrefix(s, pf)
}
// Type returns the constraint name (prefix).
func (PrefixValidator) Type() string {
return "prefix"
}
// SuffixValidator tests for a specified suffix
type SuffixValidator struct{}
// Validate tests for a suffix.
// Returns true if s ends with the suffix specified in args.
func (SuffixValidator) Validate(s string, params []string) bool {
sf := ""
if len(params) > 0 {
sf = params[0]
}
return strings.HasSuffix(s, sf)
}
// Type returns the constraint name (suffix).
func (SuffixValidator) Type() string {
return "suffix"
}
//
func getDefaultValidators() []ParamValidator {
return []ParamValidator{
EmptyValidator{},
Int32Validator{},
Int64Validator{},
AlphaValidator{},
DigitsValidator{},
Hex32Validator{},
Hex64Validator{},
HexValidator{},
UUIDValidator{},
}
}
/*
TODO: Add validators for these
alphanumeric, bool, float32, float64, uint32, uint64:
b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
uint64, err := strconv.ParseUint("42", 10, 64)
parameterised constraints:
min(minimum) - Allows only integer values with the specified minimum value.
max(maximum) - Allows only integer values with the specified maximum value.
range(minimum, maximum) - Allows only integer values within the specified range. (Between minimum and maximum)
minlength(length) - Allows only values longer than the specified minimum length.
maxlength(length) - Allows only values shorter that the maximum length.
length(minimum, maximum) - Allows only values with length within the specified range. (Between minimum and maximum)
*/