-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_map.go
43 lines (32 loc) · 1.04 KB
/
for_map.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
package validator
import (
"context"
ve "github.com/donatorsky/go-validator/error"
)
type forMapValidatorOption func(options *validatorOptions) error
func ForMap(data map[string]any, rules RulesMap, options ...forMapValidatorOption) (ve.ErrorsBag, error) {
return ForMapWithContext(context.Background(), data, rules, options...)
}
func ForMapWithContext(ctx context.Context, data map[string]any, rules RulesMap, options ...forMapValidatorOption) (ve.ErrorsBag, error) {
opts := &validatorOptions{}
for _, option := range options {
if err := option(opts); err != nil {
return nil, err
}
}
errorsBag := ve.NewErrorsBag()
for field, rules := range rules {
for fieldValue := range newFieldsIterator(field, data) {
if err := applyRules(ctx, data, rules, fieldValue, errorsBag, opts); err != nil {
return nil, err
}
}
}
return errorsBag, nil
}
func ForMapWithDataCollector(collector DataCollector) forMapValidatorOption {
return func(options *validatorOptions) error {
options.dataCollector = collector
return nil
}
}