-
Notifications
You must be signed in to change notification settings - Fork 127
/
validator.go
480 lines (427 loc) · 13.8 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
// Package validator implements value validations
//
// Copyright 2014 Roberto Teixeira <robteix@robteix.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validator
import (
"bytes"
"errors"
"fmt"
"reflect"
"regexp"
"strings"
)
// TextErr is an error that also implements the TextMarshaller interface for
// serializing out to various plain text encodings. Packages creating their
// own custom errors should use TextErr if they're intending to use serializing
// formats like json, msgpack etc.
type TextErr struct {
Err error
}
// Error implements the error interface.
func (t TextErr) Error() string {
return t.Err.Error()
}
// MarshalText implements the TextMarshaller
func (t TextErr) MarshalText() ([]byte, error) {
return []byte(t.Err.Error()), nil
}
var (
// ErrZeroValue is the error returned when variable has zero value
// and nonzero or nonnil was specified
ErrZeroValue = TextErr{errors.New("zero value")}
// ErrMin is the error returned when variable is less than mininum
// value specified
ErrMin = TextErr{errors.New("less than min")}
// ErrMax is the error returned when variable is more than
// maximum specified
ErrMax = TextErr{errors.New("greater than max")}
// ErrLen is the error returned when length is not equal to
// param specified
ErrLen = TextErr{errors.New("invalid length")}
// ErrRegexp is the error returned when the value does not
// match the provided regular expression parameter
ErrRegexp = TextErr{errors.New("regular expression mismatch")}
// ErrUnsupported is the error error returned when a validation rule
// is used with an unsupported variable type
ErrUnsupported = TextErr{errors.New("unsupported type")}
// ErrBadParameter is the error returned when an invalid parameter
// is provided to a validation rule (e.g. a string where an int was
// expected (max=foo,len=bar) or missing a parameter when one is required (len=))
ErrBadParameter = TextErr{errors.New("bad parameter")}
// ErrUnknownTag is the error returned when an unknown tag is found
ErrUnknownTag = TextErr{errors.New("unknown tag")}
// ErrInvalid is the error returned when variable is invalid
// (normally a nil pointer)
ErrInvalid = TextErr{errors.New("invalid value")}
// ErrCannotValidate is the error returned when a struct is unexported
ErrCannotValidate = TextErr{errors.New("cannot validate unexported struct")}
)
// ErrorMap is a map which contains all errors from validating a struct.
type ErrorMap map[string]ErrorArray
// ErrorMap implements the Error interface so we can check error against nil.
// The returned error is all existing errors with the map.
func (err ErrorMap) Error() string {
var b bytes.Buffer
for k, errs := range err {
if len(errs) > 0 {
b.WriteString(fmt.Sprintf("%s: %s, ", k, errs.Error()))
}
}
return strings.TrimSuffix(b.String(), ", ")
}
// ErrorArray is a slice of errors returned by the Validate function.
type ErrorArray []error
// ErrorArray implements the Error interface and returns all the errors comma seprated
// if errors exist.
func (err ErrorArray) Error() string {
var b bytes.Buffer
for _, errs := range err {
b.WriteString(fmt.Sprintf("%s, ", errs.Error()))
}
errs := b.String()
return strings.TrimSuffix(errs, ", ")
}
// ValidationFunc is a function that receives the value of a
// field and a parameter used for the respective validation tag.
type ValidationFunc func(v interface{}, param string) error
// Validator implements a validator
type Validator struct {
// validationFuncs is a map of ValidationFuncs indexed
// by their name.
validationFuncs map[string]ValidationFunc
// Tag name being used.
tagName string
// printJSON set to true will make errors print with the
// name of their json field instead of their struct tag.
// If no json tag is present the name of the struct field is used.
printJSON bool
}
// Helper validator so users can use the
// functions directly from the package
var defaultValidator = NewValidator()
// NewValidator creates a new Validator
func NewValidator() *Validator {
return &Validator{
tagName: "validate",
validationFuncs: map[string]ValidationFunc{
"nonzero": nonzero,
"len": length,
"min": min,
"max": max,
"regexp": regex,
"nonnil": nonnil,
},
printJSON: false,
}
}
// SetTag allows you to change the tag name used in structs
func SetTag(tag string) {
defaultValidator.SetTag(tag)
}
// SetTag allows you to change the tag name used in structs
func (mv *Validator) SetTag(tag string) {
mv.tagName = tag
}
// WithTag creates a new Validator with the new tag name. It is
// useful to chain-call with Validate so we don't change the tag
// name permanently: validator.WithTag("foo").Validate(t)
func WithTag(tag string) *Validator {
return defaultValidator.WithTag(tag)
}
// WithTag creates a new Validator with the new tag name. It is
// useful to chain-call with Validate so we don't change the tag
// name permanently: validator.WithTag("foo").Validate(t)
func (mv *Validator) WithTag(tag string) *Validator {
v := mv.copy()
v.SetTag(tag)
return v
}
// SetPrintJSON allows you to print errors with json tag names present in struct tags
func SetPrintJSON(printJSON bool) {
defaultValidator.SetPrintJSON(printJSON)
}
// SetPrintJSON allows you to print errors with json tag names present in struct tags
func (mv *Validator) SetPrintJSON(printJSON bool) {
mv.printJSON = printJSON
}
// WithPrintJSON creates a new Validator with printJSON set to new value. It is
// useful to chain-call with Validate so we don't change the print option
// permanently: validator.WithPrintJSON(true).Validate(t)
func WithPrintJSON(printJSON bool) *Validator {
return defaultValidator.WithPrintJSON(printJSON)
}
// WithPrintJSON creates a new Validator with printJSON set to new value. It is
// useful to chain-call with Validate so we don't change the print option
// permanently: validator.WithTag("foo").WithPrintJSON(true).Validate(t)
func (mv *Validator) WithPrintJSON(printJSON bool) *Validator {
v := mv.copy()
v.SetPrintJSON(printJSON)
return v
}
// Copy a validator
func (mv *Validator) copy() *Validator {
newFuncs := map[string]ValidationFunc{}
for k, f := range mv.validationFuncs {
newFuncs[k] = f
}
return &Validator{
tagName: mv.tagName,
validationFuncs: newFuncs,
printJSON: mv.printJSON,
}
}
// SetValidationFunc sets the function to be used for a given
// validation constraint. Calling this function with nil vf
// is the same as removing the constraint function from the list.
func SetValidationFunc(name string, vf ValidationFunc) error {
return defaultValidator.SetValidationFunc(name, vf)
}
// SetValidationFunc sets the function to be used for a given
// validation constraint. Calling this function with nil vf
// is the same as removing the constraint function from the list.
func (mv *Validator) SetValidationFunc(name string, vf ValidationFunc) error {
if name == "" {
return errors.New("name cannot be empty")
}
if vf == nil {
delete(mv.validationFuncs, name)
return nil
}
mv.validationFuncs[name] = vf
return nil
}
// Validate calls the Validate method on the default validator.
func Validate(v interface{}) error {
return defaultValidator.Validate(v)
}
// Validate validates the fields of structs (included embedded structs) based on
// 'validator' tags and returns errors found indexed by the field name.
func (mv *Validator) Validate(v interface{}) error {
m := make(ErrorMap)
mv.deepValidateCollection(reflect.ValueOf(v), m, func() string {
return ""
})
if len(m) > 0 {
return m
}
return nil
}
func (mv *Validator) validateStruct(sv reflect.Value, m ErrorMap) error {
kind := sv.Kind()
if (kind == reflect.Ptr || kind == reflect.Interface) && !sv.IsNil() {
return mv.validateStruct(sv.Elem(), m)
}
if kind != reflect.Struct && kind != reflect.Interface {
return ErrUnsupported
}
st := sv.Type()
nfields := st.NumField()
for i := 0; i < nfields; i++ {
if err := mv.validateField(st.Field(i), sv.Field(i), m); err != nil {
return err
}
}
return nil
}
// validateField validates the field of fieldVal referred to by fieldDef.
// If fieldDef refers to an anonymous/embedded field,
// validateField will walk all of the embedded type's fields and validate them on sv.
func (mv *Validator) validateField(fieldDef reflect.StructField, fieldVal reflect.Value, m ErrorMap) error {
tag := fieldDef.Tag.Get(mv.tagName)
if tag == "-" {
return nil
}
// deal with pointers
for (fieldVal.Kind() == reflect.Ptr || fieldVal.Kind() == reflect.Interface) && !fieldVal.IsNil() {
fieldVal = fieldVal.Elem()
}
// ignore private structs unless Anonymous
if !fieldDef.Anonymous && fieldDef.PkgPath != "" {
return nil
}
var errs ErrorArray
if tag != "" {
var err error
if fieldDef.PkgPath != "" {
err = ErrCannotValidate
} else {
err = mv.validValue(fieldVal, tag)
}
if errarr, ok := err.(ErrorArray); ok {
errs = errarr
} else if err != nil {
errs = ErrorArray{err}
}
}
// no-op if field is not a struct, interface, array, slice or map
fn := mv.fieldName(fieldDef)
mv.deepValidateCollection(fieldVal, m, func() string {
return fn
})
if len(errs) > 0 {
m[fn] = errs
}
return nil
}
func (mv *Validator) fieldName(fieldDef reflect.StructField) string {
if mv.printJSON {
if jsonTagValue, ok := fieldDef.Tag.Lookup("json"); ok {
return parseName(jsonTagValue)
}
}
return fieldDef.Name
}
func (mv *Validator) deepValidateCollection(f reflect.Value, m ErrorMap, fnameFn func() string) {
switch f.Kind() {
case reflect.Interface, reflect.Ptr:
if f.IsNil() {
return
}
mv.deepValidateCollection(f.Elem(), m, fnameFn)
case reflect.Struct:
subm := make(ErrorMap)
err := mv.validateStruct(f, subm)
parentName := fnameFn()
if err != nil {
m[parentName] = ErrorArray{err}
}
for j, k := range subm {
keyName := j
if parentName != "" {
keyName = parentName + "." + keyName
}
m[keyName] = k
}
case reflect.Array, reflect.Slice:
// we don't need to loop over every byte in a byte slice so we only end up
// looping when the kind is something we care about
switch f.Type().Elem().Kind() {
case reflect.Struct, reflect.Interface, reflect.Ptr, reflect.Map, reflect.Array, reflect.Slice:
for i := 0; i < f.Len(); i++ {
mv.deepValidateCollection(f.Index(i), m, func() string {
return fmt.Sprintf("%s[%d]", fnameFn(), i)
})
}
}
case reflect.Map:
for _, key := range f.MapKeys() {
mv.deepValidateCollection(key, m, func() string {
return fmt.Sprintf("%s[%+v](key)", fnameFn(), key.Interface())
}) // validate the map key
value := f.MapIndex(key)
mv.deepValidateCollection(value, m, func() string {
return fmt.Sprintf("%s[%+v](value)", fnameFn(), key.Interface())
})
}
}
}
// Valid validates a value based on the provided
// tags and returns errors found or nil.
func Valid(val interface{}, tags string) error {
return defaultValidator.Valid(val, tags)
}
// Valid validates a value based on the provided
// tags and returns errors found or nil.
func (mv *Validator) Valid(val interface{}, tags string) error {
if tags == "-" {
return nil
}
v := reflect.ValueOf(val)
if (v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface) && !v.IsNil() {
return mv.validValue(v.Elem(), tags)
}
if v.Kind() == reflect.Invalid {
return mv.validateVar(nil, tags)
}
return mv.validateVar(val, tags)
}
// validValue is like Valid but takes a Value instead of an interface
func (mv *Validator) validValue(v reflect.Value, tags string) error {
if v.Kind() == reflect.Invalid {
return mv.validateVar(nil, tags)
}
return mv.validateVar(v.Interface(), tags)
}
// validateVar validates one single variable
func (mv *Validator) validateVar(v interface{}, tag string) error {
tags, err := mv.parseTags(tag)
if err != nil {
// unknown tag found, give up.
return err
}
errs := make(ErrorArray, 0, len(tags))
for _, t := range tags {
if err := t.Fn(v, t.Param); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return errs
}
return nil
}
// tag represents one of the tag items
type tag struct {
Name string // name of the tag
Fn ValidationFunc // validation function to call
Param string // parameter to send to the validation function
}
// separate by no escaped commas
var sepPattern *regexp.Regexp = regexp.MustCompile(`((?:^|[^\\])(?:\\\\)*),`)
func splitUnescapedComma(str string) []string {
ret := []string{}
indexes := sepPattern.FindAllStringIndex(str, -1)
last := 0
for _, is := range indexes {
ret = append(ret, str[last:is[1]-1])
last = is[1]
}
ret = append(ret, str[last:])
return ret
}
// parseTags parses all individual tags found within a struct tag.
func (mv *Validator) parseTags(t string) ([]tag, error) {
tl := splitUnescapedComma(t)
tags := make([]tag, 0, len(tl))
for _, i := range tl {
i = strings.Replace(i, `\,`, ",", -1)
tg := tag{}
v := strings.SplitN(i, "=", 2)
tg.Name = strings.Trim(v[0], " ")
if tg.Name == "" {
return []tag{}, ErrUnknownTag
}
if len(v) > 1 {
tg.Param = strings.Trim(v[1], " ")
}
var found bool
if tg.Fn, found = mv.validationFuncs[tg.Name]; !found {
return []tag{}, ErrUnknownTag
}
tags = append(tags, tg)
}
return tags, nil
}
func parseName(tag string) string {
if tag == "" {
return ""
}
name := strings.SplitN(tag, ",", 2)[0]
// if the field as be skipped in json, just return an empty string
if name == "-" {
return ""
}
return name
}