-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.go
295 lines (260 loc) · 7.03 KB
/
rule.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
package validate
import (
"strings"
)
// Rules definition
type Rules []*Rule
/*************************************************************
* validation rule
*************************************************************/
// Rule definition
type Rule struct {
// eg "create" "update"
scene string
// need validate fields. allow multi.
fields []string
// is optional, only validate on value is not empty. sometimes
optional bool
// skip validate not exist field/empty value
skipEmpty bool
// error message
message string
// error messages, if fields contains multi field.
// eg {
// "field": "error message",
// "field.validator": "error message",
// }
messages map[string]string
// the input validator name
validator string
// the real validator name
realName string
// real validator name is requiredXXX validators
nameNotRequired bool
// arguments for the validator
arguments []any
// --- some hooks function
// has beforeFunc. if return false, skip validate current rule
beforeFunc func(v *Validation) bool // func (val any) bool
// you can custom filter func
filterFunc func(val any) (any, error)
// custom check function's mate info
checkFuncMeta *funcMeta
// custom check is empty. TODO
// emptyChecker func(val any) bool
}
// NewRule create new Rule instance
func NewRule(fields, validator string, args ...any) *Rule {
return &Rule{
fields: stringSplit(fields, ","),
// validator args
arguments: args,
validator: validator,
}
}
// SetScene name for the rule.
func (r *Rule) SetScene(scene string) *Rule {
r.scene = scene
return r
}
// SetOptional only validate on value is not empty.
func (r *Rule) SetOptional(optional bool) {
r.optional = optional
}
// SetSkipEmpty skip validate not exist field/empty value
func (r *Rule) SetSkipEmpty(skipEmpty bool) {
r.skipEmpty = skipEmpty
}
// SetDefValue for the rule
// func (r *Rule) SetDefValue(defValue any) {
// r.defValue = defValue
// }
// SetCheckFunc set custom validate func.
func (r *Rule) SetCheckFunc(checkFunc any) *Rule {
var name string
if r.validator != "" {
name = "rule_" + r.validator
} else {
name = "rule_" + strings.Join(r.fields, "_")
}
fv := checkValidatorFunc(name, checkFunc)
r.checkFuncMeta = newFuncMeta(name, false, fv)
return r
}
// SetFilterFunc for the rule
func (r *Rule) SetFilterFunc(fn func(val any) (any, error)) *Rule {
r.filterFunc = fn
return r
}
// SetBeforeFunc for the rule. will call it before validate.
func (r *Rule) SetBeforeFunc(fn func(v *Validation) bool) {
r.beforeFunc = fn
}
// SetMessage set error message.
//
// Usage:
//
// v.AddRule("name", "required").SetMessage("error message")
func (r *Rule) SetMessage(errMsg string) *Rule {
r.message = errMsg
return r
}
// SetMessages set error message map.
//
// Usage:
//
// v.AddRule("name,email", "required").SetMessages(MS{
// "name": "error message 1",
// "email": "error message 2",
// })
func (r *Rule) SetMessages(msgMap MS) *Rule {
r.messages = msgMap
return r
}
// Fields field names list
func (r *Rule) Fields() []string {
return r.fields
}
func (r *Rule) errorMessage(field, validator string, v *Validation) (msg string) {
if r.messages != nil {
var ok bool
// use the full key. "field.validator"
fKey := field + "." + validator
if msg, ok = r.messages[fKey]; ok {
return
}
if msg, ok = r.messages[field]; ok {
return
}
}
if r.message != "" {
return r.message
}
// built in error messages
return v.trans.Message(validator, field, r.arguments...)
}
/*************************************************************
* add validate rules
*************************************************************/
// StringRule add field rules by string
//
// Usage:
//
// v.StringRule("name", "required|string|minLen:6")
// // will try convert to int before applying validation.
// v.StringRule("age", "required|int|min:12", "toInt")
// v.StringRule("email", "required|min_len:6", "trim|email|lower")
func (v *Validation) StringRule(field, rule string, filterRule ...string) *Validation {
rule = strings.TrimSpace(rule)
if rule == "" {
if len(filterRule) > 0 {
v.FilterRule(field, filterRule[0])
}
return v
}
rules := stringSplit(strings.Trim(rule, "|:"), "|")
for _, validator := range rules {
validator = strings.Trim(validator, ":")
if validator == "" { // empty
continue
}
// has args "min:12"
if strings.ContainsRune(validator, ':') {
list := stringSplit(validator, ":")
// reassign value
validator := list[0]
realName := ValidatorName(validator)
switch realName {
// add default value for the field
case "default":
v.SetDefValue(field, list[1])
// eg 'regex:\d{4,6}' dont need split args. args is "\d{4,6}"
case RuleRegexp:
v.AddRule(field, validator, list[1])
// some special validator. need merge args to one.
case "enum", "notIn":
v.AddRule(field, validator, parseArgString(list[1]))
default:
args := parseArgString(list[1])
v.AddRule(field, validator, strings2Args(args)...)
}
} else {
v.AddRule(field, validator)
}
}
if len(filterRule) > 0 {
v.FilterRule(field, filterRule[0])
}
return v
}
// StringRules add multi rules by string map.
//
// Usage:
//
// v.StringRules(map[string]string{
// "name": "required|string|min_len:12",
// "age": "required|int|min:12",
// })
func (v *Validation) StringRules(mp MS) *Validation {
for name, rule := range mp {
v.StringRule(name, rule)
}
return v
}
// ConfigRules add multi rules by string map. alias of StringRules()
//
// Usage:
//
// v.ConfigRules(map[string]string{
// "name": "required|string|min:12",
// "age": "required|int|min:12",
// })
func (v *Validation) ConfigRules(mp MS) *Validation {
for name, rule := range mp {
v.StringRule(name, rule)
}
return v
}
// AddRule for current validation
//
// Usage:
//
// v.AddRule("name", "required")
// v.AddRule("name", "min_len", "2")
func (v *Validation) AddRule(fields, validator string, args ...any) *Rule {
return v.addOneRule(fields, validator, ValidatorName(validator), args)
}
// add one Rule for current validation
func (v *Validation) addOneRule(fields, validator, realName string, args []any) *Rule {
rule := NewRule(fields, validator, args...)
// init some settings
rule.realName = realName
rule.skipEmpty = v.SkipOnEmpty
// validator name is not "required"
rule.nameNotRequired = !strings.HasPrefix(realName, "required")
// append
v.rules = append(v.rules, rule)
return rule
}
// AppendRule instance
func (v *Validation) AppendRule(rule *Rule) *Rule {
rule.realName = ValidatorName(rule.validator)
rule.skipEmpty = v.SkipOnEmpty
// validator name is not "required"
rule.nameNotRequired = !strings.HasPrefix(rule.realName, "required")
// append
v.rules = append(v.rules, rule)
return rule
}
// AppendRules instances at once
func (v *Validation) AppendRules(rules ...*Rule) *Validation {
for _, rule := range rules {
rule.realName = ValidatorName(rule.validator)
rule.skipEmpty = v.SkipOnEmpty
// validator name is not "required"
rule.nameNotRequired = !strings.HasPrefix(rule.realName, "required")
}
// appends
v.rules = append(v.rules, rules...)
return v
}