Passed
Pull Request — main (#68)
by Igor
02:34
created

it.NotBlankConstraint.ValidateIterable   B

Complexity

Conditions 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 8
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 6
rs 8.6666
c 0
b 0
f 0
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
	var empty time.Time
170 1
	if value != nil && *value != empty {
171 1
		return nil
172
	}
173
174 1
	return c.newViolation(scope)
175
}
176
177
func (c NotBlankConstraint) newViolation(scope validation.Scope) validation.Violation {
178 1
	return scope.BuildViolation(c.code, c.messageTemplate).
179
		SetParameters(c.messageParameters...).
180
		CreateViolation()
181
}
182
183
// BlankConstraint checks that a value is blank: equal to false, nil, zero, an empty string, an empty
184
// slice, array, or a map.
185
type BlankConstraint struct {
186
	isIgnored         bool
187
	groups            []string
188
	code              string
189
	messageTemplate   string
190
	messageParameters validation.TemplateParameterList
191
}
192
193
// IsBlank creates a BlankConstraint for checking that value is empty.
194
func IsBlank() BlankConstraint {
195 1
	return BlankConstraint{
196
		code:            code.Blank,
197
		messageTemplate: message.Templates[code.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
// WhenGroups enables conditional validation of the constraint by using the validation groups.
219
func (c BlankConstraint) WhenGroups(groups ...string) BlankConstraint {
220 1
	c.groups = groups
221 1
	return c
222
}
223
224
// Code overrides default code for produced violation.
225
func (c BlankConstraint) Code(code string) BlankConstraint {
226 1
	c.code = code
227 1
	return c
228
}
229
230
// Message sets the violation message template. You can set custom template parameters
231
// for injecting its values into the final message.
232
func (c BlankConstraint) Message(template string, parameters ...validation.TemplateParameter) BlankConstraint {
233 1
	c.messageTemplate = template
234 1
	c.messageParameters = parameters
235 1
	return c
236
}
237
238
func (c BlankConstraint) ValidateNil(scope validation.Scope) error {
239 1
	return nil
240
}
241
242
func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error {
243 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || !*value {
244 1
		return nil
245
	}
246
247 1
	return c.newViolation(scope)
248
}
249
250
func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
251 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value.IsNil() || value.IsZero() {
252 1
		return nil
253
	}
254
255 1
	return c.newViolation(scope)
256
}
257
258
func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error {
259 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || *value == "" {
260 1
		return nil
261
	}
262
263 1
	return c.newViolation(scope)
264
}
265
266
func (c BlankConstraint) ValidateStrings(values []string, scope validation.Scope) error {
267 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || len(values) == 0 {
268 1
		return nil
269
	}
270
271 1
	return c.newViolation(scope)
272
}
273
274
func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
275 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value.Count() == 0 {
276 1
		return nil
277
	}
278
279 1
	return c.newViolation(scope)
280
}
281
282
func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
283 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || count == 0 {
284 1
		return nil
285
	}
286
287 1
	return c.newViolation(scope)
288
}
289
290
func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
291 1
	var empty time.Time
292 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || *value == empty {
293 1
		return nil
294
	}
295
296 1
	return c.newViolation(scope)
