Test Failed
Pull Request — main (#71)
by Igor
02:01
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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A it.BlankConstraint.WhenGroups 0 3 1
A it.BlankConstraint.When 0 3 1
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
// false or nil. Nil behavior is configurable via AllowNil() method.
13
// To check that a value is not nil only use NotNilConstraint.
14
// To check not blank numeric value use NotBlankNumberConstraint.
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
// AllowNil makes nil values valid.
33
func (c NotBlankConstraint) AllowNil() NotBlankConstraint {
34 1
	c.allowNil = true
35
	return c
36
}
37
38
// When enables conditional validation of this constraint. If the expression evaluates to false,
39
// then the constraint will be ignored.
40
func (c NotBlankConstraint) When(condition bool) NotBlankConstraint {
41
	c.isIgnored = !condition
42
	return c
43
}
44 1
45 1
// WhenGroups enables conditional validation of the constraint by using the validation groups.
46
func (c NotBlankConstraint) WhenGroups(groups ...string) NotBlankConstraint {
47
	c.groups = groups
48
	return c
49
}
50
51 1
// Code overrides default code for produced violation.
52 1
func (c NotBlankConstraint) Code(code string) NotBlankConstraint {
53
	c.code = code
54
	return c
55
}
56
57 1
// Message sets the violation message template. You can set custom template parameters
58 1
// for injecting its values into the final message.
59
func (c NotBlankConstraint) Message(template string, parameters ...validation.TemplateParameter) NotBlankConstraint {
60
	c.messageTemplate = template
61
	c.messageParameters = parameters
62
	return c
63 1
}
64 1
65
func (c NotBlankConstraint) ValidateBool(value *bool, scope validation.Scope) error {
66
	if c.isIgnored || scope.IsIgnored(c.groups...) {
67
		return nil
68
	}
69
	if c.allowNil && value == nil {
70 1
		return nil
71 1
	}
72 1
	if value != nil && *value {
73
		return nil
74
	}
75
76 1
	return c.newViolation(scope)
77 1
}
78
79
func (c NotBlankConstraint) ValidateString(value *string, scope validation.Scope) error {
80 1
	if c.isIgnored || scope.IsIgnored(c.groups...) {
81
		return nil
82
	}
83
	if c.allowNil && value == nil {
84 1
		return nil
85 1
	}
86
	if value != nil && *value != "" {
87 1
		return nil
88 1
	}
89
90 1
	return c.newViolation(scope)
91 1
}
92
93
func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
94 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || count > 0 {
95
		return nil
96
	}
97
98 1
	return c.newViolation(scope)
99 1
}
100
101 1
func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
102 1
	if c.isIgnored || scope.IsIgnored(c.groups...) {
103
		return nil
104 1
	}
105 1
	if c.allowNil && value == nil {
106
		return nil
107
	}
108 1
109
	if value != nil && !value.IsZero() {
110
		return nil
111
	}
112 1
113 1
	return c.newViolation(scope)
114
}
115 1
116 1
func (c NotBlankConstraint) newViolation(scope validation.Scope) validation.Violation {
117
	return scope.BuildViolation(c.code, c.messageTemplate).
118 1
		SetParameters(c.messageParameters...).
119 1
		CreateViolation()
120
}
121
122 1
// BlankConstraint checks that a value is blank: equal to false, nil, zero, an empty string, an empty
123
// slice, array, or a map.
124
type BlankConstraint struct {
125
	isIgnored         bool
126 1
	groups            []string
127 1
	code              string
128
	messageTemplate   string
129 1
	messageParameters validation.TemplateParameterList
130 1
}
131
132 1
// IsBlank creates a BlankConstraint for checking that value is empty.
133 1
func IsBlank() BlankConstraint {
134
	return BlankConstraint{
135
		code:            code.Blank,
136 1
		messageTemplate: message.Templates[code.Blank],
137
	}
138
}
139
140 1
// When enables conditional validation of this constraint. If the expression evaluates to false,
141 1
// then the constraint will be ignored.
142
func (c BlankConstraint) When(condition bool) BlankConstraint {
143 1
	c.isIgnored = !condition
144 1
	return c
145
}
146 1
147 1
// WhenGroups enables conditional validation of the constraint by using the validation groups.
148
func (c BlankConstraint) WhenGroups(groups ...string) BlankConstraint {
149
	c.groups = groups
150 1
	return c
151
}
152
153
// Code overrides default code for produced violation.
154 1
func (c BlankConstraint) Code(code string) BlankConstraint {
155 1
	c.code = code
156
	return c
157
}
158 1
159
// Message sets the violation message template. You can set custom template parameters
160
// for injecting its values into the final message.
161
func (c BlankConstraint) Message(template string, parameters ...validation.TemplateParameter) BlankConstraint {
162 1
	c.messageTemplate = template
163 1
	c.messageParameters = parameters
164
	return c
165 1
}
166 1
167
func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error {
168
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || !*value {
169 1
		return nil
170 1
	}
171
172
	return c.newViolation(scope)
173 1
}
174
175
func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error {
176
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || *value == "" {
177 1
		return nil
178
	}
179
180
	return c.newViolation(scope)
181
}
182
183
func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
184
	if c.isIgnored || scope.IsIgnored(c.groups...) || count == 0 {
185
		return nil
186
	}
