forked from Insei/valigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
67 lines (59 loc) · 2.48 KB
/
storage.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
package valigo
import (
"context"
"reflect"
"github.com/insei/fmap/v3"
"github.com/insei/valigo/shared"
)
// structValidationFn represents a function that validates a struct.
// It takes a context, a helper, and an object as input, and returns a slice of errors.
type structValidationFn func(ctx context.Context, h shared.Helper, obj any) []shared.Error
// storage represents the storage for struct validators.
type storage struct {
// Validators is a map that stores validators for each struct type.
// The key is the reflect.Type of the struct, and the value is a slice of structValidationFn.
validators map[reflect.Type][]structValidationFn
}
// newOnStructAppend adds a new struct validator to the storage.
// It takes a temporary object, an enabler function, and a field validation function as input.
// The enabler function is optional and can be used to conditionally enable the validation.
// The field validation function is called for each field of the struct.
func (s *storage) newOnStructAppend(temp any, enabler func(context.Context, any) bool, fn shared.FieldValidationFn) {
t := reflect.TypeOf(temp)
fnNew := func(ctx context.Context, h shared.Helper, obj any) []shared.Error {
if enabler != nil && !enabler(ctx, obj) {
return nil
}
return fn(ctx, h, obj)
}
s.validators[t] = append(s.validators[t], fnNew)
}
// newOnFieldAppend adds a new field validator to the storage.
// It takes a temporary object, an enabler function, and a field validation function as input.
// The enabler function is optional and can be used to conditionally enable the validation.
// The field validation function is called for each field of the struct.
func (s *storage) newOnFieldAppend(temp any, enabler func(context.Context, any) bool) func(field fmap.Field, fn shared.FieldValidationFn) {
t := reflect.TypeOf(temp)
return func(field fmap.Field, fn shared.FieldValidationFn) {
fnNew := func(ctx context.Context, h shared.Helper, obj, v any) []shared.Error {
if enabler != nil && !enabler(ctx, obj) {
return nil
}
return fn(ctx, h, v)
}
_, ok := s.validators[t]
if !ok {
s.validators[t] = make([]structValidationFn, 0)
}
structValidatorFn := func(ctx context.Context, h shared.Helper, obj any) []shared.Error {
return fnNew(ctx, h, obj, field.GetPtr(obj))
}
s.validators[t] = append(s.validators[t], structValidatorFn)
}
}
// newStorage creates a new storage object.
func newStorage() *storage {
return &storage{
validators: make(map[reflect.Type][]structValidationFn),
}
}