Passed
Push — main ( 10a5f7...41473a )
by Igor
02:50 queued 55s
created

it.BlankConstraint.Code   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 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) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
123 1
	if c.isIgnored {
124 1
		return nil
125
	}
126 1
	if c.allowNil && value.IsNil() {
127 1
		return nil
128
	}
129 1
	if value.Count() > 0 {
130 1
		return nil
131
	}
132
133 1
	return c.newViolation(scope)
134
}
135
136
func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
137 1
	if c.isIgnored || count > 0 {
138 1
		return nil
139
	}
140
141 1
	return c.newViolation(scope)
142
}
143
144
func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
145 1
	if c.isIgnored {
146 1
		return nil
147
	}
148 1
	if c.allowNil && value == nil {
149 1
		return nil
150
	}
151
152 1
	var empty time.Time
153 1
	if value != nil && *value != empty {
154 1
		return nil
155
	}
156
157 1
	return c.newViolation(scope)
158
}
159
160
func (c NotBlankConstraint) newViolation(scope validation.Scope) validation.Violation {
161 1
	return scope.BuildViolation(c.code, c.messageTemplate).
162
		SetParameters(c.messageParameters...).
163
		CreateViolation()
164
}
165
166
// BlankConstraint checks that a value is blank: equal to false, nil, zero, an empty string, an empty
167
// slice, array, or a map.
168
type BlankConstraint struct {
169
	isIgnored         bool
170
	code              string
171
	messageTemplate   string
172
	messageParameters validation.TemplateParameterList
173
}
174
175
// IsBlank creates a BlankConstraint for checking that value is empty.
176
//
177
// Example
178
//  s := "foo"
179
//  err := validator.ValidateString(&s, it.IsBlank())
180
func IsBlank() BlankConstraint {
181 1
	return BlankConstraint{
182
		code:            code.Blank,
183
		messageTemplate: message.Blank,
184
	}
185
}
186
187
// SetUp always returns no error.
188
func (c BlankConstraint) SetUp() error {
189 1
	return nil
190
}
191
192
// Name is the constraint name.
193
func (c BlankConstraint) Name() string {
194
	return "BlankConstraint"
195
}
196
197
// When enables conditional validation of this constraint. If the expression evaluates to false,
198
// then the constraint will be ignored.
199
func (c BlankConstraint) When(condition bool) BlankConstraint {
200 1
	c.isIgnored = !condition
201 1
	return c
202
}
203
204
// Code overrides default code for produced violation.
205
func (c BlankConstraint) Code(code string) BlankConstraint {
206 1
	c.code = code
207 1
	return c
208
}
209
210
// Message sets the violation message template. You can set custom template parameters
211
// for injecting its values into the final message.
212
func (c BlankConstraint) Message(template string, parameters ...validation.TemplateParameter) BlankConstraint {
213 1
	c.messageTemplate = template
214 1
	c.messageParameters = parameters
215 1
	return c
216
}
217
218
func (c BlankConstraint) ValidateNil(scope validation.Scope) error {
219 1
	return nil
220
}
221
222
func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error {
223 1
	if c.isIgnored || value == nil || !*value {
224 1
		return nil
225
	}
226
227 1
	return c.newViolation(scope)
228
}
229
230
func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
231 1
	if c.isIgnored || value.IsNil() || value.IsZero() {
232 1
		return nil
233
	}
234
235 1
	return c.newViolation(scope)
236
}
237
238
func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error {
239 1
	if c.isIgnored || value == nil || *value == "" {
240 1
		return nil
241
	}
242
243 1
	return c.newViolation(scope)
244
}
245
246
func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
247 1
	if c.isIgnored || value.Count() == 0 {
248 1
		return nil
249
	}
250
251 1
	return c.newViolation(scope)
252
}
253
254
func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
255 1
	if c.isIgnored || count == 0 {
256 1
		return nil
257
	}
258
259 1
	return c.newViolation(scope)
260
}
261
262
func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
263 1
	var empty time.Time
264 1
	if c.isIgnored || value == nil || *value == empty {
265 1
		return nil
266
	}
267
268 1
	return c.newViolation(scope)
