Passed
Pull Request — main (#49)
by Igor
02:47
created

it.BoolConstraint.Message   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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