Test Failed
Pull Request — main (#72)
by Igor
01:54
created

it.BoolConstraint.ValidateBool   A

Complexity

Conditions 5

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 5

Importance

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