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
|
|
|
// IsNil creates a NilConstraint to check that a value is strictly equal to nil. |
356
|
|
|
// |
357
|
|
|
// Example |
358
|
|
|
// var s *string |
359
|
|
|
// err := validator.ValidateString(s, it.IsNil()) |
360
|
|
|
func IsNil() NilConstraint { |
361
|
1 |
|
return NilConstraint{ |
362
|
|
|
messageTemplate: message.Nil, |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
// Name is the constraint name. |
367
|
|
|
func (c NilConstraint) Name() string { |
368
|
|
|
return "NilConstraint" |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
// SetUp always returns no error. |
372
|
|
|
func (c NilConstraint) SetUp() error { |
373
|
1 |
|
return nil |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
377
|
|
|
// then the constraint will be ignored. |
378
|
|
|
func (c NilConstraint) When(condition bool) NilConstraint { |
379
|
1 |
|
c.isIgnored = !condition |
380
|
1 |
|
return c |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
// Message sets the violation message template. |
384
|
|
|
func (c NilConstraint) Message(message string) NilConstraint { |
385
|
1 |
|
c.messageTemplate = message |
386
|
1 |
|
return c |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
func (c NilConstraint) ValidateNil(scope validation.Scope) error { |
390
|
|
|
return nil |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error { |
394
|
1 |
|
if c.isIgnored { |
395
|
1 |
|
return nil |
396
|
|
|
} |
397
|
1 |
|
if value.IsNil() { |
398
|
1 |
|
return nil |
399
|
|
|
} |
400
|
|
|
|
401
|
1 |
|
return c.newViolation(scope) |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error { |
405
|
1 |
|
if c.isIgnored { |
406
|
1 |
|
return nil |
407
|
|
|
} |
408
|
1 |
|
if value == nil { |
409
|
1 |
|
return nil |
410
|
|
|
} |
411
|
|
|
|
412
|
1 |
|
return c.newViolation(scope) |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error { |
416
|
1 |
|
if c.isIgnored { |
417
|
1 |
|
return nil |
418
|
|
|
} |
419
|
1 |
|
if value == nil { |
420
|
1 |
|
return nil |
421
|
|
|
} |
422
|
|
|
|
423
|
1 |
|
return c.newViolation(scope) |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error { |
427
|
1 |
|
if c.isIgnored { |
428
|
1 |
|
return nil |
429
|
|
|
} |
430
|
1 |
|
if value.IsNil() { |
431
|
1 |
|
return nil |
432
|
|
|
} |
433
|
|
|
|
434
|
1 |
|
return c.newViolation(scope) |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
func (c NilConstraint) newViolation(scope validation.Scope) validation.Violation { |
438
|
1 |
|
return scope.BuildViolation(code.Nil, c.messageTemplate).CreateViolation() |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
// BoolConstraint checks that a bool value in strictly equal to expected bool value. |
442
|
|
|
type BoolConstraint struct { |
443
|
|
|
isIgnored bool |
444
|
|
|
value bool |
445
|
|
|
messageTemplate string |
446
|
|
|
code string |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
// IsTrue creates a BoolConstraint to check that a value is not strictly equal to true. |
450
|
|
|
// |
451
|
|
|
// Example |
452
|
|
|
// var b *bool |
453
|
|
|
// err := validator.ValidateBool(b, it.IsTrue()) |
454
|
|
|
func IsTrue() BoolConstraint { |
455
|
1 |
|
return BoolConstraint{ |
456
|
|
|
value: true, |
457
|
|
|
messageTemplate: message.True, |
458
|
|
|
code: code.True, |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
// IsFalse creates a BoolConstraint to check that a value is not strictly equal to false. |
463
|
|
|
// |
464
|
|
|
// Example |
465
|
|
|
// var b *bool |
466
|
|
|
// err := validator.ValidateBool(b, it.IsFalse()) |
467
|
|
|
func IsFalse() BoolConstraint { |
468
|
1 |
|
return BoolConstraint{ |
469
|
|
|
value: false, |
470
|
|
|
messageTemplate: message.False, |
471
|
|
|
code: code.False, |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
// Name is the constraint name. |
476
|
|
|
func (c BoolConstraint) Name() string { |
477
|
|
|
return "BoolConstraint" |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
// SetUp always returns no error. |
481
|
|
|
func (c BoolConstraint) SetUp() error { |
482
|
1 |
|
return nil |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
486
|
|
|
// then the constraint will be ignored. |
487
|
|
|
func (c BoolConstraint) When(condition bool) BoolConstraint { |
488
|
1 |
|
c.isIgnored = !condition |
489
|
1 |
|
return c |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
// Message sets the violation message template. |
493
|
|
|
func (c BoolConstraint) Message(message string) BoolConstraint { |
494
|
1 |
|
c.messageTemplate = message |
495
|
1 |
|
return c |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error { |
499
|
1 |
|
if c.isIgnored || value == nil || c.value == *value { |
500
|
1 |
|
return nil |
501
|
|
|
} |
502
|
|
|
|
503
|
1 |
|
return c.newViolation(scope) |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
func (c BoolConstraint) newViolation(scope validation.Scope) validation.Violation { |
507
|
1 |
|
return scope.BuildViolation(c.code, c.messageTemplate).CreateViolation() |
508
|
|
|
} |
509
|
|
|
|