-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
54 lines (51 loc) · 1.5 KB
/
validation.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
package structdbpostgres
import (
validator "github.com/mikolajgs/struct-validator"
"reflect"
"regexp"
"strings"
)
// Validate checks object's fields. It returns result of validation as a bool and list of fields with invalid value
func (c Controller) Validate(obj interface{}, filters map[string]interface{}) (bool, map[string]int, error) {
if filters != nil {
restrictFilters := make(map[string]bool)
failedFiltersLike := make(map[string]int)
for k, v := range filters {
if !strings.Contains(k, ":") {
restrictFilters[k] = true
continue
}
kArr := strings.Split(k, ":")
if kArr[1] != "%" {
continue
}
r := reflect.TypeOf(v).Kind()
if r != reflect.String {
// TODO Use validator.FailType when added
failedFiltersLike[k] = validator.FailRegexp
continue
}
reLike := regexp.MustCompile(`^[a-zA-Z0-9\-+_\.: \$@!=|\[\]\{\}<>,\?/]{0,32}$`)
if !reLike.MatchString(v.(string)) {
failedFiltersLike[k] = validator.FailRegexp
continue
}
}
valid, failedFields := validator.Validate(obj, &validator.ValidationOptions{
ValidateWhenSuffix: true,
OverwriteFieldValues: filters,
RestrictFields: restrictFilters,
OverwriteTagName: c.tagName,
})
for k, v := range failedFiltersLike {
failedFields[k] = v
valid = true
}
return valid, failedFields, nil
}
valid, failedFields := validator.Validate(obj, &validator.ValidationOptions{
ValidateWhenSuffix: true,
OverwriteTagName: c.tagName,
})
return valid, failedFields, nil
}