-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilder.go
161 lines (150 loc) · 4.98 KB
/
builder.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package valigo
import (
"context"
"github.com/insei/fmap/v3"
"github.com/insei/valigo/num"
"github.com/insei/valigo/shared"
"github.com/insei/valigo/str"
"github.com/insei/valigo/uuid"
)
// builder represents a builder for validators.
// It is generic and can be used to build validators for any type.
type builder[T any] struct {
*str.StringBundle
*num.NumberBundle
*uuid.UUIDBundle
obj any
v *Validator
enablerFn func(ctx context.Context, obj any) bool
}
// When adds a condition to the builder.
// It takes a function that returns a boolean and returns a builder.
// The function is called with the context and the object being validated.
// If the function returns true, the validation is enabled.
func (b *builder[T]) When(fn func(ctx context.Context, obj *T) bool) Configurator[T] {
return configure[T](b.v, b.obj, func(ctx context.Context, obj any) bool {
enablerFn := fn
if b.enablerFn != nil {
enablerFn = func(ctx context.Context, obj *T) bool {
if b.enablerFn(ctx, obj) && fn(ctx, obj) {
return true
}
return false
}
}
return enablerFn(ctx, obj.(*T))
})
}
// errorTFn represents a function that returns an error.
// It takes a context, a pointer to a field, a field value, a locale key, and optional arguments as input,
// and returns a shared.Error.
type errorTFn func(ctx context.Context, ptrToField, fieldValue any, localeKey string, args ...any) shared.Error
// ErrorT calls the errorTFn function and returns a shared.Error.
// It takes a context, a pointer to a field, a field value, a locale key, and optional arguments as input,
// and returns a shared.Error.
func (e errorTFn) ErrorT(ctx context.Context, ptrToFieldValue, fieldValue any, localeKey string, args ...any) shared.Error {
return e(ctx, ptrToFieldValue, fieldValue, localeKey, args...)
}
// Custom adds a custom validation function to the builder.
// It takes a function that takes a context, a helper, and an object as input,
// and returns a slice of shared.Error.
// The helper is used to create errors.
func (b *builder[T]) Custom(fn func(ctx context.Context, h shared.StructCustomHelper, obj *T) []shared.Error) {
newHFn := func(obj any, h shared.Helper) errorTFn {
return func(ctx context.Context, ptrToField, fieldValue any, localeKey string, args ...any) shared.Error {
fields, err := fmap.GetFrom(obj)
if err != nil {
panic(err)
}
field, err := fields.GetFieldByPtr(obj, ptrToField)
if err != nil {
panic(err)
}
return h.ErrorT(ctx, field, fieldValue, localeKey, args...)
}
}
fnConvert := func(ctx context.Context, h shared.Helper, objAny any) []shared.Error {
return fn(ctx, newHFn(objAny, h), objAny.(*T))
}
b.v.storage.newOnStructAppend(b.obj, b.enablerFn, fnConvert)
}
func (b *builder[T]) StringSlice(sliceFieldPtr any) *str.StringSliceFieldConfigurator {
fields, err := fmap.GetFrom(b.obj)
if err != nil {
panic(err)
}
field, err := fields.GetFieldByPtr(b.obj, sliceFieldPtr)
if err != nil {
panic(err)
}
appendFn := b.v.storage.newOnFieldAppend(b.obj, b.enablerFn)
return str.NewStringSliceFieldConfigurator(shared.SliceFieldConfiguratorParams{
Field: field,
Helper: b.v.GetHelper(),
AppendFn: func(fn shared.FieldValidationFn) {
appendFn(field, fn)
},
})
}
func (b *builder[T]) UUIDSlice(sliceFieldPtr any) *uuid.UUIDSliceFieldConfigurator {
fields, err := fmap.GetFrom(b.obj)
if err != nil {
panic(err)
}
field, err := fields.GetFieldByPtr(b.obj, sliceFieldPtr)
if err != nil {
panic(err)
}
appendFn := b.v.storage.newOnFieldAppend(b.obj, b.enablerFn)
return uuid.NewUUIDSliceFieldConfigurator(shared.SliceFieldConfiguratorParams{
Field: field,
Helper: b.v.GetHelper(),
AppendFn: func(fn shared.FieldValidationFn) {
appendFn(field, fn)
},
})
}
func (b *builder[T]) Slice(sliceFieldPtr any) *shared.SliceFieldConfigurator {
fields, err := fmap.GetFrom(b.obj)
if err != nil {
panic(err)
}
field, err := fields.GetFieldByPtr(b.obj, sliceFieldPtr)
if err != nil {
panic(err)
}
appendFn := b.v.storage.newOnFieldAppend(b.obj, b.enablerFn)
return shared.NewSliceFieldConfigurator(shared.SliceFieldConfiguratorParams{
Field: field,
Helper: b.v.GetHelper(),
AppendFn: func(fn shared.FieldValidationFn) {
appendFn(field, fn)
},
})
}
// configure creates a new builder with the given validator, object, and enabler function.
// It takes a validator, an object, and an enabler function as input,
// and returns a builder.
func configure[T any](v *Validator, obj any, enabler func(ctx context.Context, obj any) bool) *builder[T] {
fields, err := fmap.GetFrom(obj)
if err != nil {
panic(err)
}
bundleDeps := shared.BundleDependencies{
Object: obj,
Helper: v.GetHelper(),
AppendFn: v.storage.newOnFieldAppend(obj, enabler),
Fields: fields,
}
sb := str.NewStringBundle(bundleDeps)
nb := num.NewNumBundle(bundleDeps)
ub := uuid.NewUUIDBundle(bundleDeps)
return &builder[T]{
StringBundle: sb,
NumberBundle: nb,
UUIDBundle: ub,
obj: obj,
v: v,
enablerFn: enabler,
}
}