-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_value.go
52 lines (41 loc) · 1.12 KB
/
for_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
package validator
import (
"context"
"reflect"
ve "github.com/donatorsky/go-validator/error"
vr "github.com/donatorsky/go-validator/rule"
)
type forValueValidatorOption func(options *validatorOptions) error
func ForValue[In any](value In, rules []vr.Rule, options ...forValueValidatorOption) (errors []ve.ValidationError, _ error) {
return ForValueWithContext[In](context.Background(), value, rules, options...)
}
func ForValueWithContext[In any](ctx context.Context, value In, rules []vr.Rule, options ...forValueValidatorOption) (errors []ve.ValidationError, _ error) {
opts := &validatorOptions{}
for _, option := range options {
if err := option(opts); err != nil {
return nil, err
}
}
errorsBag := ve.NewErrorsBag()
if err := applyRules(
ctx,
value,
rules,
fieldValue{
field: "_",
value: value,
},
errorsBag,
opts,
); err != nil {
return nil, err
}
return errorsBag.Get("_"), nil
}
func ForValueWithValueExporter[Out any](value *Out) forValueValidatorOption {
return func(options *validatorOptions) error {
rv := reflect.ValueOf(value)
options.valueExporter = &rv
return nil
}
}