-
Notifications
You must be signed in to change notification settings - Fork 8
/
validator.go
190 lines (168 loc) · 3.97 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
package valid
import (
"bytes"
"fmt"
"reflect"
"github.com/cuigh/auxo/errors"
"github.com/cuigh/auxo/ext/reflects"
)
var (
// Tag is the default tag key for validation.
Tag = "valid"
validator = new(Validator)
)
// Register adds a rule to default validator.
func Register(name string, rule Rule) {
rules[name] = rule
}
// Prompt sets the default validation message for rules.
func Prompt(name, msg string) {
messages[name] = msg
}
// Validate checks value of struct is available.
func Validate(i interface{}) error {
return validator.Validate(i)
}
type Error struct {
Field string
Rule string
cause error
}
func (e *Error) Cause() error {
return e.cause
}
func (e *Error) Error() string {
return fmt.Sprintf("failed to validate field `%s` with rule `%s`: %v", e.Field, e.Rule, e.cause)
}
// Validator is a struct data validator.
type Validator struct {
Tag string
rules map[string]Rule
messages map[string]string
}
// Register adds a rule to validator.
func (v *Validator) Register(name string, rule Rule) {
if v.rules == nil {
v.rules = make(map[string]Rule)
}
v.rules[name] = rule
}
// Prompt sets the validation message for rules.
func (v *Validator) Prompt(name, msg string) {
if v.messages == nil {
v.messages = make(map[string]string)
}
v.messages[name] = msg
}
// Validate checks value of struct is available.
func (v *Validator) Validate(i interface{}) error {
value := reflects.Indirect(reflect.ValueOf(i))
if value.Kind() != reflect.Struct {
return errors.New("valid: target value must be a struct")
}
key := v.Tag
if key == "" {
key = Tag
}
ctx := &Context{messages: messages}
sv := reflects.StructOf(value)
return sv.VisitFields(func(fv reflect.Value, fi *reflect.StructField) error {
// ignore unexported fields.
if fi.PkgPath != "" {
return nil
}
rules, err := v.parseRules(fi.Tag.Get(key))
if err != nil {
return err
}
if len(rules) == 0 {
fv = reflects.Indirect(fv)
if fv.Kind() == reflect.Struct && fv.IsValid() {
return v.Validate(fv.Interface())
}
return nil
}
ctx.Value, ctx.Info = fv, fi
for name, info := range rules {
if r := v.getRule(name); r != nil {
if err = r(ctx, &info); err != nil {
return &Error{Field: fi.Name, Rule: name, cause: err}
}
} else {
return errors.New("unknown rule: " + name)
}
}
return nil
})
}
func (v *Validator) getRule(name string) Rule {
if v.rules != nil {
if r, ok := v.rules[name]; ok {
return r
}
}
return rules[name]
}
func (v *Validator) parseRules(tag string) (rules map[string]Argument, err error) {
// tag: length[1,5),regex(\d+),ip,width(5)
const (
stateName = 0
//stateLeftFlag = 1
stateRightFlag = 2
stateArg = 3
)
var (
state = stateName
last byte
name string
left, right ArgumentFlag
buf bytes.Buffer
)
rules = make(map[string]Argument)
for i, l := 0, len(tag); i < l; i++ {
b := tag[i]
switch state {
case stateName:
if b == LeftOpen || b == LeftSquare {
state = stateArg
left = ArgumentFlag(b)
name = buf.String()
buf.Reset()
} else if b == ',' {
rules[buf.String()] = Argument{}
buf.Reset()
} else {
buf.WriteByte(b)
}
case stateArg:
if b == RightOpen || b == RightSquare {
state = stateRightFlag
right = ArgumentFlag(b)
} else {
buf.WriteByte(b)
}
case stateRightFlag:
if b == ',' {
state = stateName
rules[name] = Argument{Value: buf.String(), Left: left, Right: right}
buf.Reset()
} else if (b == RightOpen || b == RightSquare) && last == b {
state = stateArg
buf.WriteByte(b)
} else {
return nil, fmt.Errorf("invalid rule: %s(pos: %d)", tag, i)
}
}
last = tag[i]
}
if state == stateName {
if buf.Len() > 0 {
rules[buf.String()] = Argument{}
}
} else if state == stateRightFlag {
rules[name] = Argument{Value: buf.String(), Left: left, Right: right}
} else {
return nil, fmt.Errorf("invalid rule: %s(pos: %d)", tag, len(tag)-1)
}
return
}