Passed
Pull Request — main (#28)
by Rushan
01:44
created

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