/
validator.go
540 lines (480 loc) 路 17.5 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
package validation
import (
"fmt"
"reflect"
"sort"
"strings"
"goyave.dev/goyave/v3/helper"
"goyave.dev/goyave/v3/lang"
)
// Ruler adapter interface for method dispatching between RuleSet and Rules
// at route registration time. Allows to input both of these types as parameters
// of the Route.Validate method.
type Ruler interface {
AsRules() *Rules
}
// RuleFunc function defining a validation rule.
// Passing rules should return true, false otherwise.
//
// Rules can modifiy the validated value if needed.
// For example, the "numeric" rule converts the data to float64 if it's a string.
type RuleFunc func(string, interface{}, []string, map[string]interface{}) bool
// RuleDefinition is the definition of a rule, containing the information
// related to the behavior executed on validation-time.
type RuleDefinition struct {
// The Function field is the function that will be executed
Function RuleFunc
// The minimum amount of parameters
RequiredParameters int
// A type rule is a rule that checks if a field has a certain type
// and can convert the raw value to a value fitting. For example, the UUID
// rule is a type rule because it takes a string as input, checks if it's a
// valid UUID and converts it to a "uuid.UUID".
// The "array" rule is an exception. It does convert the value to a new slice of
// the correct type if provided, but is not considered a type rule to avoid being
// able to be used as parameter for itself ("array:array").
IsType bool
// Type-dependent rules are rules that can be used with different field types
// (numeric, string, arrays and files) and have a different validation messages
// depending on the type.
// The language entry used will be "validation.rules.rulename.type"
IsTypeDependent bool
// ComparesFields is true when the rule compares the value of the field under
// validation with another field. A field containing at least one rule with
// ComparesFields = true will be executed later in the validation process to
// ensure conversions are properly executed prior.
ComparesFields bool
}
// RuleSet is a request rules definition. Each entry is a field in the request.
type RuleSet map[string][]string
var _ Ruler = (RuleSet)(nil) // implements Ruler
// AsRules parses and checks this RuleSet and returns it as Rules.
func (r RuleSet) AsRules() *Rules {
return r.parse()
}
// Parse converts the more convenient RuleSet validation rules syntax to
// a Rules map.
func (r RuleSet) parse() *Rules {
rules := &Rules{
Fields: make(FieldMap, len(r)),
}
for k, r := range r {
field := &Field{
Rules: make([]*Rule, 0, len(r)),
}
for _, v := range r {
field.Rules = append(field.Rules, parseRule(v))
}
rules.Fields[k] = field
}
rules.Check()
rules.sortKeys()
return rules
}
// Rule is a component of rule sets for route validation. Each validated fields
// has one or multiple validation rules. The goal of this struct is to
// gather information about how to use a rule definition for this field.
// This inludes the rule name (referring to a RuleDefinition), the parameters
// and the array dimension for array validation.
type Rule struct {
Name string
Params []string
ArrayDimension uint8
}
// IsType returns true if the rule definition is a type rule.
// See RuleDefinition.IsType
func (r *Rule) IsType() bool {
if r.Name == "nullable" {
return false
}
def, exists := validationRules[r.Name]
if !exists {
panic(fmt.Sprintf("Rule \"%s\" doesn't exist", r.Name))
}
return def.IsType
}
// IsTypeDependent returns true if the rule definition is a type-dependent rule.
// See RuleDefinition.IsTypeDependent
func (r *Rule) IsTypeDependent() bool {
if r.Name == "nullable" {
return false
}
def, exists := validationRules[r.Name]
if !exists {
panic(fmt.Sprintf("Rule \"%s\" doesn't exist", r.Name))
}
return def.IsTypeDependent
}
// Field is a component of route validation. A Field is a value in
// a Rules map, the key being the name of the field.
type Field struct {
Rules []*Rule
isArray bool
isRequired bool
isNullable bool
}
// IsRequired check if a field has the "required" rule
func (f *Field) IsRequired() bool {
return f.isRequired
}
// IsNullable check if a field has the "nullable" rule
func (f *Field) IsNullable() bool {
return f.isNullable
}
// IsArray check if a field has the "array" rule
func (f *Field) IsArray() bool {
return f.isArray
}
// Check if rules meet the minimum parameters requirement and update
// the isRequired, isNullable and isArray fields.
func (f *Field) Check() {
for _, rule := range f.Rules {
switch rule.Name {
case "confirmed", "file", "mime", "image", "extension", "count",
"count_min", "count_max", "count_between":
if rule.ArrayDimension != 0 {
panic(fmt.Sprintf("Cannot use rule \"%s\" in array validation", rule.Name))
}
case "required":
f.isRequired = true
case "nullable":
f.isNullable = true
continue
case "array":
f.isArray = true
}
def, exists := validationRules[rule.Name]
if !exists {
panic(fmt.Sprintf("Rule \"%s\" doesn't exist", rule.Name))
}
if len(rule.Params) < def.RequiredParameters {
panic(fmt.Sprintf("Rule \"%s\" requires %d parameter(s)", rule.Name, def.RequiredParameters))
}
}
}
// FieldMap is an alias to shorten verbose validation rules declaration.
// Maps a field name (key) with a Field struct (value).
type FieldMap map[string]*Field
// Rules is a component of route validation and maps a
// field name (key) with a Field struct (value).
type Rules struct {
Fields FieldMap
sortedKeys []string
checked bool
}
var _ Ruler = (*Rules)(nil) // implements Ruler
// AsRules performs the checking and returns the same Rules instance.
func (r *Rules) AsRules() *Rules {
r.Check()
if r.sortedKeys == nil {
r.sortKeys()
}
return r
}
// Check all rules in this set. This function will panic if
// any of the rules doesn't refer to an existing RuleDefinition, doesn't
// meet the parameters requirement, or if the rule cannot be used in array validation
// while ArrayDimension is not equal to 0.
func (r *Rules) Check() {
if !r.checked {
for _, field := range r.Fields {
field.Check()
}
r.checked = true
}
}
func (r *Rules) sortKeys() {
r.sortedKeys = make([]string, 0, len(r.Fields))
for k := range r.Fields {
r.sortedKeys = append(r.sortedKeys, k)
}
sort.SliceStable(r.sortedKeys, func(i, j int) bool {
fieldName1 := r.sortedKeys[i]
field2 := r.Fields[r.sortedKeys[j]]
for _, r := range field2.Rules {
def, ok := validationRules[r.Name]
if ok && def.ComparesFields && helper.ContainsStr(r.Params, fieldName1) {
return true
}
}
return false
})
}
// Errors is a map of validation errors with the field name as a key.
type Errors map[string][]string
var validationRules map[string]*RuleDefinition
func init() {
validationRules = map[string]*RuleDefinition{
"required": {validateRequired, 0, false, false, false},
"numeric": {validateNumeric, 0, true, false, false},
"integer": {validateInteger, 0, true, false, false},
"min": {validateMin, 1, false, true, false},
"max": {validateMax, 1, false, true, false},
"between": {validateBetween, 2, false, true, false},
"greater_than": {validateGreaterThan, 1, false, true, true},
"greater_than_equal": {validateGreaterThanEqual, 1, false, true, true},
"lower_than": {validateLowerThan, 1, false, true, true},
"lower_than_equal": {validateLowerThanEqual, 1, false, true, true},
"string": {validateString, 0, true, false, false},
"array": {validateArray, 0, false, false, false},
"distinct": {validateDistinct, 0, false, false, false},
"digits": {validateDigits, 0, false, false, false},
"regex": {validateRegex, 1, false, false, false},
"email": {validateEmail, 0, false, false, false},
"size": {validateSize, 1, false, true, false},
"alpha": {validateAlpha, 0, false, false, false},
"alpha_dash": {validateAlphaDash, 0, false, false, false},
"alpha_num": {validateAlphaNumeric, 0, false, false, false},
"starts_with": {validateStartsWith, 1, false, false, false},
"ends_with": {validateEndsWith, 1, false, false, false},
"in": {validateIn, 1, false, false, false},
"not_in": {validateNotIn, 1, false, false, false},
"in_array": {validateInArray, 1, false, false, true},
"not_in_array": {validateNotInArray, 1, false, false, true},
"timezone": {validateTimezone, 0, true, false, false},
"ip": {validateIP, 0, true, false, false},
"ipv4": {validateIPv4, 0, true, false, false},
"ipv6": {validateIPv6, 0, true, false, false},
"json": {validateJSON, 0, true, false, false},
"url": {validateURL, 0, true, false, false},
"uuid": {validateUUID, 0, true, false, false},
"bool": {validateBool, 0, true, false, false},
"same": {validateSame, 1, false, false, true},
"different": {validateDifferent, 1, false, false, true},
"confirmed": {validateConfirmed, 0, false, false, false},
"file": {validateFile, 0, false, false, false},
"mime": {validateMIME, 1, false, false, false},
"image": {validateImage, 0, false, false, false},
"extension": {validateExtension, 1, false, false, false},
"count": {validateCount, 1, false, false, false},
"count_min": {validateCountMin, 1, false, false, false},
"count_max": {validateCountMax, 1, false, false, false},
"count_between": {validateCountBetween, 2, false, false, false},
"date": {validateDate, 0, true, false, false},
"before": {validateBefore, 1, false, false, true},
"before_equal": {validateBeforeEqual, 1, false, false, true},
"after": {validateAfter, 1, false, false, true},
"after_equal": {validateAfterEqual, 1, false, false, true},
"date_equals": {validateDateEquals, 1, false, false, true},
"date_between": {validateDateBetween, 2, false, false, true},
"object": {validateObject, 0, true, false, false},
}
}
// AddRule register a validation rule.
// The rule will be usable in request validation by using the
// given rule name.
//
// Type-dependent messages let you define a different message for
// numeric, string, arrays and files.
// The language entry used will be "validation.rules.rulename.type"
func AddRule(name string, rule *RuleDefinition) {
if _, exists := validationRules[name]; exists {
panic(fmt.Sprintf("Rule %s already exists", name))
}
validationRules[name] = rule
}
// Validate the given data with the given rule set.
// If all validation rules pass, returns an empty "validation.Errors".
// Third parameter tells the function if the data comes from a JSON request.
// Last parameter sets the language of the validation error messages.
func Validate(data map[string]interface{}, rules Ruler, isJSON bool, language string) Errors {
if data == nil {
var malformedMessage string
if isJSON {
malformedMessage = lang.Get(language, "malformed-json")
} else {
malformedMessage = lang.Get(language, "malformed-request")
}
return map[string][]string{"error": {malformedMessage}}
}
return validate(data, isJSON, rules.AsRules(), language)
}
func validate(data map[string]interface{}, isJSON bool, rules *Rules, language string) Errors {
errors := Errors{}
for _, fieldName := range rules.sortedKeys {
field := rules.Fields[fieldName]
name, fieldVal, parent, _ := GetFieldFromName(fieldName, data)
if !field.IsNullable() && fieldVal == nil {
delete(parent, fieldName)
}
if !field.IsRequired() && !validateRequired(fieldName, fieldVal, nil, data) {
continue
}
convertArray(isJSON, name, field, parent) // Convert single value arrays in url-encoded requests
for _, rule := range field.Rules {
fieldVal = parent[name]
if rule.Name == "nullable" {
if fieldVal == nil {
break
}
continue
}
if rule.ArrayDimension > 0 {
if ok, errorValue := validateRuleInArray(rule, fieldName, rule.ArrayDimension, data); !ok {
errors[fieldName] = append(
errors[fieldName],
processPlaceholders(fieldName, rule.Name, rule.Params, getMessage(field.Rules, rule, errorValue, language), language),
)
}
} else if !validationRules[rule.Name].Function(fieldName, fieldVal, rule.Params, data) {
errors[fieldName] = append(
errors[fieldName],
processPlaceholders(fieldName, rule.Name, rule.Params, getMessage(field.Rules, rule, reflect.ValueOf(fieldVal), language), language),
)
}
}
}
return errors
}
func validateRuleInArray(rule *Rule, fieldName string, arrayDimension uint8, data map[string]interface{}) (bool, reflect.Value) {
if t := GetFieldType(data[fieldName]); t != "array" {
return false, reflect.ValueOf(data[fieldName])
}
converted := false
var convertedArr reflect.Value
list := reflect.ValueOf(data[fieldName])
length := list.Len()
for i := 0; i < length; i++ {
v := list.Index(i)
value := v.Interface()
tmpData := map[string]interface{}{fieldName: value}
if arrayDimension > 1 {
ok, errorValue := validateRuleInArray(rule, fieldName, arrayDimension-1, tmpData)
if !ok {
return false, errorValue
}
} else if !validationRules[rule.Name].Function(fieldName, value, rule.Params, tmpData) {
return false, v
}
// Update original array if value has been modified.
if rule.Name == "array" {
if !converted { // Ensure field is a two dimensional array of the correct type
convertedArr = reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(tmpData[fieldName])), 0, length)
converted = true
}
convertedArr = reflect.Append(convertedArr, reflect.ValueOf(tmpData[fieldName]))
} else {
v.Set(reflect.ValueOf(tmpData[fieldName]))
}
}
if converted {
data[fieldName] = convertedArr.Interface()
}
return true, reflect.Value{}
}
func convertArray(isJSON bool, fieldName string, field *Field, data map[string]interface{}) {
if !isJSON {
val := data[fieldName]
rv := reflect.ValueOf(val)
kind := rv.Kind().String()
if field.IsArray() && kind != "slice" {
rt := reflect.TypeOf(val)
slice := reflect.MakeSlice(reflect.SliceOf(rt), 0, 1)
slice = reflect.Append(slice, rv)
data[fieldName] = slice.Interface()
}
}
}
func getMessage(rules []*Rule, rule *Rule, value reflect.Value, language string) string {
langEntry := "validation.rules." + rule.Name
if validationRules[rule.Name].IsTypeDependent {
expectedType := findTypeRule(rules, rule.ArrayDimension)
if expectedType == "unsupported" {
langEntry += "." + getFieldType(value)
} else {
if expectedType == "integer" {
expectedType = "numeric"
}
langEntry += "." + expectedType
}
}
if rule.ArrayDimension > 0 {
langEntry += ".array"
}
return lang.Get(language, langEntry)
}
// findTypeRule find the expected type of a field for a given array dimension.
func findTypeRule(rules []*Rule, arrayDimension uint8) string {
for _, rule := range rules {
if rule.ArrayDimension == arrayDimension-1 && rule.Name == "array" && len(rule.Params) > 0 {
return rule.Params[0]
} else if rule.ArrayDimension == arrayDimension && validationRules[rule.Name].IsType {
return rule.Name
}
}
return "unsupported"
}
// GetFieldType returns the non-technical type of the given "value" interface.
// This is used by validation rules to know if the input data is a candidate
// for validation or not and is especially useful for type-dependent rules.
// - "numeric" if the value is an int, uint or a float
// - "string" if the value is a string
// - "array" if the value is a slice
// - "file" if the value is a slice of "filesystem.File"
// - "unsupported" otherwise
func GetFieldType(value interface{}) string {
return getFieldType(reflect.ValueOf(value))
}
func getFieldType(value reflect.Value) string {
kind := value.Kind().String()
switch {
case strings.HasPrefix(kind, "int"), strings.HasPrefix(kind, "uint") && kind != "uintptr", strings.HasPrefix(kind, "float"):
return "numeric"
case kind == "string":
return "string"
case kind == "slice":
if value.Type().String() == "[]filesystem.File" {
return "file"
}
return "array"
default:
if value.IsValid() {
if _, ok := value.Interface().(map[string]interface{}); ok {
return "object"
}
}
return "unsupported"
}
}
// GetFieldFromName find potentially nested field by it's dot-separated path
// in the given object.
// Returns the name without its prefix, the value, its parent object and a bool indicating if it has been found or not.
func GetFieldFromName(name string, data map[string]interface{}) (string, interface{}, map[string]interface{}, bool) {
key := name
i := strings.Index(name, ".")
if i != -1 {
key = name[:i]
}
val, ok := data[key]
if !ok {
return "", nil, nil, false
}
if i != -1 {
if obj, ok := val.(map[string]interface{}); ok {
return GetFieldFromName(name[len(key)+1:], obj)
}
}
return name, val, data, ok
}
func parseRule(rule string) *Rule {
indexName := strings.Index(rule, ":")
params := []string{}
arrayDimensions := uint8(0)
var ruleName string
if indexName == -1 {
if strings.Count(rule, ",") > 0 {
panic(fmt.Sprintf("Invalid rule: \"%s\"", rule))
}
ruleName = rule
} else {
ruleName = rule[:indexName]
params = strings.Split(rule[indexName+1:], ",")
}
if ruleName[0] == '>' {
for ruleName[0] == '>' {
ruleName = ruleName[1:]
arrayDimensions++
}
}
return &Rule{ruleName, params, arrayDimensions}
}