-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
283 lines (248 loc) · 8 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
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 miso
import (
"fmt"
"reflect"
"strconv"
"strings"
"unicode/utf8"
)
const (
TagValidationV1 = "validation" // name of validation tag
TagValidationV2 = "valid" // name of validation tag (v2)
ValidMaxLen = "maxLen" // max length of a string, array, slice, e.g., `valid:"maxLen:10"`
ValidNotEmpty = "notEmpty" // not empty, supports string, array, slice, map
ValidNotNil = "notNil" // not nil, only validates slice, map, pointer, func
// must be one of the values listed, e.g., 'valid:"member:PUBLIC|PROTECTED"', means that the tag value must be either PUBLIC or PROTECTED.
// only string type is supported.
ValidMember = "member"
ValidPositive = "positive" // greater than 0, only supports int... or string type
ValidPositiveOrZero = "positiveOrZero" // greater than or equal to 0, only supports int... or string type
ValidNegative = "negative" // less than 0, only supports int... or string type
ValidNegativeOrZero = "negativeOrZero" // less than or equal to 0, only supports int... or string type
ValidNotZero = "notZero" // not zero, only supports int... or string type
Validated = "validated" // mark a nested struct or pointer validated, nil pointer is ignored, one may combine "notNil,validated"
)
var (
rules = NewSet[string]()
ValidateWalkTagCallbackDeprecated = WalkTagCallback{
Tag: TagValidationV1,
OnWalked: validateOnWalked,
}
ValidateWalkTagCallback = WalkTagCallback{
Tag: TagValidationV2,
OnWalked: validateOnWalked,
}
)
func init() {
rules.AddThen(ValidMaxLen).
AddThen(ValidNotEmpty).
AddThen(ValidNotNil).
AddThen(ValidMember).
AddThen(ValidPositive).
AddThen(ValidPositiveOrZero).
AddThen(ValidNegative).
AddThen(ValidNegativeOrZero).
AddThen(ValidNotZero).
Add(Validated)
}
// Validation Error
type ValidationError struct {
Field string
Rule string
ValidationMsg string
}
func ChainValidationError(parentField string, e error) error {
if e == nil {
return nil
}
if ve, ok := e.(*ValidationError); ok {
return &ValidationError{Field: parentField + "." + ve.Field, Rule: ve.Rule, ValidationMsg: ve.ValidationMsg}
}
return e
}
func (ve *ValidationError) Error() string {
return ve.Field + " " + ve.ValidationMsg
}
/*
Validate target object based on the validation rules specified by tags 'valid:"[RULE]"'.
Available Rules:
- maxLen
- notEmpty
- notNil
- positive
- positiveOrZero
- negative
- negativeOrZero
- notZero
- validated
*/
func Validate(target any) error {
introspector := Introspect(target)
targetVal := reflect.ValueOf(target)
var verr error
forEach := func(i int, field reflect.StructField) (breakIteration bool) {
vtag := field.Tag.Get(TagValidationV2) // new tag
if vtag == "" {
vtag = field.Tag.Get(TagValidationV1) // old tag
}
if vtag == "" { // no tag found
return false
}
taggedRules := strings.Split(vtag, ",")
fval := targetVal.Field(i)
// for each rule
for _, rul := range taggedRules {
rul = strings.TrimSpace(rul)
// the tagged rule may contain extra parameters, e.g., 'maxLen:10'
splited := strings.Split(rul, ":")
for i := range splited {
splited[i] = strings.TrimSpace(splited[i])
}
rul = splited[0] // rule is the one before ':'
param := "" // param is those joined after the first ':'
if len(splited) > 1 { // contains extra parameters
param = strings.Join(splited[1:], ":")
}
if rules.Has(rul) { // is a valid rule
if e := ValidateRule(field, fval, rul, param); e != nil {
verr = e
return true
}
}
}
return false
}
introspector.IterFields(forEach)
return verr
}
func ValidateRule(field reflect.StructField, value reflect.Value, rule string, ruleParam string) error {
fname := field.Name
if !IsFieldExposed(fname) {
return nil
}
// Infof("Validating '%s' with value '%v' against rule '%s'", fname, value, rule)
switch rule {
case ValidMaxLen:
maxLen, e := strconv.Atoi(ruleParam)
if e == nil && maxLen > -1 {
switch value.Kind() {
case reflect.Slice, reflect.Array:
currLen := value.Len()
if currLen > maxLen {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: fmt.Sprintf("exceeded maximum length %d, current length: %d", maxLen, currLen)}
}
case reflect.String:
currLen := utf8.RuneCountInString(value.String())
if currLen > maxLen {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: fmt.Sprintf("exceeded maximum length %d, current length: %d", maxLen, currLen)}
}
}
}
case ValidNotEmpty:
switch value.Kind() {
case reflect.String:
sval := value.String()
if trimed := strings.TrimSpace(sval); trimed == "" {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "must not be empty"}
}
case reflect.Array, reflect.Slice, reflect.Map:
if value.Len() < 1 {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "must not be empty"}
}
}
case ValidNotNil:
switch value.Kind() {
case reflect.Pointer, reflect.Slice, reflect.Map, reflect.Func:
if value.IsNil() {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "cannot be nil"}
}
}
case ValidMember:
members := strings.Split(ruleParam, "|")
if len(members) < 1 {
return nil
}
switch value.Kind() {
case reflect.String:
sval := value.String()
trimed := strings.TrimSpace(sval)
for _, v := range members {
if v == trimed {
return nil
}
}
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: fmt.Sprintf("must be one of %v", members)}
}
case ValidPositive, ValidPositiveOrZero, ValidNegative, ValidNegativeOrZero, ValidNotZero:
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return ValidateIntRule(value.Int(), rule, fname, ruleParam)
case reflect.String:
ival, e := strconv.Atoi(value.String())
if e != nil {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "is not an integer"}
}
return ValidateIntRule(int64(ival), rule, fname, ruleParam)
}
case Validated:
switch value.Kind() {
case reflect.Struct:
// nested struct, validate recursively
return ChainValidationError(fname, Validate(value.Interface()))
case reflect.Pointer:
// not nil pointer, dereference and validate recursively
if !value.IsNil() {
return ChainValidationError(fname, Validate(value.Elem().Interface()))
}
}
}
return nil
}
func ValidateIntRule(ival int64, rule string, fname string, param string) error {
switch rule {
case ValidPositive:
if ival <= 0 {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "must be greater than zero"}
}
case ValidPositiveOrZero:
if ival < 0 {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "must be grater than or equal to zero"}
}
case ValidNegative:
if ival >= 0 {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "must be less than zero"}
}
case ValidNegativeOrZero:
if ival > 0 {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "must be less than or equal to zero"}
}
case ValidNotZero:
if ival == 0 {
return &ValidationError{Field: fname, Rule: rule, ValidationMsg: "must not be zero"}
}
}
return nil
}
func validateOnWalked(tagVal string, fieldVal reflect.Value, fieldType reflect.StructField) error {
taggedRules := strings.Split(tagVal, ",")
// for each rule
for _, rul := range taggedRules {
rul = strings.TrimSpace(rul)
// the tagged rule may contain extra parameters, e.g., 'maxLen:10'
splited := strings.Split(rul, ":")
for i := range splited {
splited[i] = strings.TrimSpace(splited[i])
}
rul = splited[0] // rule is the one before ':'
param := "" // param is those joined after the first ':'
if len(splited) > 1 { // contains extra parameters
param = strings.Join(splited[1:], ":")
}
if rules.Has(rul) { // is a valid rule
if err := ValidateRule(fieldType, fieldVal, rul, param); err != nil {
return err
}
}
}
return nil
}