-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaravalidate.go
462 lines (392 loc) · 11.3 KB
/
laravalidate.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
package laravalidate
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"golang.org/x/text/language"
)
type Validator struct {
inputValue reflect.Value
ctx context.Context
errors []FieldErrors
languages []string
mode Mode
// Cache
customValidationMessagesCache []CustomError
}
func newValidator(ctx context.Context, languages []language.Tag, value reflect.Value, mode Mode) *Validator {
return &Validator{
inputValue: value,
ctx: ctx,
errors: []FieldErrors{},
languages: lookupLanguages(languages),
mode: mode,
}
}
type Mode uint8
const (
GoMode Mode = iota
JsonMode // Tries to use json struct tags
FormMode // Tries to use form struct tags
)
// JsonValidate should be used to validate a json parsed message, errors returned will have a json paths.
// These look at the `json="xx"` struct tag for hints how to name the error keys.
//
// If an error is returned the type should be of *ValidationError.
//
// Ctx can be set to nil, default value will be context.Background().
// Languages can be set to nil, default value will be []language.Tag{language.English}.
func JsonValidate(ctx context.Context, languages []language.Tag, input any) error {
return validate(ctx, languages, input, JsonMode)
}
// FormValidate should be used to validate a form parsed message, errors returned will have a form paths.
// These look at the `form="xx"` struct tag for hints how to name the error keys.
//
// If an error is returned the type should be of *ValidationError.
//
// Ctx can be set to nil, default value will be context.Background().
// Languages can be set to nil, default value will be []language.Tag{language.English}.
func FormValidate(ctx context.Context, languages []language.Tag, input any) error {
return validate(ctx, languages, input, FormMode)
}
// GoValidate should be used to validate something within a go codebase with validation errors that apply to the go codebase.
//
// If an error is returned the type should be of *ValidationError.
//
// Ctx can be set to nil, default value will be context.Background().
// Languages can be set to nil, default value will be []language.Tag{language.English}.
func GoValidate(ctx context.Context, languages []language.Tag, input any) error {
return validate(ctx, languages, input, GoMode)
}
func validate(ctx context.Context, languages []language.Tag, input any, mode Mode) error {
value := reflect.ValueOf(input)
if ctx == nil {
ctx = context.Background()
}
v := newValidator(ctx, languages, value, mode)
for value.Kind() == reflect.Ptr {
if !value.IsNil() {
value = value.Elem()
continue
}
v.Nil(Stack{}, value.Type().Elem())
return v.Error()
}
switch value.Kind() {
case reflect.Slice, reflect.Array:
v.List(Stack{}, value, nil)
case reflect.Struct:
v.Struct(Stack{}, value)
default:
return nil
}
return v.Error()
}
func (v *Validator) Error() error {
if len(v.errors) == 0 {
return nil
}
return &ValidationError{
Mode: v.mode,
Language: v.languages,
Errors: v.errors,
}
}
func (v *Validator) Nil(stack Stack, valueType reflect.Type) {
if len(stack) > 100 {
return
}
for valueType.Kind() == reflect.Ptr {
valueType = valueType.Elem()
}
if valueType.Kind() != reflect.Struct {
return
}
v.NilStruct(stack, valueType)
}
func (v *Validator) List(stack Stack, value reflect.Value, validateInner []validationRule) {
if value.IsNil() {
return
}
if len(stack) > 100 {
return
}
var innerStack Stack
var element reflect.Value
outer:
for idx := 0; idx < value.Len(); idx++ {
element = value.Index(idx)
innerStack = stack.AppendIndex(idx, &value, value.Type())
v.Validate(innerStack, &element, element.Type(), validateInner)
for element.Kind() == reflect.Ptr {
if element.IsNil() {
v.Nil(innerStack, element.Type().Elem())
continue outer
}
element = element.Elem()
}
switch element.Kind() {
case reflect.Struct:
v.Struct(innerStack, element)
case reflect.Slice, reflect.Array:
v.List(innerStack, element, nil)
}
}
}
func (v *Validator) Struct(stack Stack, value reflect.Value) {
if len(stack) > 100 {
return
}
var fieldType reflect.StructField
var field reflect.Value
var innerStack Stack
outer:
for idx := 0; idx < value.NumField(); idx++ {
fieldType = value.Type().Field(idx)
field = value.Field(idx)
innerStack = stack.AppendField(fieldType, &value, value.Type())
validate := validationRules(fieldType.Tag.Get("validate"))
validateInner := validationRules(fieldType.Tag.Get("validateInner"))
if len(validate) > 0 || len(validateInner) > 0 {
v.Validate(innerStack, &field, fieldType.Type, validate)
}
for field.Kind() == reflect.Ptr {
if field.IsNil() {
v.Nil(innerStack, field.Type().Elem())
continue outer
}
field = field.Elem()
}
switch field.Kind() {
case reflect.Struct:
v.Struct(innerStack, field)
case reflect.Slice, reflect.Array:
v.List(innerStack, field, validateInner)
}
}
}
func (v *Validator) NilStruct(stack Stack, valueType reflect.Type) {
if len(stack) > 100 {
return
}
for idx := 0; idx < valueType.NumField(); idx++ {
fieldType := valueType.Field(idx)
innerStack := stack.AppendField(fieldType, nil, valueType)
validate := validationRules(fieldType.Tag.Get("validate"))
validateInner := validationRules(fieldType.Tag.Get("validateInner"))
if len(validate) == 0 && len(validateInner) == 0 {
continue
}
v.Validate(innerStack, nil, fieldType.Type, validate)
for fieldType.Type.Kind() == reflect.Ptr {
fieldType.Type = fieldType.Type.Elem()
}
switch fieldType.Type.Kind() {
case reflect.Struct:
v.NilStruct(innerStack, fieldType.Type)
}
}
}
func (v *Validator) Validate(stack Stack, value *reflect.Value, valueType reflect.Type, rules []validationRule) {
if len(rules) == 0 {
return
}
errors := []FieldValidatorError{}
state := &ValidatorCtxState{
bail: false,
state: map[string]any{},
stack: stack,
validator: v,
}
for _, rule := range rules {
ctx := &ValidatorCtx{
ctx: v.ctx,
Args: rule.args,
state: state,
Needle: Needle{
Value: value,
Type: valueType,
},
}
hint, ok := rule.validator.Fn(ctx)
if ok {
continue
}
errors = append(errors, FieldValidatorError{
Rule: rule.name,
Hint: hint,
Message: v.ErrorMessage(rule.name, rule.validator.Messages, hint, ctx),
})
if state.bail {
break
}
}
if len(errors) == 0 {
return
}
goPath, jsonPath, formPath := stack.ToPaths()
path := goPath
if v.mode == JsonMode {
path = jsonPath
} else if v.mode == FormMode {
path = formPath
}
v.errors = append(v.errors, FieldErrors{
Path: path,
Errors: errors,
})
}
func (v *Validator) ErrorMessage(ruleName string, resolvers map[string]MessageResolver, hint string, ctx *ValidatorCtx) string {
template := v.ErrorMessageTemplate(ruleName, resolvers, hint, ctx.state.stack)
replaceVariable := func(location templateVariableT, a string) {
template = template[:location.from] + a + template[location.to:]
}
variables := parseMsgTemplate([]byte(template))
outer:
for idx := len(variables) - 1; idx >= 0; idx-- {
variable := variables[idx]
variableName := template[variable.from:variable.to]
switch variableName[1:] {
case "attribute":
stack := ctx.state.stack
if len(stack) == 0 {
replaceVariable(variable, "")
continue outer
}
stackElement := stack[len(stack)-1]
if v.mode == JsonMode {
replaceVariable(variable, stackElement.JsonName)
} else if v.mode == FormMode {
replaceVariable(variable, stackElement.FormName)
} else {
replaceVariable(variable, stackElement.GoName)
}
continue outer
case "other":
if ctx.lastObtainedField == nil || !ctx.lastObtainedField.HasValue() {
replaceVariable(variable, "")
continue outer
}
if v.mode == JsonMode {
jsonValue, err := json.Marshal(ctx.lastObtainedField.Value.Interface())
if err == nil {
replaceVariable(variable, string(jsonValue))
continue outer
}
}
replaceVariable(variable, fmt.Sprintf("%+v", ctx.lastObtainedField.Value.Interface()))
continue outer
case "value":
if !ctx.HasValue() {
replaceVariable(variable, "")
continue outer
}
if v.mode == JsonMode {
jsonValue, err := json.Marshal(ctx.Value.Interface())
if err == nil {
replaceVariable(variable, string(jsonValue))
continue outer
}
}
replaceVariable(variable, fmt.Sprintf("%+v", ctx.Value.Interface()))
continue outer
case "date":
t, ok := ctx.DateFromArgs(0)
if !ok {
replaceVariable(variable, "")
continue outer
}
replaceVariable(variable, t.Format(time.DateTime))
continue outer
}
if strings.HasPrefix(variableName[1:], "arg") {
if variableName == ":args" {
replaceVariable(variable, strings.Join(ctx.Args, ", "))
} else if variableName == ":arg" {
if len(ctx.Args) == 0 {
replaceVariable(variable, "")
continue
}
replaceVariable(variable, ctx.Args[0])
} else {
suffix := variableName[4:]
idx, err := strconv.Atoi(suffix)
if err != nil && idx < 0 {
continue
}
if idx >= len(ctx.Args) {
replaceVariable(variable, "")
continue
}
replaceVariable(variable, ctx.Args[idx])
}
}
}
return template
}
func (v *Validator) ErrorMessageTemplate(ruleName string, resolvers map[string]MessageResolver, hint string, stack Stack) string {
customResolver := v.CustomValidationRule(ruleName, stack)
if customResolver != nil {
return customResolver.Resolve(hint)
}
for _, lang := range v.languages {
langResolver, ok := resolvers[lang]
if !ok {
continue
}
msg := langResolver.Resolve(hint)
if msg == "" {
break
}
return msg
}
return FallbackMessageResolver.Resolve(hint)
}
// field tries to return a value from the input based on the requested path
// There are 2 main ways of using this function
//
// 1. Absolute path:
// - "foo.1.bar" = Get from the input (struct) the field "foo", then when it's a list like get the element at index 1 from the list, then get the field "bar" from the struct
// - "" = Get the source input
//
// 2. Relative path:
// - ".foo" = Get relative to the currently processed struct the field "foo"
// - ".1" = Get relative to the currently processed list the element at index 1
// - "." = Get the currently processed struct
// - "..foo" = Get the parent of the currentl`y processed struct and then get the field "foo" from it
//
// If nil is returned the field does not exist or path is invalid
// If a needle with only a reflect.Type is returned the path exists but the value is nil
func (v *Validator) field(stack Stack, path string) *Needle {
relativity := 0
endRelative := false
pathParts := []string{}
for _, part := range strings.Split(path, ".") {
part = strings.TrimSpace(part)
if part == "" {
if endRelative {
return nil
} else {
relativity++
}
continue
} else {
endRelative = true
}
pathParts = append(pathParts, part)
}
if relativity == 0 || len(stack) == 0 || relativity > len(stack) {
// Absolute path
return resolveWithValue(v.inputValue, pathParts)
}
// Relative to the currently processed struct
stackElement := stack[len(stack)-relativity]
if stackElement.Parent == nil {
return resolveWithType(stackElement.ParentType, pathParts)
}
return resolveWithValue(*stackElement.Parent, pathParts)
}