297
}
298
299
func (c BlankConstraint) newViolation(scope validation.Scope) validation.Violation {
300 1
	return scope.BuildViolation(c.code, c.messageTemplate).
301
		SetParameters(c.messageParameters...).
302
		CreateViolation()
303
}
304
305
// NotNilConstraint checks that a value in not strictly equal to nil. To check that values in not blank use
306
// NotBlankConstraint.
307
type NotNilConstraint struct {
308
	isIgnored         bool
309
	groups            []string
310
	code              string
311
	messageTemplate   string
312
	messageParameters validation.TemplateParameterList
313
}
314
315
// IsNotNil creates a NotNilConstraint to check that a value is not strictly equal to nil.
316
func IsNotNil() NotNilConstraint {
317 1
	return NotNilConstraint{
318
		code:            code.NotNil,
319
		messageTemplate: message.Templates[code.NotNil],
320
	}
321
}
322
323
// SetUp always returns no error.
324
func (c NotNilConstraint) SetUp() error {
325 1
	return nil
326
}
327
328
// Name is the constraint name.
329
func (c NotNilConstraint) Name() string {
330
	return "NotNilConstraint"
331
}
332
333
// When enables conditional validation of this constraint. If the expression evaluates to false,
334
// then the constraint will be ignored.
335
func (c NotNilConstraint) When(condition bool) NotNilConstraint {
336 1
	c.isIgnored = !condition
337 1
	return c
338
}
339
340
// WhenGroups enables conditional validation of the constraint by using the validation groups.
341
func (c NotNilConstraint) WhenGroups(groups ...string) NotNilConstraint {
342 1
	c.groups = groups
343 1
	return c
344
}
345
346
// Code overrides default code for produced violation.
347
func (c NotNilConstraint) Code(code string) NotNilConstraint {
348 1
	c.code = code
349 1
	return c
350
}
351
352
// Message sets the violation message template. You can set custom template parameters
353
// for injecting its values into the final message.
354
func (c NotNilConstraint) Message(template string, parameters ...validation.TemplateParameter) NotNilConstraint {
355 1
	c.messageTemplate = template
356 1
	c.messageParameters = parameters
357 1
	return c
358
}
359
360
func (c NotNilConstraint) ValidateNil(scope validation.Scope) error {
361
	if c.isIgnored || scope.IsIgnored(c.groups...) {
362
		return nil
363
	}
364
365
	return c.newViolation(scope)
366
}
367
368
func (c NotNilConstraint) ValidateBool(value *bool, scope validation.Scope) error {
369 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil {
370 1
		return nil
371
	}
372
373 1
	return c.newViolation(scope)
374
}
375
376
func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
377 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || !value.IsNil() {
378 1
		return nil
379
	}
380
381 1
	return c.newViolation(scope)
382
}
383
384
func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error {
385 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil {
386 1
		return nil
387
	}
388
389 1
	return c.newViolation(scope)
390
}
391
392
func (c NotNilConstraint) ValidateStrings(values []string, scope validation.Scope) error {
393 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || values != nil {
394 1
		return nil
395
	}
396
397 1
	return c.newViolation(scope)
398
}
399
400
func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
401 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil {
402 1
		return nil
403
	}
404
405 1
	return c.newViolation(scope)
406
}
407
408
func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
409 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || !value.IsNil() {
410 1
		return nil
411
	}
412
413 1
	return c.newViolation(scope)
414
}
415
416
func (c NotNilConstraint) newViolation(scope validation.Scope) validation.Violation {
417 1
	return scope.BuildViolation(c.code, c.messageTemplate).
418
		SetParameters(c.messageParameters...).
419
		CreateViolation()
420
}
421
422
// NilConstraint checks that a value in strictly equal to nil. To check that values in blank use
423
// BlankConstraint.
424
type NilConstraint struct {
425
	isIgnored         bool
426
	groups            []string
427
	code              string
428
	messageTemplate   string
429
	messageParameters validation.TemplateParameterList
430
}
431
432
// IsNil creates a NilConstraint to check that a value is strictly equal to nil.
433
func IsNil() NilConstraint {
434 1
	return NilConstraint{
435
		code:            code.Nil,
436
		messageTemplate: message.Templates[code.Nil],
437
	}
438
}
439
440
// SetUp always returns no error.
441
func (c NilConstraint) SetUp() error {
442 1
	return nil
443
}
444
445
// Name is the constraint name.
446
func (c NilConstraint) Name() string {
447
	return "NilConstraint"
448
}
449
450
// When enables conditional validation of this constraint. If the expression evaluates to false,
451
// then the constraint will be ignored.
452
func (c NilConstraint) When(condition bool) NilConstraint {
453 1
	c.isIgnored = !condition
454 1
	return c
455
}
456
457
// WhenGroups enables conditional validation of the constraint by using the validation groups.
458
func (c NilConstraint) WhenGroups(groups ...string) NilConstraint {
459 1
	c.groups = groups
460 1
	return c
461
}
462
463
// Code overrides default code for produced violation.
464
func (c NilConstraint) Code(code string) NilConstraint {
465 1
	c.code = code
466 1
	return c
467
}
468
469
// Message sets the violation message template. You can set custom template parameters
470
// for injecting its values into the final message.
471
func (c NilConstraint) Message(template string, parameters ...validation.TemplateParameter) NilConstraint {
472 1
	c.messageTemplate = template
473 1
	c.messageParameters = parameters
474 1
	return c
475
}
476
477
func (c NilConstraint) ValidateNil(scope validation.Scope) error {
478
	return nil
479
}
480
481
func (c NilConstraint) ValidateBool(value *bool, scope validation.Scope) error {
482 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil {
483 1
		return nil
484
	}
485
486 1
	return c.newViolation(scope)
487
}
488
489
func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
490 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value.IsNil() {
491 1
		return nil
492
	}