187
188
	return c.newViolation(scope)
189
}
190
191
func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
192
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || value.IsZero() {
193
		return nil
194 1
	}
195
196
	return c.newViolation(scope)
197
}
198
199
func (c BlankConstraint) newViolation(scope validation.Scope) validation.Violation {
200
	return scope.BuildViolation(c.code, c.messageTemplate).
201
		SetParameters(c.messageParameters...).
202 1
		CreateViolation()
203
}
204
205
// NotNilConstraint checks that a value in not strictly equal to nil. To check that values in not blank use
206
// NotBlankConstraint.
207
type NotNilConstraint struct {
208
	isIgnored         bool
209
	groups            []string
210
	code              string
211
	messageTemplate   string
212
	messageParameters validation.TemplateParameterList
213 1
}
214 1
215
// IsNotNil creates a NotNilConstraint to check that a value is not strictly equal to nil.
216
func IsNotNil() NotNilConstraint {
217
	return NotNilConstraint{
218
		code:            code.NotNil,
219 1
		messageTemplate: message.Templates[code.NotNil],
220 1
	}
221
}
222
223
// When enables conditional validation of this constraint. If the expression evaluates to false,
224
// then the constraint will be ignored.
225 1
func (c NotNilConstraint) When(condition bool) NotNilConstraint {
226 1
	c.isIgnored = !condition
227
	return c
228
}
229
230
// WhenGroups enables conditional validation of the constraint by using the validation groups.
231
func (c NotNilConstraint) WhenGroups(groups ...string) NotNilConstraint {
232 1
	c.groups = groups
233 1
	return c
234 1
}
235
236
// Code overrides default code for produced violation.
237
func (c NotNilConstraint) Code(code string) NotNilConstraint {
238 1
	c.code = code
239
	return c
240
}
241
242 1
// Message sets the violation message template. You can set custom template parameters
243 1
// for injecting its values into the final message.
244
func (c NotNilConstraint) Message(template string, parameters ...validation.TemplateParameter) NotNilConstraint {
245
	c.messageTemplate = template
246 1
	c.messageParameters = parameters
247
	return c
248
}
249
250 1
func (c NotNilConstraint) ValidateNil(isNil bool, scope validation.Scope) error {
251 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || !isNil {
252
		return nil
253
	}
254 1
255
	return c.newViolation(scope)
256
}
257
258 1
func (c NotNilConstraint) ValidateBool(value *bool, scope validation.Scope) error {
259 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil {
260
		return nil
261
	}
262 1
263
	return c.newViolation(scope)
264
}
265
266 1
func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error {
267 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil {
268
		return nil
269
	}
270 1
271
	return c.newViolation(scope)
272
}
273
274 1
func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
275 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || value != nil {
276
		return nil
277
	}
278 1
279
	return c.newViolation(scope)
