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