-
Notifications
You must be signed in to change notification settings - Fork 1
/
arguments.go
344 lines (282 loc) · 14.7 KB
/
arguments.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package validation
import (
"context"
"time"
)
// Argument used to set up the validation process. It is used to set up the current validation context and to pass
// arguments for validation values.
type Argument interface {
setUp(ctx *executionContext)
}
// Nil argument is used to validate nil values of any nillable types.
func Nil(isNil bool, constraints ...NilConstraint) ValidatorArgument {
return NewArgument(validateNil(isNil, constraints))
}
// NilProperty argument is an alias for [Nil] that automatically adds property name to the current validation context.
func NilProperty(name string, isNil bool, constraints ...NilConstraint) ValidatorArgument {
return NewArgument(validateNil(isNil, constraints)).At(PropertyName(name))
}
// Bool argument is used to validate boolean values.
func Bool(value bool, constraints ...BoolConstraint) ValidatorArgument {
return NewArgument(validateBool(&value, constraints))
}
// BoolProperty argument is an alias for [Bool] that automatically adds property name to the current validation context.
func BoolProperty(name string, value bool, constraints ...BoolConstraint) ValidatorArgument {
return NewArgument(validateBool(&value, constraints)).At(PropertyName(name))
}
// NilBool argument is used to validate nillable boolean values.
func NilBool(value *bool, constraints ...BoolConstraint) ValidatorArgument {
return NewArgument(validateBool(value, constraints))
}
// NilBoolProperty argument is an alias for [NilBool] that automatically adds property name to the current validation context.
func NilBoolProperty(name string, value *bool, constraints ...BoolConstraint) ValidatorArgument {
return NewArgument(validateBool(value, constraints)).At(PropertyName(name))
}
// Number argument is used to validate numbers.
func Number[T Numeric](value T, constraints ...NumberConstraint[T]) ValidatorArgument {
return NewArgument(validateNumber(&value, constraints))
}
// NumberProperty argument is an alias for [Number] that automatically adds property name to the current validation context.
func NumberProperty[T Numeric](name string, value T, constraints ...NumberConstraint[T]) ValidatorArgument {
return NewArgument(validateNumber(&value, constraints)).At(PropertyName(name))
}
// NilNumber argument is used to validate nillable numbers.
func NilNumber[T Numeric](value *T, constraints ...NumberConstraint[T]) ValidatorArgument {
return NewArgument(validateNumber(value, constraints))
}
// NilNumberProperty argument is an alias for [NilNumber] that automatically adds property name to the current validation context.
func NilNumberProperty[T Numeric](name string, value *T, constraints ...NumberConstraint[T]) ValidatorArgument {
return NewArgument(validateNumber(value, constraints)).At(PropertyName(name))
}
// String argument is used to validate strings.
func String(value string, constraints ...StringConstraint) ValidatorArgument {
return NewArgument(validateString(&value, constraints))
}
// StringProperty argument is an alias for [String] that automatically adds property name to the current validation context.
func StringProperty(name string, value string, constraints ...StringConstraint) ValidatorArgument {
return NewArgument(validateString(&value, constraints)).At(PropertyName(name))
}
// NilString argument is used to validate nillable strings.
func NilString(value *string, constraints ...StringConstraint) ValidatorArgument {
return NewArgument(validateString(value, constraints))
}
// NilStringProperty argument is an alias for [NilString] that automatically adds property name to the current validation context.
func NilStringProperty(name string, value *string, constraints ...StringConstraint) ValidatorArgument {
return NewArgument(validateString(value, constraints)).At(PropertyName(name))
}
// Countable argument can be used to validate size of an array, slice, or map. You can pass result of len()
// function as an argument.
func Countable(count int, constraints ...CountableConstraint) ValidatorArgument {
return NewArgument(validateCountable(count, constraints))
}
// CountableProperty argument is an alias for [Countable] that automatically adds property name to the current validation context.
func CountableProperty(name string, count int, constraints ...CountableConstraint) ValidatorArgument {
return NewArgument(validateCountable(count, constraints)).At(PropertyName(name))
}
// Time argument is used to validate [time.Time] value.
func Time(value time.Time, constraints ...TimeConstraint) ValidatorArgument {
return NewArgument(validateTime(&value, constraints))
}
// TimeProperty argument is an alias for [Time] that automatically adds property name to the current validation context.
func TimeProperty(name string, value time.Time, constraints ...TimeConstraint) ValidatorArgument {
return NewArgument(validateTime(&value, constraints)).At(PropertyName(name))
}
// NilTime argument is used to validate nillable [time.Time] value.
func NilTime(value *time.Time, constraints ...TimeConstraint) ValidatorArgument {
return NewArgument(validateTime(value, constraints))
}
// NilTimeProperty argument is an alias for [NilTime] that automatically adds property name to the current validation context.
func NilTimeProperty(name string, value *time.Time, constraints ...TimeConstraint) ValidatorArgument {
return NewArgument(validateTime(value, constraints)).At(PropertyName(name))
}
// Valid is used to run validation on the [Validatable] type. This method is recommended
// to build a complex validation process.
func Valid(value Validatable) ValidatorArgument {
return NewArgument(validateIt(value))
}
// ValidProperty argument is an alias for [Valid] that automatically adds property name to the current validation context.
func ValidProperty(name string, value Validatable) ValidatorArgument {
return NewArgument(validateIt(value)).At(PropertyName(name))
}
// ValidSlice is a generic argument used to run validation on the slice of [Validatable] types.
// This method is recommended to build a complex validation process.
func ValidSlice[T Validatable](values []T) ValidatorArgument {
return NewArgument(validateSlice(values))
}
// ValidSliceProperty argument is an alias for [ValidSlice] that automatically adds property name to the current validation context.
func ValidSliceProperty[T Validatable](name string, values []T) ValidatorArgument {
return NewArgument(validateSlice(values)).At(PropertyName(name))
}
// ValidMap is a generic argument used to run validation on the map of [Validatable] types.
// This method is recommended to build a complex validation process.
func ValidMap[T Validatable](values map[string]T) ValidatorArgument {
return NewArgument(validateMap(values))
}
// ValidMapProperty argument is an alias for [ValidSlice] that automatically adds property name to the current validation context.
func ValidMapProperty[T Validatable](name string, values map[string]T) ValidatorArgument {
return NewArgument(validateMap(values)).At(PropertyName(name))
}
// Comparable argument is used to validate generic comparable value.
func Comparable[T comparable](value T, constraints ...ComparableConstraint[T]) ValidatorArgument {
return NewArgument(validateComparable(&value, constraints))
}
// ComparableProperty argument is an alias for [Comparable] that automatically adds property name to the current validation context.
func ComparableProperty[T comparable](name string, value T, constraints ...ComparableConstraint[T]) ValidatorArgument {
return NewArgument(validateComparable(&value, constraints)).At(PropertyName(name))
}
// NilComparable argument is used to validate nillable generic comparable value.
func NilComparable[T comparable](value *T, constraints ...ComparableConstraint[T]) ValidatorArgument {
return NewArgument(validateComparable(value, constraints))
}
// NilComparableProperty argument is an alias for [NilComparable] that automatically adds property name to the current validation context.
func NilComparableProperty[T comparable](name string, value *T, constraints ...ComparableConstraint[T]) ValidatorArgument {
return NewArgument(validateComparable(value, constraints)).At(PropertyName(name))
}
// Comparables argument is used to validate generic comparable types.
func Comparables[T comparable](values []T, constraints ...ComparablesConstraint[T]) ValidatorArgument {
return NewArgument(validateComparables(values, constraints))
}
// ComparablesProperty argument is an alias for [Comparables] that automatically adds property name to the current validation context.
func ComparablesProperty[T comparable](name string, values []T, constraints ...ComparablesConstraint[T]) ValidatorArgument {
return NewArgument(validateComparables(values, constraints)).At(PropertyName(name))
}
// EachString is used to validate a slice of strings.
func EachString(values []string, constraints ...StringConstraint) ValidatorArgument {
return NewArgument(validateEachString(values, constraints))
}
// EachStringProperty argument is an alias for [EachString] that automatically adds property name to the current validation context.
func EachStringProperty(name string, values []string, constraints ...StringConstraint) ValidatorArgument {
return NewArgument(validateEachString(values, constraints)).At(PropertyName(name))
}
// EachNumber is used to validate a slice of numbers.
func EachNumber[T Numeric](values []T, constraints ...NumberConstraint[T]) ValidatorArgument {
return NewArgument(validateEachNumber(values, constraints))
}
// EachNumberProperty argument is an alias for [EachNumber] that automatically adds property name to the current validation context.
func EachNumberProperty[T Numeric](name string, values []T, constraints ...NumberConstraint[T]) ValidatorArgument {
return NewArgument(validateEachNumber(values, constraints)).At(PropertyName(name))
}
// EachComparable is used to validate a slice of generic comparables.
func EachComparable[T comparable](values []T, constraints ...ComparableConstraint[T]) ValidatorArgument {
return NewArgument(validateEachComparable(values, constraints))
}
// EachComparableProperty argument is an alias for [EachComparable] that automatically adds property name to the current validation context.
func EachComparableProperty[T comparable](name string, values []T, constraints ...ComparableConstraint[T]) ValidatorArgument {
return NewArgument(validateEachComparable(values, constraints)).At(PropertyName(name))
}
// CheckNoViolations is a special argument that checks err for violations. If err contains [Violation] or [ViolationList]
// then these violations will be appended into returned violation list from the validator. If err contains an error
// that does not implement an error interface, then the validation process will be terminated and
// this error will be returned.
func CheckNoViolations(err error) ValidatorArgument {
return NewArgument(func(ctx context.Context, validator *Validator) (*ViolationList, error) {
return unwrapViolationList(err)
})
}
// Check argument can be useful for quickly checking the result of some simple expression
// that returns a boolean value.
func Check(isValid bool) Checker {
return Checker{
isValid: isValid,
err: ErrNotValid,
messageTemplate: ErrNotValid.Message(),
}
}
// CheckProperty argument is an alias for [Check] that automatically adds property name to the current validation context.
// It is useful to apply a simple checks on structs.
func CheckProperty(name string, isValid bool) Checker {
return Check(isValid).At(PropertyName(name))
}
type ValidateFunc func(ctx context.Context, validator *Validator) (*ViolationList, error)
// NewArgument can be used to implement validation functional arguments for the specific types.
func NewArgument(validate ValidateFunc) ValidatorArgument {
return ValidatorArgument{validate: validate}
}
// This creates a generic validation argument that can help implement the validation
// argument for client-side types.
func This[T any](v T, constraints ...Constraint[T]) ValidatorArgument {
return NewArgument(func(ctx context.Context, validator *Validator) (*ViolationList, error) {
violations := NewViolationList()
for _, constraint := range constraints {
err := violations.AppendFromError(constraint.Validate(ctx, validator, v))
if err != nil {
return nil, err
}
}
return violations, nil
})
}
// ValidatorArgument is common implementation of [Argument] that is used to run validation
// process on given argument.
type ValidatorArgument struct {
isIgnored bool
validate ValidateFunc
path []PropertyPathElement
}
// At returns a copy of [ValidatorArgument] with appended property path suffix.
func (arg ValidatorArgument) At(path ...PropertyPathElement) ValidatorArgument {
arg.path = append(arg.path, path...)
return arg
}
// When enables conditional validation of this argument. If the expression evaluates to false,
// then the argument will be ignored.
func (arg ValidatorArgument) When(condition bool) ValidatorArgument {
arg.isIgnored = !condition
return arg
}
func (arg ValidatorArgument) setUp(ctx *executionContext) {
if !arg.isIgnored {
ctx.addValidation(arg.validate, arg.path...)
}
}
// Checker is an argument that can be useful for quickly checking the result of
// some simple expression that returns a boolean value.
type Checker struct {
isIgnored bool
isValid bool
path []PropertyPathElement
groups []string
err error
messageTemplate string
messageParameters TemplateParameterList
}
// At returns a copy of [Checker] with appended property path suffix.
func (c Checker) At(path ...PropertyPathElement) Checker {
c.path = append(c.path, path...)
return c
}
// When enables conditional validation of this constraint. If the expression evaluates to false,
// then the constraint will be ignored.
func (c Checker) When(condition bool) Checker {
c.isIgnored = !condition
return c
}
// WhenGroups enables conditional validation of the constraint by using the validation groups.
func (c Checker) WhenGroups(groups ...string) Checker {
c.groups = groups
return c
}
// WithError overrides default code for produced violation.
func (c Checker) WithError(err error) Checker {
c.err = err
return c
}
// WithMessage sets the violation message template. You can set custom template parameters
// for injecting its values into the final message.
func (c Checker) WithMessage(template string, parameters ...TemplateParameter) Checker {
c.messageTemplate = template
c.messageParameters = parameters
return c
}
func (c Checker) setUp(arguments *executionContext) {
arguments.addValidation(c.validate, c.path...)
}
func (c Checker) validate(ctx context.Context, validator *Validator) (*ViolationList, error) {
if c.isValid || c.isIgnored || validator.IsIgnoredForGroups(c.groups...) {
return nil, nil
}
violation := validator.BuildViolation(ctx, c.err, c.messageTemplate).
WithParameters(c.messageParameters...).
Create()
return NewViolationList(violation), nil
}