-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgvalid_validator_check_value.go
313 lines (292 loc) · 10.1 KB
/
gvalid_validator_check_value.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gvalid
import (
"context"
"errors"
"reflect"
"strings"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gvalid/internal/builtin"
)
type doCheckValueInput struct {
Name string // Name specifies the name of parameter `value`, which might be the custom tag name of the parameter.
Value interface{} // Value specifies the value for the rules to be validated.
ValueType reflect.Type // ValueType specifies the type of the value, mainly used for value type id retrieving.
Rule string // Rule specifies the validation rules string, like "required", "required|between:1,100", etc.
Messages interface{} // Messages specifies the custom error messages for this rule from parameters input, which is usually type of map/slice.
DataRaw interface{} // DataRaw specifies the `raw data` which is passed to the Validator. It might be type of map/struct or a nil value.
DataMap map[string]interface{} // DataMap specifies the map that is converted from `dataRaw`. It is usually used internally
}
// doCheckValue does the really rules validation for single key-value.
func (v *Validator) doCheckValue(ctx context.Context, in doCheckValueInput) Error {
// If there's no validation rules, it does nothing and returns quickly.
if in.Rule == "" {
return nil
}
// It converts value to string and then does the validation.
var (
// Do not trim it as the space is also part of the value.
ruleErrorMap = make(map[string]error)
)
// Custom error messages handling.
var (
msgArray = make([]string, 0)
customMsgMap = make(map[string]string)
)
switch messages := in.Messages.(type) {
case string:
msgArray = strings.Split(messages, "|")
default:
for k, message := range gconv.Map(in.Messages) {
customMsgMap[k] = gconv.String(message)
}
}
// Handle the char '|' in the rule,
// which makes this rule separated into multiple rules.
ruleItems := strings.Split(strings.TrimSpace(in.Rule), "|")
for i := 0; ; {
array := strings.Split(ruleItems[i], ":")
if builtin.GetRule(array[0]) == nil && v.getCustomRuleFunc(array[0]) == nil {
// ============================ SPECIAL ============================
// Special `regex` and `not-regex` rules.
// Merge the regex pattern if there are special chars, like ':', '|', in pattern.
// ============================ SPECIAL ============================
var (
ruleNameRegexLengthMatch bool
ruleNameNotRegexLengthMatch bool
)
if i > 0 {
ruleItem := ruleItems[i-1]
if len(ruleItem) >= len(ruleNameRegex) && ruleItem[:len(ruleNameRegex)] == ruleNameRegex {
ruleNameRegexLengthMatch = true
}
if len(ruleItem) >= len(ruleNameNotRegex) && ruleItem[:len(ruleNameNotRegex)] == ruleNameNotRegex {
ruleNameNotRegexLengthMatch = true
}
}
if i > 0 && (ruleNameRegexLengthMatch || ruleNameNotRegexLengthMatch) {
ruleItems[i-1] += "|" + ruleItems[i]
ruleItems = append(ruleItems[:i], ruleItems[i+1:]...)
} else {
return newValidationErrorByStr(
internalRulesErrRuleName,
errors.New(internalRulesErrRuleName+": "+ruleItems[i]),
)
}
} else {
i++
}
if i == len(ruleItems) {
break
}
}
var (
hasBailRule = v.bail
hasForeachRule = v.foreach
hasCaseInsensitive = v.caseInsensitive
)
for index := 0; index < len(ruleItems); {
var (
err error
results = ruleRegex.FindStringSubmatch(ruleItems[index]) // split single rule.
ruleKey = gstr.Trim(results[1]) // rule key like "max" in rule "max: 6"
rulePattern = gstr.Trim(results[2]) // rule pattern is like "6" in rule:"max:6"
)
if !hasBailRule && ruleKey == ruleNameBail {
hasBailRule = true
}
if !hasForeachRule && ruleKey == ruleNameForeach {
hasForeachRule = true
}
if !hasCaseInsensitive && ruleKey == ruleNameCi {
hasCaseInsensitive = true
}
// Ignore logic executing for marked rules.
if decorativeRuleMap[ruleKey] {
index++
continue
}
if len(msgArray) > index {
customMsgMap[ruleKey] = strings.TrimSpace(msgArray[index])
}
var (
message = v.getErrorMessageByRule(ctx, ruleKey, customMsgMap)
customRuleFunc = v.getCustomRuleFunc(ruleKey)
builtinRule = builtin.GetRule(ruleKey)
foreachValues = []interface{}{in.Value}
)
if hasForeachRule {
// As it marks `foreach`, so it converts the value to slice.
foreachValues = gconv.Interfaces(in.Value)
// Reset `foreach` rule as it only takes effect just once for next rule.
hasForeachRule = false
}
for _, value := range foreachValues {
switch {
// Custom validation rules.
case customRuleFunc != nil:
err = customRuleFunc(ctx, RuleFuncInput{
Rule: ruleItems[index],
Message: message,
Field: in.Name,
ValueType: in.ValueType,
Value: gvar.New(value),
Data: gvar.New(in.DataRaw),
})
// Builtin validation rules.
case customRuleFunc == nil && builtinRule != nil:
err = builtinRule.Run(builtin.RunInput{
RuleKey: ruleKey,
RulePattern: rulePattern,
Field: in.Name,
ValueType: in.ValueType,
Value: gvar.New(value),
Data: gvar.New(in.DataRaw),
Message: message,
Option: builtin.RunOption{
CaseInsensitive: hasCaseInsensitive,
},
})
default:
// It never comes across here.
}
// Error handling.
if err != nil {
// Error variable replacement for error message.
if errMsg := err.Error(); gstr.Contains(errMsg, "{") {
errMsg = gstr.ReplaceByMap(errMsg, map[string]string{
"{field}": in.Name, // Field name of the `value`.
"{value}": gconv.String(value), // Current validating value.
"{pattern}": rulePattern, // The variable part of the rule.
"{attribute}": in.Name, // The same as `{field}`. It is deprecated.
})
errMsg, _ = gregex.ReplaceString(`\s{2,}`, ` `, errMsg)
err = errors.New(errMsg)
}
// The error should have stack info to indicate the error position.
if !gerror.HasStack(err) {
err = gerror.NewCode(gcode.CodeValidationFailed, err.Error())
}
// The error should have error code that is `gcode.CodeValidationFailed`.
if gerror.Code(err) == gcode.CodeNil {
// TODO it's better using interface?
if e, ok := err.(*gerror.Error); ok {
e.SetCode(gcode.CodeValidationFailed)
}
}
ruleErrorMap[ruleKey] = err
// If it is with error and there's bail rule,
// it then does not continue validating for left rules.
if hasBailRule {
goto CheckDone
}
}
}
index++
}
CheckDone:
if len(ruleErrorMap) > 0 {
return newValidationError(
gcode.CodeValidationFailed,
[]fieldRule{{Name: in.Name, Rule: in.Rule}},
map[string]map[string]error{
in.Name: ruleErrorMap,
},
)
}
return nil
}
type doCheckValueRecursivelyInput struct {
Value interface{} // Value to be validated.
Type reflect.Type // Struct/map/slice type which to be recursively validated.
Kind reflect.Kind // Struct/map/slice kind to be asserted in following switch case.
ErrorMaps map[string]map[string]error // The validated failed error map.
ResultSequenceRules *[]fieldRule // The validated failed rule in sequence.
}
func (v *Validator) doCheckValueRecursively(ctx context.Context, in doCheckValueRecursivelyInput) {
switch in.Kind {
case reflect.Ptr:
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
Value: in.Value,
Type: in.Type.Elem(),
Kind: in.Type.Elem().Kind(),
ErrorMaps: in.ErrorMaps,
ResultSequenceRules: in.ResultSequenceRules,
})
case reflect.Struct:
// Ignore data, assoc, rules and messages from parent.
var (
validator = v.Clone()
toBeValidatedObject interface{}
)
if in.Type.Kind() == reflect.Ptr {
toBeValidatedObject = reflect.New(in.Type.Elem()).Interface()
} else {
toBeValidatedObject = reflect.New(in.Type).Interface()
}
validator.assoc = nil
validator.rules = nil
validator.messages = nil
if err := validator.Data(toBeValidatedObject).Assoc(in.Value).Run(ctx); err != nil {
// It merges the errors into single error map.
for k, m := range err.(*validationError).errors {
in.ErrorMaps[k] = m
}
if in.ResultSequenceRules != nil {
*in.ResultSequenceRules = append(*in.ResultSequenceRules, err.(*validationError).rules...)
}
}
case reflect.Map:
var (
dataMap = gconv.Map(in.Value)
mapTypeElem = in.Type.Elem()
mapTypeKind = mapTypeElem.Kind()
)
for _, item := range dataMap {
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
Value: item,
Type: mapTypeElem,
Kind: mapTypeKind,
ErrorMaps: in.ErrorMaps,
ResultSequenceRules: in.ResultSequenceRules,
})
// Bail feature.
if v.bail && len(in.ErrorMaps) > 0 {
break
}
}
case reflect.Slice, reflect.Array:
var array []interface{}
if gjson.Valid(in.Value) {
array = gconv.Interfaces(gconv.Bytes(in.Value))
} else {
array = gconv.Interfaces(in.Value)
}
if len(array) == 0 {
return
}
for _, item := range array {
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
Value: item,
Type: in.Type.Elem(),
Kind: in.Type.Elem().Kind(),
ErrorMaps: in.ErrorMaps,
ResultSequenceRules: in.ResultSequenceRules,
})
// Bail feature.
if v.bail && len(in.ErrorMaps) > 0 {
break
}
}
}
}