-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.go
52 lines (44 loc) · 1.35 KB
/
options.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 valigo
import (
"github.com/insei/fmap/v3"
"github.com/insei/valigo/shared"
"github.com/insei/valigo/translator"
)
// Option is an interface that defines a single method, apply,
// which is used to apply an option to a Validator.
type Option interface {
apply(v *Validator)
}
// optionFunc is a type that implements the Option interface.
// It is a function that takes a Validator as an argument.
type optionFunc func(*Validator)
// apply implements the Option interface for optionFunc.
// It calls the underlying function with the given Validator.
func (f optionFunc) apply(v *Validator) {
f(v)
}
// WithTranslator returns an Option that sets the translator for the Validator.
func WithTranslator(t translator.Translator) Option {
return optionFunc(func(v *Validator) {
if t != nil {
v.helper.t = t
}
})
}
// WithFieldLocationNamingFn returns an Option that sets the field location
// naming function for the Validator.
func WithFieldLocationNamingFn(fn func(field fmap.Field) string) Option {
return optionFunc(func(v *Validator) {
if fn != nil {
v.helper.getFieldLocation = fn
}
})
}
// WithErrorsTransformer returns an Option that sets the transformer for the Validator.
func WithErrorsTransformer(fn func(errs []shared.Error) []error) Option {
return optionFunc(func(v *Validator) {
if fn != nil {
v.transformError = fn
}
})
}