-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
126 lines (110 loc) · 2.96 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
package configuro
import (
"reflect"
"go.uber.org/multierr"
"gopkg.in/go-playground/validator.v9"
)
//Validatable Any Type that Implements this interface will have its WithValidateByTags() function called when validating config.
type Validatable interface {
Validate() error
}
//Validate Validates Struct using Tags and Any Fields that Implements the Validatable interface.
func (c *Config) Validate(configStruct interface{}) error {
var errs error
if c.validateUsingTags {
errs = multierr.Append(c.validateTags(configStruct), errs)
}
if c.validateUsingFunc {
err := recursiveValidate(configStruct, c.validateRecursive, c.validateFuncStopOnFirstErr)
if err != nil {
errs = multierr.Append(errs, err)
}
}
// cast errs to ErrValidationErrs if it is multierr (So we use the package error instead of 3rd party error type)
if len(multierr.Errors(errs)) > 1 {
return ErrValidationErrors{error: errs}
}
return errs
}
func (c *Config) validateTags(configStruct interface{}) error {
var errs error
// validate tags
err := c.validator.Struct(configStruct)
if err != nil {
errorLists, ok := err.(validator.ValidationErrors)
if ok {
for _, tagErr := range errorLists {
// Add translated Tag error.
errs = multierr.Append(errs, newErrFieldTagValidation(tagErr, tagErr.Translate(c.validatorTrans)))
}
} else {
errs = multierr.Append(errs, err)
}
}
return errs
}
func recursiveValidate(obj interface{}, recursive bool, returnOnFirstErr bool) error {
var errs error
if reflect.ValueOf(obj).Kind() == reflect.Ptr && reflect.ValueOf(obj).IsNil() {
return nil
}
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
// Recursively WithValidateByTags Obj Fields / Elements
switch val.Kind() {
case reflect.Struct:
if recursive {
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if field.CanInterface() {
err := recursiveValidate(field.Interface(), recursive, returnOnFirstErr)
if err != nil {
errs = multierr.Append(errs, err)
if returnOnFirstErr {
return errs
}
}
}
}
}
case reflect.Map:
for _, e := range val.MapKeys() {
v := val.MapIndex(e)
if v.CanInterface() {
err := recursiveValidate(v.Interface(), recursive, returnOnFirstErr)
if err != nil {
errs = multierr.Append(errs, err)
if returnOnFirstErr {
return err
}
}
}
}
case reflect.Array, reflect.Slice:
for i := 0; i < val.Len(); i++ {
v := val.Index(i)
if v.CanInterface() {
err := recursiveValidate(v.Interface(), recursive, returnOnFirstErr)
if err != nil {
errs = multierr.Append(errs, err)
if returnOnFirstErr {
return err
}
}
}
}
}
// Call WithValidateByTags on the value it self
if val.CanInterface() {
validatable, ok := val.Interface().(Validatable)
if ok {
err := validatable.Validate()
if err != nil {
errs = multierr.Append(errs, newErrValidate("", err))
}
}
}
return errs
}