-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator_types.go
326 lines (279 loc) · 8.62 KB
/
validator_types.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package validation
import (
"context"
"fmt"
)
// Validator interface represents a validator object
type Validator interface {
Validate(context.Context) Errors
OnError(...ErrorMod) Validator
}
// baseValidator base validator
type baseValidator struct {
errMods []ErrorMod
}
// applyErrMods applies mods on the target error
func (b *baseValidator) applyErrMods(err Error) {
for _, mod := range b.errMods {
mod(err)
}
}
// applyErrModsWithGrouping applies mods on the target error.
// If the input has more than 1 error, this creates a `group` error and applies mods on it.
func (b *baseValidator) applyErrModsWithGrouping(errs Errors) Errors {
if len(b.errMods) == 0 || len(errs) == 0 {
return errs
}
if len(errs) > 1 {
errs = []Error{errorBuild("group", "", nil, errs)}
}
b.applyErrMods(errs[0])
return errs
}
// SingleValidator interface represents a validator that performs a single validation
type SingleValidator interface {
Validator
}
// NewSingleValidator creates a new SingleValidator
func NewSingleValidator(execFn func(ctx context.Context) Error) SingleValidator {
return &singleValidator{
execFn: execFn,
}
}
// singleValidator implementation of SingleValidator interface
type singleValidator struct {
baseValidator
execFn func(ctx context.Context) Error
}
// OnError implementation of Validator interface
func (v *singleValidator) OnError(mods ...ErrorMod) Validator {
v.errMods = mods
return v
}
// Validate executes the validator
func (v *singleValidator) Validate(ctx context.Context) Errors {
err := v.execFn(ctx)
if err == nil {
return nil
}
v.applyErrMods(err)
return []Error{err}
}
// CondValidator interface represents a validator that performs multiple validations based on
// specified conditions.
type CondValidator interface {
Validator
ValidateWithCond(context.Context) (bool, Errors)
}
// SingleCondValidator validator that accepts only one condition
type SingleCondValidator interface {
CondValidator
Then(validators ...Validator) SingleCondValidator
Else(validators ...Validator) SingleCondValidator
}
// singleCondValidator implementation of SingleCondValidator
type singleCondValidator struct {
baseValidator
conditions []any
thenValidators []Validator
elseValidators []Validator
}
// NewSingleCondValidator creates a new SingleCondValidator
func NewSingleCondValidator(conditions ...any) SingleCondValidator {
return &singleCondValidator{conditions: conditions}
}
func (c *singleCondValidator) Then(validators ...Validator) SingleCondValidator {
c.thenValidators = validators
return c
}
func (c *singleCondValidator) Else(validators ...Validator) SingleCondValidator {
c.elseValidators = validators
return c
}
func (c *singleCondValidator) OnError(errMods ...ErrorMod) Validator {
c.errMods = errMods
return c
}
func (c *singleCondValidator) Validate(ctx context.Context) Errors {
_, errs := c.ValidateWithCond(ctx)
return c.applyErrModsWithGrouping(errs)
}
func (c *singleCondValidator) ValidateWithCond(ctx context.Context) (bool, Errors) {
validators := c.thenValidators
match := c.match(ctx)
if !match {
validators = c.elseValidators
}
if len(validators) == 0 {
return match, nil
}
return match, execValidators(ctx, validators, false)
}
func (c *singleCondValidator) match(ctx context.Context) bool {
if len(c.conditions) == 0 {
return false
}
for _, cond := range c.conditions {
boolVal, ok := cond.(bool)
if ok {
if !boolVal {
return false
}
continue
}
validator, ok := cond.(Validator)
if ok {
errs := validator.Validate(ctx)
if len(errs) > 0 {
return false
}
continue
}
panic(fmt.Errorf("%w: only 'bool' or 'validator' allowed", ErrTypeUnsupported))
}
return true
}
// MultiCondValidator validator that accepts multiple conditions
type MultiCondValidator interface {
CondValidator
Default(validators ...Validator) MultiCondValidator
}
// multiCondValidator implementation of MultiCondValidator
type multiCondValidator struct {
baseValidator
conditions []SingleCondValidator
defaultValidators []Validator
}
// NewMultiCondValidator creates a new MultiCondValidator
func NewMultiCondValidator(conditions ...SingleCondValidator) MultiCondValidator {
return &multiCondValidator{conditions: conditions}
}
func (c *multiCondValidator) Default(validators ...Validator) MultiCondValidator {
c.defaultValidators = validators
return c
}
func (c *multiCondValidator) OnError(mods ...ErrorMod) Validator {
c.errMods = mods
return c
}
func (c *multiCondValidator) Validate(ctx context.Context) Errors {
_, errs := c.ValidateWithCond(ctx)
return c.applyErrModsWithGrouping(errs)
}
func (c *multiCondValidator) ValidateWithCond(ctx context.Context) (bool, Errors) {
for _, v := range c.conditions {
match, errs := v.ValidateWithCond(ctx)
if match {
return true, errs
}
}
// No match condition, executes the default ones
return false, execValidators(ctx, c.defaultValidators, false)
}
// ItemValidator validator to collect input validators via its Validate() func
type ItemValidator interface {
Validate(validators ...Validator)
Group(validators ...Validator) SingleValidator
OneOf(validators ...Validator) SingleValidator
ExactOneOf(validators ...Validator) SingleValidator
NotOf(validators ...Validator) SingleValidator
}
// itemValidator implements ItemValidator interface
type itemValidator struct {
ItemValidator
validators []Validator
}
func (iv *itemValidator) Validate(validators ...Validator) {
iv.validators = append(iv.validators, validators...)
}
func (iv *itemValidator) Group(validators ...Validator) SingleValidator {
group := Group(validators...)
iv.validators = append(iv.validators, group)
return group
}
func (iv *itemValidator) OneOf(validators ...Validator) SingleValidator {
oneOf := OneOf(validators...)
iv.validators = append(iv.validators, oneOf)
return oneOf
}
func (iv *itemValidator) ExactOneOf(validators ...Validator) SingleValidator {
exactOneOf := ExactOneOf(validators...)
iv.validators = append(iv.validators, exactOneOf)
return exactOneOf
}
func (iv *itemValidator) NotOf(validators ...Validator) SingleValidator {
notOf := NotOf(validators...)
iv.validators = append(iv.validators, notOf)
return notOf
}
func (iv *itemValidator) get() []Validator {
return iv.validators
}
// SliceContentValidator validator that validates slice elements
type SliceContentValidator[T any, S ~[]T] interface {
Validator
ForEach(fn func(element T, index int, elemValidator ItemValidator)) SliceContentValidator[T, S]
}
// sliceContentValidator implementation of SliceContentValidator
type sliceContentValidator[T any, S ~[]T] struct {
baseValidator
slice S
elemValidatorFunc func(T, int, ItemValidator)
}
// NewSliceContentValidator creates a new SliceContentValidator
func NewSliceContentValidator[T any, S ~[]T](slice S) SliceContentValidator[T, S] {
return &sliceContentValidator[T, S]{slice: slice}
}
func (c *sliceContentValidator[T, S]) ForEach(fn func(T, int, ItemValidator)) SliceContentValidator[T, S] {
c.elemValidatorFunc = fn
return c
}
func (c *sliceContentValidator[T, S]) OnError(errMods ...ErrorMod) Validator {
c.errMods = errMods
return c
}
func (c *sliceContentValidator[T, S]) Validate(ctx context.Context) Errors {
if len(c.slice) == 0 {
return nil
}
elemValidator := &itemValidator{}
for i, elem := range c.slice {
c.elemValidatorFunc(elem, i, elemValidator)
}
errs := execValidators(ctx, elemValidator.get(), false)
return c.applyErrModsWithGrouping(errs)
}
// MapContentValidator validator that validates map entries
type MapContentValidator[K comparable, V any, M ~map[K]V] interface {
Validator
ForEach(fn func(k K, v V, entryValidator ItemValidator)) MapContentValidator[K, V, M]
}
// mapContentValidator implementation of MapContentValidator
type mapContentValidator[K comparable, V any, M ~map[K]V] struct {
baseValidator
mapObj M
entryValidatorFunc func(K, V, ItemValidator)
}
// NewMapContentValidator creates a new MapContentValidator
func NewMapContentValidator[K comparable, V any, M ~map[K]V](mapObj M) MapContentValidator[K, V, M] {
return &mapContentValidator[K, V, M]{mapObj: mapObj}
}
func (m *mapContentValidator[K, V, M]) ForEach(fn func(K, V, ItemValidator)) MapContentValidator[K, V, M] {
m.entryValidatorFunc = fn
return m
}
func (m *mapContentValidator[K, V, M]) OnError(errMods ...ErrorMod) Validator {
m.errMods = errMods
return m
}
func (m *mapContentValidator[K, V, M]) Validate(ctx context.Context) Errors {
if len(m.mapObj) == 0 {
return nil
}
entryValidator := &itemValidator{}
for k, v := range m.mapObj {
m.entryValidatorFunc(k, v, entryValidator)
}
errs := execValidators(ctx, entryValidator.get(), false)
return m.applyErrModsWithGrouping(errs)
}