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