269
}
270
271
func (c BlankConstraint) newViolation(scope validation.Scope) validation.Violation {
272 1
	return scope.BuildViolation(c.code, c.messageTemplate).
273
		SetParameters(c.messageParameters...).
274
		CreateViolation()
275
}
276
277
// NotNilConstraint checks that a value in not strictly equal to nil. To check that values in not blank use
278
// NotBlankConstraint.
279
type NotNilConstraint struct {
280
	isIgnored         bool
281
	code              string
282
	messageTemplate   string
283
	messageParameters validation.TemplateParameterList
284
}
285
286
// IsNotNil creates a NotNilConstraint to check that a value is not strictly equal to nil.
287
//
288
// Example
289
//  var s *string
290
//  err := validator.ValidateString(s, it.IsNotNil())
291
func IsNotNil() NotNilConstraint {
292 1
	return NotNilConstraint{
293
		code:            code.NotNil,
294
		messageTemplate: message.NotNil,
295
	}
296
}
297
298
// SetUp always returns no error.
299
func (c NotNilConstraint) SetUp() error {
300 1
	return nil
301
}
302
303
// Name is the constraint name.
304
func (c NotNilConstraint) Name() string {
305
	return "NotNilConstraint"
306
}
307
308
// When enables conditional validation of this constraint. If the expression evaluates to false,
309
// then the constraint will be ignored.
310
func (c NotNilConstraint) When(condition bool) NotNilConstraint {
311 1
	c.isIgnored = !condition
312 1
	return c
313
}
314
315
// Code overrides default code for produced violation.
316
func (c NotNilConstraint) Code(code string) NotNilConstraint {
317 1
	c.code = code
318 1
	return c
319
}
320
321
// Message sets the violation message template. You can set custom template parameters
322
// for injecting its values into the final message.
323
func (c NotNilConstraint) Message(template string, parameters ...validation.TemplateParameter) NotNilConstraint {
324 1
	c.messageTemplate = template
325 1
	c.messageParameters = parameters
326 1
	return c
327
}
328
329
func (c NotNilConstraint) ValidateNil(scope validation.Scope) error {
330
	if c.isIgnored {
331
		return nil
332
	}
333
334
	return c.newViolation(scope)
335
}
336
337
func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
338 1
	if c.isIgnored {
339 1
		return nil
340
	}
341 1
	if !value.IsNil() {
342 1
		return nil
343
	}
344
345 1
	return c.newViolation(scope)
346
}
347
348
func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error {
349 1
	if c.isIgnored {
350 1
		return nil
351
	}
352 1
	if value != nil {
353 1
		return nil
354
	}
355
356 1
	return c.newViolation(scope)
357
}
358
359
func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
360 1
	if c.isIgnored {
361 1
		return nil
362
	}
363 1
	if value != nil {
364 1
		return nil
365
	}
366
367 1
	return c.newViolation(scope)
368
}
369
370
func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
371 1
	if c.isIgnored {
372 1
		return nil
373
	}
374 1
	if !value.IsNil() {
375 1
		return nil
376
	}
377
378 1
	return c.newViolation(scope)
