Passed
Push — main ( a82edc...08e96c )
by Igor
58s queued 11s
created

it.BlankConstraint.SetUp   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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