forked from tonyhb/validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
181 lines (153 loc) · 5.39 KB
/
validate.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
package validate
import (
"reflect"
"regexp"
"strings"
"github.com/tonyhb/govalidate/rules"
_ "github.com/tonyhb/govalidate/rules/alpha"
_ "github.com/tonyhb/govalidate/rules/alphanumeric"
_ "github.com/tonyhb/govalidate/rules/email"
_ "github.com/tonyhb/govalidate/rules/greaterthan"
_ "github.com/tonyhb/govalidate/rules/length"
_ "github.com/tonyhb/govalidate/rules/lessthan"
_ "github.com/tonyhb/govalidate/rules/maxlength"
_ "github.com/tonyhb/govalidate/rules/minlength"
_ "github.com/tonyhb/govalidate/rules/notempty"
_ "github.com/tonyhb/govalidate/rules/notzero"
_ "github.com/tonyhb/govalidate/rules/notzerotime"
_ "github.com/tonyhb/govalidate/rules/regexp"
_ "github.com/tonyhb/govalidate/rules/url"
_ "github.com/tonyhb/govalidate/rules/uuid"
)
// Takes a struct, loops through all fields and calls check on any fields that
// have a validate tag. If the field is an anonymous struct recursively run
// validation on it.
func Run(object interface{}, fieldsSlice ...string) error {
pass := true // We'll override this if checking returns false
err := ValidationError{}
// If we have been passed a slice of fields to valiate - to check only a
// subset of fields - change the slice into a map for O(1) lookups instead
// of O(n).
fields := map[string]struct{}{}
for _, field := range fieldsSlice {
fields[field] = struct{}{}
}
// If we're passed a pointer to a struct we need to dereference the pointer before
// inspecting its tags
value := reflect.ValueOf(object)
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
// Iterate through each field of the struct and validate
typ := value.Type() // A Type's Field method returns StructFields
for i := 0; i < value.NumField(); i++ {
var validateTag string
var validateError error
// Is this an anonymous struct? If so, we also need to validate on this.
if typ.Field(i).Anonymous == true {
if anonErr := Run(value.Field(i).Interface(), fieldsSlice...); anonErr != nil {
// The validation failed: set pass to false and merge the anonymous struct's
// validation errors with our current validation error above to give a complete
// error message.
pass = false
// A non validation error occurred: return this immediately
if _, ok := anonErr.(ValidationError); !ok {
return anonErr
}
err.Merge(anonErr.(ValidationError))
}
}
if len(fields) > 0 {
// We're only checking for a subset of fields; if this field isn't
// included in the subset of fields to validate we can skip.
if _, ok := fields[typ.Field(i).Name]; !ok {
continue
}
}
if validateTag = typ.Field(i).Tag.Get("validate"); validateTag == "" {
continue
}
// Validate this particular field against the options in our tag
if validateError = validateField(value.Field(i).Interface(), typ.Field(i).Name, validateTag); validateError == nil {
continue
}
// If there was no validation rule defined for the given tag return
// that error immediately.
if _, ok := validateError.(rules.ErrNoValidationMethod); ok {
return validateError
}
pass = false
err.addFailure(typ.Field(i).Name, validateError.Error())
}
if pass {
return nil
}
return err
}
var rxRegexp = regexp.MustCompile(`Regexp:\/.+/`)
// Takes a field's value and the validation tag and applies each check
// until either a test fails or all tests pass.
func validateField(data interface{}, fieldName, tag string) (err error) {
// A tag can specify multiple validation rules which are delimited via ','.
// However, because we allow regular expressions we can't split the tag field
// via all commas to find our validation rules: we need to extract the regular expression
// first (in case it specifies a comma), and *then* run through our validation rules.
if match := rxRegexp.FindString(tag); match != "" {
// If we fail validating the regexp we can break here
if err := validateRule(data, fieldName, match); err != nil {
return err
}
// Now we need to replace our regular expression from the tag list to continue
// normally.
tag = rxRegexp.ReplaceAllString(tag, "")
}
for tag != "" {
var next string
i := strings.Index(tag, ",")
if i >= 0 {
tag, next = tag[:i], tag[i+1:]
}
if err := validateRule(data, fieldName, tag); err != nil {
return err
}
// Continue with the next tag
tag = next
}
return nil
}
// Given a validation rule from a tag, run the associated validation methods and return
// the result.
func validateRule(data interface{}, fieldName, rule string) error {
var args []string
// Remove any preceeding spaces from comma separated tags
rule = strings.TrimLeft(rule, " ")
// If the rule is empty we don't need to process anything. This only happens
// if we have a regex followed by another rule:
// `validate:"Regexp:/.+/, NotEmpty"`
// Becomes:
// `validate:", NotEmpty"`
// After processing in validateField()
if rule == "" {
return nil
}
// rule is the method we want to call. If it has a colon we need to further
// process the rule to extract arguments to our validation method.
i := strings.Index(rule, ":")
if i > 0 {
var arg string
rule, arg = rule[:i], rule[i+1:]
args = append(args, arg)
}
// Attempt to validate the data using methods registered with the rules
// sub package
if method, err := rules.Get(rule); err != nil {
return err
} else {
var data = rules.ValidationData{
Field: fieldName,
Value: data,
Args: args,
}
return method(data)
}
}