379
}
380
381
func (c NotNilConstraint) newViolation(scope validation.Scope) validation.Violation {
382 1
	return scope.BuildViolation(c.code, c.messageTemplate).
383
		SetParameters(c.messageParameters...).
384
		CreateViolation()
385
}
386
387
// NilConstraint checks that a value in strictly equal to nil. To check that values in blank use
388
// BlankConstraint.
389
type NilConstraint struct {
390
	isIgnored         bool
391
	code              string
392
	messageTemplate   string
393
	messageParameters validation.TemplateParameterList
394
}
395
396
// IsNil creates a NilConstraint to check that a value is strictly equal to nil.
397
//
398
// Example
399
//  var s *string
400
//  err := validator.ValidateString(s, it.IsNil())
401
func IsNil() NilConstraint {
402 1
	return NilConstraint{
403
		code:            code.Nil,
404
		messageTemplate: message.Nil,
405
	}
406
}
407
408
// SetUp always returns no error.
409
func (c NilConstraint) SetUp() error {
410 1
	return nil
411
}
412
413
// Name is the constraint name.
414
func (c NilConstraint) Name() string {
415
	return "NilConstraint"
416
}
417
418
// When enables conditional validation of this constraint. If the expression evaluates to false,
419
// then the constraint will be ignored.
420
func (c NilConstraint) When(condition bool) NilConstraint {
421 1
	c.isIgnored = !condition
422 1
	return c
423
}
424
425
// Code overrides default code for produced violation.
426
func (c NilConstraint) Code(code string) NilConstraint {
427 1
	c.code = code
428 1
	return c
429
}
430
431
// Message sets the violation message template. You can set custom template parameters
432
// for injecting its values into the final message.
433
func (c NilConstraint) Message(template string, parameters ...validation.TemplateParameter) NilConstraint {
434 1
	c.messageTemplate = template
435 1
	c.messageParameters = parameters
436 1
	return c
437
}
438
439
func (c NilConstraint) ValidateNil(scope validation.Scope) error {
440
	return nil
441
}
442
443
func (c NilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
444 1
	if c.isIgnored || value.IsNil() {
445 1
		return nil
446
	}
447
448 1
	return c.newViolation(scope)
449
}
450
451
func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error {
452 1
	if c.isIgnored || value == nil {
453 1
		return nil
454
	}
455
456 1
	return c.newViolation(scope)
457
}
458
459
func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
460 1
	if c.isIgnored || value == nil {
461 1
		return nil
462
	}
463
464 1
	return c.newViolation(scope)
465
}
466
467
func (c NilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
468 1
	if c.isIgnored || value.IsNil() {
469 1
		return nil
470
	}
471
472 1
	return c.newViolation(scope)
473
}
474
475
func (c NilConstraint) newViolation(scope validation.Scope) validation.Violation {
476 1
	return scope.BuildViolation(c.code, c.messageTemplate).
477
		SetParameters(c.messageParameters...).
478
		CreateViolation()
479
}
480
481
// BoolConstraint checks that a bool value in strictly equal to expected bool value.
482
type BoolConstraint struct {
483
	isIgnored         bool
484
	expected          bool
485
	code              string
486
	messageTemplate   string
487
	messageParameters validation.TemplateParameterList
488
}
489
490
// IsTrue creates a BoolConstraint to check that a value is not strictly equal to true.
491
//
492
// Example
493
//  var b *bool
494
//  err := validator.ValidateBool(b, it.IsTrue())
495
func IsTrue() BoolConstraint {
496 1
	return BoolConstraint{
497
		expected:        true,
498
		code:            code.True,
499
		messageTemplate: message.True,
500
	}
501
}
502
503
// IsFalse creates a BoolConstraint to check that a value is not strictly equal to false.
504
//
505
// Example
506
//  var b *bool
507
//  err := validator.ValidateBool(b, it.IsFalse())
508
func IsFalse() BoolConstraint {
509 1
	return BoolConstraint{
510
		expected:        false,
511
		code:            code.False,
512
		messageTemplate: message.False,
513
	}
514
}
515
516
// SetUp always returns no error.
517
func (c BoolConstraint) SetUp() error {
518 1
	return nil
519
}
520
521
// Name is the constraint name.
522
func (c BoolConstraint) Name() string {
523
	return "BoolConstraint"
524
}
525
526
// When enables conditional validation of this constraint. If the expression evaluates to false,
527
// then the constraint will be ignored.
528
func (c BoolConstraint) When(condition bool) BoolConstraint {
529 1
	c.isIgnored = !condition
530 1
	return c
531
}
532
533
// Code overrides default code for produced violation.
534
func (c BoolConstraint) Code(code string) BoolConstraint {
535 1
	c.code = code
536 1
	return c
537
}
538
539
// Message sets the violation message template. You can set custom template parameters
540
// for injecting its values into the final message.
541
func (c BoolConstraint) Message(template string, parameters ...validation.TemplateParameter) BoolConstraint {
542 1
	c.messageTemplate = template
543 1
	c.messageParameters = parameters
544 1
	return c
545
}
546
547
func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error {
548 1
	if c.isIgnored || value == nil || *value == c.expected {
549 1
		return nil
550
	}
551
552 1
	return scope.BuildViolation(c.code, c.messageTemplate).
553
		SetParameters(c.messageParameters...).
554
		CreateViolation()
555
}
556