-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
56 lines (40 loc) · 1.29 KB
/
helpers.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
package validator
import (
"context"
"reflect"
ve "github.com/donatorsky/go-validator/error"
vr "github.com/donatorsky/go-validator/rule"
)
type RulesMap map[string][]vr.Rule
func applyRules(ctx context.Context, data any, rules []vr.Rule, fieldValue fieldValue, errorsBag ve.ErrorsBag, options *validatorOptions) error {
anyRuleFailed := false
i := newRecursiveIterator(rules, ctx, fieldValue.value, data)
value := fieldValue.value
for i.Valid() {
rule := i.Current()
var err ve.ValidationError
if value, err = rule.Apply(ctx, value, data); err != nil {
errorsBag.Add(fieldValue.field, err)
anyRuleFailed = true
}
if bailingRule, ok := rule.(vr.BailingRule); ok && anyRuleFailed && bailingRule.Bails() {
break
}
i.Next(ctx, value, data)
}
if !anyRuleFailed && options.dataCollector != nil {
options.dataCollector.Set(fieldValue.field, value)
}
if !anyRuleFailed && options.valueExporter != nil {
targetValue := options.valueExporter.Elem()
targetType := targetValue.Type()
if valueType := reflect.TypeOf(value); !valueType.ConvertibleTo(targetType) {
return ve.ValueExporterTypeMismatchError{
ValueType: valueType.String(),
TargetType: targetType.String(),
}
}
targetValue.Set(reflect.ValueOf(value).Convert(targetType))
}
return nil
}