Test Setup Failed
Pull Request — main (#57)
by Igor
01:27
created

it.NilConstraint.ValidateStrings   A

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 3
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
	if c.allowNil && value.IsNil() {
141 1
		return nil
142
	}
143
	if value.Count() > 0 {
144
		return nil
145 1
	}
146 1
147
	return c.newViolation(scope)
148 1
}
149 1
150
func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
151
	if c.isIgnored || count > 0 {
152 1
		return nil
153 1
	}
154 1
155
	return c.newViolation(scope)
156
}
157 1
158
func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
159
	if c.isIgnored {
160
		return nil
161 1
	}
162
	if c.allowNil && value == nil {
163
		return nil
164
	}
165
166
	var empty time.Time
167
	if value != nil && *value != empty {
168
		return nil
169
	}
170
171
	return c.newViolation(scope)
172
}
173
174
func (c NotBlankConstraint) newViolation(scope validation.Scope) validation.Violation {
175
	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 1
// 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 1
// 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
	return BlankConstraint{
196
		code:            code.Blank,
197
		messageTemplate: message.Blank,
198
	}
199
}
200 1
201 1
// SetUp always returns no error.
202
func (c BlankConstraint) SetUp() error {
203
	return nil
204
}
205
206 1
// Name is the constraint name.
207 1
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 1
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 1
func (c BlankConstraint) Code(code string) BlankConstraint {
220
	c.code = code
221
	return c
222
}
223 1
224 1
// 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
	c.messageParameters = parameters
229
	return c
230
}
231 1
232 1
func (c BlankConstraint) ValidateNil(scope validation.Scope) error {
233
	return nil
234
}
235 1
236
func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error {
237
	if c.isIgnored || value == nil || !*value {
238
		return nil
239 1
	}
240 1
241
	return c.newViolation(scope)
242
}
243 1
244
func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
245
	if c.isIgnored || value.IsNil() || value.IsZero() {
246
		return nil
247 1
	}
248 1
249
	return c.newViolation(scope)
250
}
251 1
252
func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error {
253
	if c.isIgnored || value == nil || *value == "" {
254
		return nil
255 1
	}
256 1
257
	return c.newViolation(scope)
258
}
259 1
260
func (c BlankConstraint) ValidateStrings(values []string, scope validation.Scope) error {
261
	if c.isIgnored || len(values) == 0 {
262
		return nil
263 1
	}
264 1
265 1
	return c.newViolation(scope)
266
}
267
268 1
func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
269
	if c.isIgnored || value.Count() == 0 {
270
		return nil
271
	}
272 1
273
	return c.newViolation(scope)
274
}
275
276
func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
277
	if c.isIgnored || count == 0 {
278
		return nil
279
	}
280
281
	return c.newViolation(scope)
282
}
283
284
func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
285
	var empty time.Time
286
	if c.isIgnored || value == nil || *value == empty {
287
		return nil
288
	}
289
290
	return c.newViolation(scope)
291
}
292 1
293
func (c BlankConstraint) newViolation(scope validation.Scope) validation.Violation {
294
	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 1
// 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 1
//  var s *string
312 1
//  err := validator.ValidateString(s, it.IsNotNil())
313
func IsNotNil() NotNilConstraint {
314
	return NotNilConstraint{
315
		code:            code.NotNil,
316
		messageTemplate: message.NotNil,
317 1
	}
318 1
}
319
320
// SetUp always returns no error.
321
func (c NotNilConstraint) SetUp() error {
322
	return nil
323
}
324 1
325 1
// Name is the constraint name.
326 1
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
	c.isIgnored = !condition
334
	return c
335
}
336
337
// Code overrides default code for produced violation.
338 1
func (c NotNilConstraint) Code(code string) NotNilConstraint {
339 1
	c.code = code
340
	return c
341 1
}
342 1
343
// Message sets the violation message template. You can set custom template parameters
344
// for injecting its values into the final message.
345 1
func (c NotNilConstraint) Message(template string, parameters ...validation.TemplateParameter) NotNilConstraint {
346
	c.messageTemplate = template
347
	c.messageParameters = parameters
348
	return c
349 1
}
350 1
351
func (c NotNilConstraint) ValidateNil(scope validation.Scope) error {
352 1
	if c.isIgnored {
353 1
		return nil
354
	}
355
356 1
	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 1
364 1
	return c.newViolation(scope)
365
}
366
367 1
func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
368
	if c.isIgnored || !value.IsNil() {
369
		return nil
370
	}
