1
|
|
|
package it |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"time" |
5
|
|
|
|
6
|
|
|
"github.com/muonsoft/validation" |
7
|
|
|
"github.com/muonsoft/validation/code" |
8
|
|
|
"github.com/muonsoft/validation/generic" |
9
|
|
|
"github.com/muonsoft/validation/message" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// NotBlankConstraint checks that a value is not blank: not equal to zero, an empty string, an empty |
13
|
|
|
// slice/array, an empty map, false or nil. Nil behavior is configurable via AllowNil() method. |
14
|
|
|
// To check that a value is not nil only use NotNilConstraint. |
15
|
|
|
type NotBlankConstraint struct { |
16
|
|
|
isIgnored bool |
17
|
|
|
allowNil bool |
18
|
|
|
groups []string |
19
|
|
|
code string |
20
|
|
|
messageTemplate string |
21
|
|
|
messageParameters validation.TemplateParameterList |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// IsNotBlank creates a NotBlankConstraint for checking that value is not empty. |
25
|
|
|
func IsNotBlank() NotBlankConstraint { |
26
|
1 |
|
return NotBlankConstraint{ |
27
|
|
|
code: code.NotBlank, |
28
|
|
|
messageTemplate: message.Templates[code.NotBlank], |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// SetUp always returns no error. |
33
|
|
|
func (c NotBlankConstraint) SetUp() error { |
34
|
1 |
|
return nil |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Name is the constraint name. |
38
|
|
|
func (c NotBlankConstraint) Name() string { |
39
|
|
|
return "NotBlankConstraint" |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// AllowNil makes nil values valid. |
43
|
|
|
func (c NotBlankConstraint) AllowNil() NotBlankConstraint { |
44
|
1 |
|
c.allowNil = true |
45
|
1 |
|
return c |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
49
|
|
|
// then the constraint will be ignored. |
50
|
|
|
func (c NotBlankConstraint) When(condition bool) NotBlankConstraint { |
51
|
1 |
|
c.isIgnored = !condition |
52
|
1 |
|
return c |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// WhenGroups enables conditional validation of the constraint by using the validation groups. |
56
|
|
|
func (c NotBlankConstraint) WhenGroups(groups ...string) NotBlankConstraint { |
57
|
1 |
|
c.groups = groups |
58
|
1 |
|
return c |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Code overrides default code for produced violation. |
62
|
|
|
func (c NotBlankConstraint) Code(code string) NotBlankConstraint { |
63
|
1 |
|
c.code = code |
64
|
1 |
|
return c |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Message sets the violation message template. You can set custom template parameters |
68
|
|
|
// for injecting its values into the final message. |
69
|
|
|
func (c NotBlankConstraint) Message(template string, parameters ...validation.TemplateParameter) NotBlankConstraint { |
70
|
1 |
|
c.messageTemplate = template |
71
|
1 |
|
c.messageParameters = parameters |
72
|
1 |
|
return c |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
func (c NotBlankConstraint) ValidateNil(scope validation.Scope) error { |
76
|
1 |
|
if c.isIgnored || c.allowNil || scope.IsIgnored(c.groups...) { |
77
|
1 |
|
return nil |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return c.newViolation(scope) |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
func (c NotBlankConstraint) ValidateBool(value *bool, scope validation.Scope) error { |
84
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) { |
85
|
1 |
|
return nil |
86
|
|
|
} |
87
|
1 |
|
if c.allowNil && value == nil { |
88
|
1 |
|
return nil |
89
|
|
|
} |
90
|
1 |
|
if value != nil && *value { |
91
|
1 |
|
return nil |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return c.newViolation(scope) |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
func (c NotBlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error { |
98
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) { |
99
|
1 |
|
return nil |
100
|
|
|
} |
101
|
1 |
|
if c.allowNil && value.IsNil() { |
102
|
1 |
|
return nil |
103
|
|
|
} |
104
|
1 |
|
if !value.IsNil() && !value.IsZero() { |
105
|
1 |
|
return nil |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return c.newViolation(scope) |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
func (c NotBlankConstraint) ValidateString(value *string, scope validation.Scope) error { |
112
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) { |
113
|
1 |
|
return nil |
114
|
|
|
} |
115
|
1 |
|
if c.allowNil && value == nil { |
116
|
1 |
|
return nil |
117
|
|
|
} |
118
|
1 |
|
if value != nil && *value != "" { |
119
|
1 |
|
return nil |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
return c.newViolation(scope) |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
func (c NotBlankConstraint) ValidateStrings(values []string, scope validation.Scope) error { |
126
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) { |
127
|
1 |
|
return nil |
128
|
|
|
} |
129
|
1 |
|
if c.allowNil && values == nil { |
130
|
1 |
|
return nil |
131
|
|
|
} |
132
|
1 |
|
if len(values) > 0 { |
133
|
1 |
|
return nil |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
return c.newViolation(scope) |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
func (c NotBlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error { |
140
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) { |
141
|
1 |
|
return nil |
142
|
|
|
} |
143
|
1 |
|
if c.allowNil && value.IsNil() { |
144
|
1 |
|
return nil |
145
|
|
|
} |
146
|
1 |
|
if value.Count() > 0 { |
147
|
1 |
|
return nil |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
return c.newViolation(scope) |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error { |
154
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || count > 0 { |
155
|
1 |
|
return nil |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return c.newViolation(scope) |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error { |
162
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) { |
163
|
1 |
|
return nil |
164
|
|
|
} |
165
|
1 |
|
if c.allowNil && value == nil { |
166
|
1 |
|
return nil |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
if value != nil && !value.IsZero() { |
170
|
1 |
|
return nil |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
return c.newViolation(scope) |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
func (c NotBlankConstraint) newViolation(scope validation.Scope) validation.Violation { |
177
|
1 |
|
return scope.BuildViolation(c.code, c.messageTemplate). |
178
|
|
|
SetParameters(c.messageParameters...). |
179
|
|
|
CreateViolation() |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// BlankConstraint checks that a value is blank: equal to false, nil, zero, an empty string, an empty |
183
|
|
|
// slice, array, or a map. |
184
|
|
|
type BlankConstraint struct { |
185
|
|
|
isIgnored bool |
186
|
|
|
groups []string |
187
|
|
|
code string |
188
|
|
|
messageTemplate string |
189
|
|
|
messageParameters validation.TemplateParameterList |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// IsBlank creates a BlankConstraint for checking that value is empty. |
193
|
|
|
func IsBlank() BlankConstraint { |
194
|
1 |
|
return BlankConstraint{ |
195
|
|
|
code: code.Blank, |
196
|
|
|
messageTemplate: message.Templates[code.Blank], |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// SetUp always returns no error. |
201
|
|
|
func (c BlankConstraint) SetUp() error { |
202
|
1 |
|
return nil |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Name is the constraint name. |
206
|
|
|
func (c BlankConstraint) Name() string { |
207
|
|
|
return "BlankConstraint" |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
211
|
|
|
// then the constraint will be ignored. |
212
|
|
|
func (c BlankConstraint) When(condition bool) BlankConstraint { |
213
|
1 |
|
c.isIgnored = !condition |
214
|
1 |
|
return c |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// WhenGroups enables conditional validation of the constraint by using the validation groups. |
218
|
|
|
func (c BlankConstraint) WhenGroups(groups ...string) BlankConstraint { |
219
|
1 |
|
c.groups = groups |
220
|
1 |
|
return c |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// Code overrides default code for produced violation. |
224
|
|
|
func (c BlankConstraint) Code(code string) BlankConstraint { |
225
|
1 |
|
c.code = code |
226
|
1 |
|
return c |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// Message sets the violation message template. You can set custom template parameters |
230
|
|
|
// for injecting its values into the final message. |
231
|
|
|
func (c BlankConstraint) Message(template string, parameters ...validation.TemplateParameter) BlankConstraint { |
232
|
1 |
|
c.messageTemplate = template |
233
|
1 |
|
c.messageParameters = parameters |
234
|
1 |
|
return c |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
func (c BlankConstraint) ValidateNil(scope validation.Scope) error { |
238
|
1 |
|
return nil |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error { |
242
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || !*value { |
243
|
1 |
|
return nil |
244
|
|
|
} |
245
|
|
|
|
246
|
1 |
|
return c.newViolation(scope) |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error { |
250
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value.IsNil() || value.IsZero() { |
251
|
1 |
|
return nil |
252
|
|
|
} |
253
|
|
|
|
254
|
1 |
|
return c.newViolation(scope) |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error { |
258
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || *value == "" { |
259
|
1 |
|
return nil |
260
|
|
|
} |
261
|
|
|
|
262
|
1 |
|
return c.newViolation(scope) |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
func (c BlankConstraint) ValidateStrings(values []string, scope validation.Scope) error { |
266
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || len(values) == 0 { |
267
|
1 |
|
return nil |
268
|
|
|
} |
269
|
|
|
|
270
|
1 |
|
return c.newViolation(scope) |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error { |
274
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value.Count() == 0 { |
275
|
1 |
|
return nil |
276
|
|
|
} |
277
|
|
|
|
278
|
1 |
|
return c.newViolation(scope) |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error { |
282
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || count == 0 { |
283
|
1 |
|
return nil |
284
|
|
|
} |
285
|
|
|
|
286
|
1 |
|
return c.newViolation(scope) |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error { |
290
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || value.IsZero() { |
291
|
1 |
|
return nil |
292
|
|
|
} |
293
|
|
|
|
294
|
1 |
|
return c.newViolation(scope) |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
func (c BlankConstraint) newViolation(scope validation.Scope) validation.Violation { |
298
|
1 |
|
return scope.BuildViolation(c.code, c.messageTemplate). |
299
|
|
|
SetParameters(c.messageParameters...). |
300
|
|
|
CreateViolation() |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
// NotNilConstraint checks that a value in not strictly equal to nil. To check that values in not blank use |
304
|
|
|
// NotBlankConstraint. |
305
|
|
|
type NotNilConstraint struct { |
306
|
|
|
isIgnored bool |
307
|
|
|
groups []string |
308
|
|
|
code string |
309
|
|
|
messageTemplate string |
310
|
|
|
messageParameters validation.TemplateParameterList |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
// IsNotNil creates a NotNilConstraint to check that a value is not strictly equal to nil. |
314
|
|
|
func IsNotNil() NotNilConstraint { |
315
|
1 |
|
return NotNilConstraint{ |
316
|
|
|
code: code.NotNil, |
317
|
|
|
messageTemplate: message.Templates[code.NotNil], |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// SetUp always returns no error. |
322
|
|
|
func (c NotNilConstraint) SetUp() error { |
323
|
1 |
|
return nil |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
// Name is the constraint name. |
327
|
|
|
func (c NotNilConstraint) Name() string { |
328
|
|
|
return "NotNilConstraint" |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
332
|
|
|
// then the constraint will be ignored. |
333
|
|
|
func (c NotNilConstraint) When(condition bool) NotNilConstraint { |
334
|
1 |
|
c.isIgnored = !condition |
335
|
1 |
|
return c |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
// WhenGroups enables conditional validation of the constraint by using the validation groups. |
339
|
|
|
func (c NotNilConstraint) WhenGroups(groups ...string) NotNilConstraint { |
340
|
1 |
|
c.groups = groups |
341
|
1 |
|
return c |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
// Code overrides default code for produced violation. |
345
|
|
|
func (c NotNilConstraint) Code(code string) NotNilConstraint { |
346
|
1 |
|
c.code = code |
347
|
1 |
|
return c |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
// Message sets the violation message template. You can set custom template parameters |
351
|
|
|
// for injecting its values into the final message. |
352
|
|
|
func (c NotNilConstraint) Message(template string, parameters ...validation.TemplateParameter) NotNilConstraint { |
353
|
1 |
|
c.messageTemplate = template |
354
|
1 |
|
c.messageParameters = parameters |
355
|
1 |
|
return c |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
func (c NotNilConstraint) ValidateNil(scope validation.Scope) error { |
359
|
|
|
if c.isIgnored || scope.IsIgnored(c.groups...) { |
360
|
|
|
return nil |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return c.newViolation(scope) |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
func (c NotNilConstraint) ValidateBool(value *bool, scope validation.Scope) error { |
367
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil { |
368
|
1 |
|
return nil |
369
|
|
|
} |
370
|
|
|
|
371
|
1 |
|
return c.newViolation(scope) |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error { |
375
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || !value.IsNil() { |
376
|
1 |
|
return nil |
377
|
|
|
} |
378
|
|
|
|
379
|
1 |
|
return c.newViolation(scope) |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error { |
383
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil { |
384
|
1 |
|
return nil |
385
|
|
|
} |
386
|
|
|
|
387
|
1 |
|
return c.newViolation(scope) |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
func (c NotNilConstraint) ValidateStrings(values []string, scope validation.Scope) error { |
391
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || values != nil { |
392
|
1 |
|
return nil |
393
|
|
|
} |
394
|
|
|
|
395
|
1 |
|
return c.newViolation(scope) |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error { |
399
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil { |
400
|
1 |
|
return nil |
401
|
|
|
} |
402
|
|
|
|
403
|
1 |
|
return c.newViolation(scope) |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error { |
407
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || !value.IsNil() { |
408
|
1 |
|
return nil |
409
|
|
|
} |
410
|
|
|
|
411
|
1 |
|
return c.newViolation(scope) |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
func (c NotNilConstraint) newViolation(scope validation.Scope) validation.Violation { |
415
|
1 |
|
return scope.BuildViolation(c.code, c.messageTemplate). |
416
|
|
|
SetParameters(c.messageParameters...). |
417
|
|
|
CreateViolation() |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
// NilConstraint checks that a value in strictly equal to nil. To check that values in blank use |
421
|
|
|
// BlankConstraint. |
422
|
|
|
type NilConstraint struct { |
423
|
|
|
isIgnored bool |
424
|
|
|
groups []string |
425
|
|
|
code string |
426
|
|
|
messageTemplate string |
427
|
|
|
messageParameters validation.TemplateParameterList |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
// IsNil creates a NilConstraint to check that a value is strictly equal to nil. |
431
|
|
|
func IsNil() NilConstraint { |
432
|
1 |
|
return NilConstraint{ |
433
|
|
|
code: code.Nil, |
434
|
|
|
messageTemplate: message.Templates[code.Nil], |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
// SetUp always returns no error. |
439
|
|
|
func (c NilConstraint) SetUp() error { |
440
|
1 |
|
return nil |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
// Name is the constraint name. |
444
|
|
|
func (c NilConstraint) Name() string { |
445
|
|
|
return "NilConstraint" |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
449
|
|
|
// then the constraint will be ignored. |
450
|
|
|
func (c NilConstraint) When(condition bool) NilConstraint { |
451
|
1 |
|
c.isIgnored = !condition |
452
|
1 |
|
return c |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
// WhenGroups enables conditional validation of the constraint by using the validation groups. |
456
|
|
|
func (c NilConstraint) WhenGroups(groups ...string) NilConstraint { |
457
|
1 |
|
c.groups = groups |
458
|
1 |
|
return c |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
// Code overrides default code for produced violation. |
462
|
|
|
func (c NilConstraint) Code(code string) NilConstraint { |
463
|
1 |
|
c.code = code |
464
|
1 |
|
return c |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
// Message sets the violation message template. You can set custom template parameters |
468
|
|
|
// for injecting its values into the final message. |
469
|
|
|
func (c NilConstraint) Message(template string, parameters ...validation.TemplateParameter) NilConstraint { |
470
|
1 |
|
c.messageTemplate = template |
471
|
1 |
|
c.messageParameters = parameters |
472
|
1 |
|
return c |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
func (c NilConstraint) ValidateNil(scope validation.Scope) error { |
476
|
|
|
return nil |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
func (c NilConstraint) ValidateBool(value *bool, scope validation.Scope) error { |
480
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil { |
481
|
1 |
|
return nil |
482
|
|
|
} |
483
|
|
|
|
484
|
1 |
|
return c.newViolation(scope) |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error { |
488
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value.IsNil() { |
489
|
1 |
|
return nil |
490
|
|
|
} |
491
|
|
|
|
492
|
1 |
|
return c.newViolation(scope) |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error { |
496
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil { |
497
|
1 |
|
return nil |
498
|
|
|
} |
499
|
|
|
|
500
|
1 |
|
return c.newViolation(scope) |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
func (c NilConstraint) ValidateStrings(values []string, scope validation.Scope) error { |
504
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || values == nil { |
505
|
1 |
|
return nil |
506
|
|
|
} |
507
|
|
|
|
508
|
1 |
|
return c.newViolation(scope) |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error { |
512
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil { |
513
|
1 |
|
return nil |
514
|
|
|
} |
515
|
|
|
|
516
|
1 |
|
return c.newViolation(scope) |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error { |
520
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value.IsNil() { |
521
|
1 |
|
return nil |
522
|
|
|
} |
523
|
|
|
|
524
|
1 |
|
return c.newViolation(scope) |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
func (c NilConstraint) newViolation(scope validation.Scope) validation.Violation { |
528
|
1 |
|
return scope.BuildViolation(c.code, c.messageTemplate). |
529
|
|
|
SetParameters(c.messageParameters...). |
530
|
|
|
CreateViolation() |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
// BoolConstraint checks that a bool value in strictly equal to expected bool value. |
534
|
|
|
type BoolConstraint struct { |
535
|
|
|
isIgnored bool |
536
|
|
|
expected bool |
537
|
|
|
groups []string |
538
|
|
|
code string |
539
|
|
|
messageTemplate string |
540
|
|
|
messageParameters validation.TemplateParameterList |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
// IsTrue creates a BoolConstraint to check that a value is not strictly equal to true. |
544
|
|
|
func IsTrue() BoolConstraint { |
545
|
1 |
|
return BoolConstraint{ |
546
|
|
|
expected: true, |
547
|
|
|
code: code.True, |
548
|
|
|
messageTemplate: message.Templates[code.True], |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
// IsFalse creates a BoolConstraint to check that a value is not strictly equal to false. |
553
|
|
|
func IsFalse() BoolConstraint { |
554
|
1 |
|
return BoolConstraint{ |
555
|
|
|
expected: false, |
556
|
|
|
code: code.False, |
557
|
|
|
messageTemplate: message.Templates[code.False], |
558
|
|
|
} |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
// SetUp always returns no error. |
562
|
|
|
func (c BoolConstraint) SetUp() error { |
563
|
1 |
|
return nil |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
// Name is the constraint name. |
567
|
|
|
func (c BoolConstraint) Name() string { |
568
|
|
|
return "BoolConstraint" |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
572
|
|
|
// then the constraint will be ignored. |
573
|
|
|
func (c BoolConstraint) When(condition bool) BoolConstraint { |
574
|
1 |
|
c.isIgnored = !condition |
575
|
1 |
|
return c |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
// WhenGroups enables conditional validation of the constraint by using the validation groups. |
579
|
|
|
func (c BoolConstraint) WhenGroups(groups ...string) BoolConstraint { |
580
|
1 |
|
c.groups = groups |
581
|
1 |
|
return c |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
// Code overrides default code for produced violation. |
585
|
|
|
func (c BoolConstraint) Code(code string) BoolConstraint { |
586
|
1 |
|
c.code = code |
587
|
1 |
|
return c |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
// Message sets the violation message template. You can set custom template parameters |
591
|
|
|
// for injecting its values into the final message. |
592
|
|
|
func (c BoolConstraint) Message(template string, parameters ...validation.TemplateParameter) BoolConstraint { |
593
|
1 |
|
c.messageTemplate = template |
594
|
1 |
|
c.messageParameters = parameters |
595
|
1 |
|
return c |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error { |
599
|
1 |
|
if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || *value == c.expected { |
600
|
1 |
|
return nil |
601
|
|
|
} |
602
|
|
|
|
603
|
1 |
|
return scope.BuildViolation(c.code, c.messageTemplate). |
604
|
|
|
SetParameters(c.messageParameters...). |
605
|
|
|
CreateViolation() |
606
|
|
|
} |
607
|
|
|
|