493
494 1
	return c.newViolation(scope)
495
}
496
497
func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error {
498 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil {
499 1
		return nil
500
	}
501
502 1
	return c.newViolation(scope)
503
}
504
505
func (c NilConstraint) ValidateStrings(values []string, scope validation.Scope) error {
506 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || values == nil {
507 1
		return nil
508
	}
509
510 1
	return c.newViolation(scope)
511
}
512
513
func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
514 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil {
515 1
		return nil
516
	}
517
518 1
	return c.newViolation(scope)
519
}
520
521
func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
522 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value.IsNil() {
523 1
		return nil
524
	}
525
526 1
	return c.newViolation(scope)
527
}
528
529
func (c NilConstraint) newViolation(scope validation.Scope) validation.Violation {
530 1
	return scope.BuildViolation(c.code, c.messageTemplate).
531
		SetParameters(c.messageParameters...).
532
		CreateViolation()
533
}
534
535
// BoolConstraint checks that a bool value in strictly equal to expected bool value.
536
type BoolConstraint struct {
537
	isIgnored         bool
538
	expected          bool
539
	groups            []string
540
	code              string
541
	messageTemplate   string
542
	messageParameters validation.TemplateParameterList
543
}
544
545
// IsTrue creates a BoolConstraint to check that a value is not strictly equal to true.
546
func IsTrue() BoolConstraint {
547 1
	return BoolConstraint{
548
		expected:        true,
549
		code:            code.True,
550
		messageTemplate: message.Templates[code.True],
551
	}
552
}
553
554
// IsFalse creates a BoolConstraint to check that a value is not strictly equal to false.
555
func IsFalse() BoolConstraint {
556 1
	return BoolConstraint{
557
		expected:        false,
558
		code:            code.False,
559
		messageTemplate: message.Templates[code.False],
560
	}
561
}
562
563
// SetUp always returns no error.
564
func (c BoolConstraint) SetUp() error {
565 1
	return nil
566
}
567
568
// Name is the constraint name.
569
func (c BoolConstraint) Name() string {
570
	return "BoolConstraint"
571
}
572
573
// When enables conditional validation of this constraint. If the expression evaluates to false,
574
// then the constraint will be ignored.
575
func (c BoolConstraint) When(condition bool) BoolConstraint {
576 1
	c.isIgnored = !condition
577 1
	return c
578
}
579
580
// WhenGroups enables conditional validation of the constraint by using the validation groups.
581
func (c BoolConstraint) WhenGroups(groups ...string) BoolConstraint {
582 1
	c.groups = groups
583 1
	return c
584
}
585
586
// Code overrides default code for produced violation.
587
func (c BoolConstraint) Code(code string) BoolConstraint {
588 1
	c.code = code
589 1
	return c
590
}
591
592
// Message sets the violation message template. You can set custom template parameters
593
// for injecting its values into the final message.
594
func (c BoolConstraint) Message(template string, parameters ...validation.TemplateParameter) BoolConstraint {
595 1
	c.messageTemplate = template
596 1
	c.messageParameters = parameters
597 1
	return c
598
}
599
600
func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error {
601 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || *value == c.expected {
602 1
		return nil
603
	}
604
605 1
	return scope.BuildViolation(c.code, c.messageTemplate).
606
		SetParameters(c.messageParameters...).
607
		CreateViolation()
608
}
609