371 1
372 1
	return c.newViolation(scope)
373
}
374 1
375 1
func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error {
376
	if c.isIgnored || value != nil {
377
		return nil
378 1
	}
379
380
	return c.newViolation(scope)
381
}
382 1
383
func (c NotNilConstraint) ValidateStrings(values []string, scope validation.Scope) error {
384
	if c.isIgnored || values != nil {
385
		return nil
386
	}
387
388
	return c.newViolation(scope)
389
}
390
391
func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
392
	if c.isIgnored || value != nil {
393
		return nil
394
	}
395
396
	return c.newViolation(scope)
397
}
398
399
func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
400
	if c.isIgnored || !value.IsNil() {
401
		return nil
402 1
	}
403
404
	return c.newViolation(scope)
405
}
406
407
func (c NotNilConstraint) newViolation(scope validation.Scope) validation.Violation {
408
	return scope.BuildViolation(c.code, c.messageTemplate).
409
		SetParameters(c.messageParameters...).
410 1
		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 1
422 1
// 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 1
func IsNil() NilConstraint {
428 1
	return NilConstraint{
429
		code:            code.Nil,
430
		messageTemplate: message.Nil,
431
	}
432
}
433
434 1
// SetUp always returns no error.
435 1
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 1
// When enables conditional validation of this constraint. If the expression evaluates to false,
445 1
// then the constraint will be ignored.
446
func (c NilConstraint) When(condition bool) NilConstraint {
447
	c.isIgnored = !condition
448 1
	return c
449
}
450
451
// Code overrides default code for produced violation.
452 1
func (c NilConstraint) Code(code string) NilConstraint {
453 1
	c.code = code
454
	return c
455
}
456 1
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
	return c
463
}
464 1
465
func (c NilConstraint) ValidateNil(scope validation.Scope) error {
466
	return nil
467
}
468 1
469 1
func (c NilConstraint) ValidateBool(value *bool, scope validation.Scope) error {
470
	if c.isIgnored || value == nil {
471
		return nil
472 1
	}
473
474
	return c.newViolation(scope)
475
}
476 1
477
func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
478
	if c.isIgnored || value.IsNil() {
479
		return nil
480
	}
481
482
	return c.newViolation(scope)
483
}
484
485
func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error {
486
	if c.isIgnored || value == nil {
487
		return nil
488
	}
489
490
	return c.newViolation(scope)
491
}
492
493
func (c NilConstraint) ValidateStrings(values []string, scope validation.Scope) error {
494
	if c.isIgnored || values == nil {
495
		return nil
496 1
	}
497
498
	return c.newViolation(scope)
499
}
500
501
func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
502
	if c.isIgnored || value == nil {
503
		return nil
504
	}
505
506
	return c.newViolation(scope)
507
}
508
509 1
func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
510
	if c.isIgnored || value.IsNil() {
511
		return nil
512
	}
513
514
	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 1
	messageParameters validation.TemplateParameterList
530 1
}
531
532
// IsTrue creates a BoolConstraint to check that a value is not strictly equal to true.
533
//
534
// Example
535 1
//  var b *bool
536 1
//  err := validator.ValidateBool(b, it.IsTrue())
537
func IsTrue() BoolConstraint {
538
	return BoolConstraint{
539
		expected:        true,
540
		code:            code.True,
541
		messageTemplate: message.True,
542 1
	}
543 1
}
544 1
545
// IsFalse creates a BoolConstraint to check that a value is not strictly equal to false.
546
//
547
// Example
548 1
//  var b *bool
549 1
//  err := validator.ValidateBool(b, it.IsFalse())
550
func IsFalse() BoolConstraint {
551
	return BoolConstraint{
552 1
		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
	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
	c.isIgnored = !condition
572
	return c
573
}
574
575
// Code overrides default code for produced violation.
576
func (c BoolConstraint) Code(code string) BoolConstraint {
577
	c.code = code
578
	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
	c.messageTemplate = template
585
	c.messageParameters = parameters
586
	return c
587
}
588
589
func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error {
590
	if c.isIgnored || value == nil || *value == c.expected {
591
		return nil
592
	}
593
594
	return scope.BuildViolation(c.code, c.messageTemplate).
595
		SetParameters(c.messageParameters...).
596
		CreateViolation()
597
}
598