280
}
281
282 1
func (c NotNilConstraint) newViolation(scope validation.Scope) validation.Violation {
283 1
	return scope.BuildViolation(c.code, c.messageTemplate).
284
		SetParameters(c.messageParameters...).
285
		CreateViolation()
286 1
}
287
288
// NilConstraint checks that a value in strictly equal to nil. To check that values in blank use
289
// BlankConstraint.
290 1
type NilConstraint struct {
291 1
	isIgnored         bool
292
	groups            []string
293
	code              string
294 1
	messageTemplate   string
295
	messageParameters validation.TemplateParameterList
296
}
297
298 1
// IsNil creates a NilConstraint to check that a value is strictly equal to nil.
299
func IsNil() NilConstraint {
300
	return NilConstraint{
301
		code:            code.Nil,
302
		messageTemplate: message.Templates[code.Nil],
303
	}
304
}
305
306
// When enables conditional validation of this constraint. If the expression evaluates to false,
307
// then the constraint will be ignored.
308
func (c NilConstraint) When(condition bool) NilConstraint {
309
	c.isIgnored = !condition
310
	return c
311
}
312
313
// WhenGroups enables conditional validation of the constraint by using the validation groups.
314
func (c NilConstraint) WhenGroups(groups ...string) NilConstraint {
315 1
	c.groups = groups
316
	return c
317
}
318
319
// Code overrides default code for produced violation.
320
func (c NilConstraint) Code(code string) NilConstraint {
321
	c.code = code
322
	return c
323 1
}
324
325
// Message sets the violation message template. You can set custom template parameters
326
// for injecting its values into the final message.
327
func (c NilConstraint) Message(template string, parameters ...validation.TemplateParameter) NilConstraint {
328
	c.messageTemplate = template
329
	c.messageParameters = parameters
330
	return c
331
}
332
333
func (c NilConstraint) ValidateNil(isNil bool, scope validation.Scope) error {
334 1
	if c.isIgnored || scope.IsIgnored(c.groups...) || isNil {
335 1
		return nil
336
	}
337
338
	return c.newViolation(scope)
339
}
340 1
341 1
func (c NilConstraint) ValidateBool(value *bool, scope validation.Scope) error {
342
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil {
343
		return nil
344
	}
345
346 1
	return c.newViolation(scope)
347 1
}
348
349
func (c NilConstraint) ValidateString(value *string, scope validation.Scope) error {
350
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil {
351
		return nil
352
	}
353 1
354 1
	return c.newViolation(scope)
355 1
}
356
357
func (c NilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
358
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil {
359
		return nil
360
	}
361
362
	return c.newViolation(scope)
363
}
364
365
func (c NilConstraint) newViolation(scope validation.Scope) validation.Violation {
366
	return scope.BuildViolation(c.code, c.messageTemplate).
367 1
		SetParameters(c.messageParameters...).
368 1
		CreateViolation()
369
}
370
371 1
// BoolConstraint checks that a bool value in strictly equal to expected bool value.
372
type BoolConstraint struct {
373
	isIgnored         bool
374
	expected          bool
375 1
	groups            []string
376 1
	code              string
377
	messageTemplate   string
378
	messageParameters validation.TemplateParameterList
379 1
}
380
381
// IsTrue creates a BoolConstraint to check that a value is not strictly equal to true.
382
func IsTrue() BoolConstraint {
383 1
	return BoolConstraint{
384 1
		expected:        true,
385
		code:            code.True,
386
		messageTemplate: message.Templates[code.True],
387 1
	}
388
}
389
390
// IsFalse creates a BoolConstraint to check that a value is not strictly equal to false.
391 1
func IsFalse() BoolConstraint {
392 1
	return BoolConstraint{
393
		expected:        false,
394
		code:            code.False,
395 1
		messageTemplate: message.Templates[code.False],
396
	}
397
}
398
399 1
// When enables conditional validation of this constraint. If the expression evaluates to false,
400 1
// then the constraint will be ignored.
401
func (c BoolConstraint) When(condition bool) BoolConstraint {
402
	c.isIgnored = !condition
403 1
	return c
404
}
405
406
// WhenGroups enables conditional validation of the constraint by using the validation groups.
407 1
func (c BoolConstraint) WhenGroups(groups ...string) BoolConstraint {
408 1
	c.groups = groups
409
	return c
410
}
411 1
412
// Code overrides default code for produced violation.
413
func (c BoolConstraint) Code(code string) BoolConstraint {
414
	c.code = code
415 1
	return c
416
}
417
418
// Message sets the violation message template. You can set custom template parameters
419
// for injecting its values into the final message.
420
func (c BoolConstraint) Message(template string, parameters ...validation.TemplateParameter) BoolConstraint {
421
	c.messageTemplate = template
422
	c.messageParameters = parameters
423
	return c
424
}
425
426
func (c BoolConstraint) ValidateBool(value *bool, scope validation.Scope) error {
427
	if c.isIgnored || scope.IsIgnored(c.groups...) || value == nil || *value == c.expected {
428
		return nil
429
	}
430
431
	return scope.BuildViolation(c.code, c.messageTemplate).
432 1
		SetParameters(c.messageParameters...).
433
		CreateViolation()
434
}
435