Passed
Pull Request — main (#23)
by Igor
01:32
created

it.CountConstraint.MaxMessage   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
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
nop 1
1
package it
2
3
import (
4
	"strconv"
5
6
	"github.com/muonsoft/validation"
7
	"github.com/muonsoft/validation/code"
8
	"github.com/muonsoft/validation/generic"
9
	"github.com/muonsoft/validation/message"
10
)
11
12
// CountConstraint checks that a given collection's (array, slice or a map) length is between some minimum and
13
// maximum value.
14
type CountConstraint struct {
15
	isIgnored            bool
16
	checkMin             bool
17
	checkMax             bool
18
	min                  int
19
	max                  int
20
	minMessageTemplate   string
21
	maxMessageTemplate   string
22
	exactMessageTemplate string
23
}
24
25
func newCountConstraint(min int, max int, checkMin bool, checkMax bool) CountConstraint {
26 1
	return CountConstraint{
27
		min:                  min,
28
		max:                  max,
29
		checkMin:             checkMin,
30
		checkMax:             checkMax,
31
		minMessageTemplate:   message.CountTooFew,
32
		maxMessageTemplate:   message.CountTooMany,
33
		exactMessageTemplate: message.CountExact,
34
	}
35
}
36
37
func HasMinCount(min int) CountConstraint {
38 1
	return newCountConstraint(min, 0, true, false)
39
}
40
41
func HasMaxCount(max int) CountConstraint {
42 1
	return newCountConstraint(0, max, false, true)
43
}
44
45
func HasCountBetween(min int, max int) CountConstraint {
46 1
	return newCountConstraint(min, max, true, true)
47
}
48
49
func HasExactCount(count int) CountConstraint {
50 1
	return newCountConstraint(count, count, true, true)
51
}
52
53
func (c CountConstraint) When(condition bool) CountConstraint {
54 1
	c.isIgnored = !condition
55 1
	return c
56
}
57
58
func (c CountConstraint) MinMessage(message string) CountConstraint {
59 1
	c.minMessageTemplate = message
60 1
	return c
61
}
62
63
func (c CountConstraint) MaxMessage(message string) CountConstraint {
64 1
	c.maxMessageTemplate = message
65 1
	return c
66
}
67
68
func (c CountConstraint) ExactMessage(message string) CountConstraint {
69 1
	c.exactMessageTemplate = message
70 1
	return c
71
}
72
73
func (c CountConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
74 1
	return c.ValidateCountable(value.Count(), scope)
75
}
76
77
func (c CountConstraint) ValidateCountable(count int, scope validation.Scope) error {
78 1
	if c.isIgnored {
79 1
		return nil
80
	}
81 1
	if c.checkMax && count > c.max {
82 1
		return c.newViolation(count, c.max, code.CountTooMany, c.maxMessageTemplate, scope)
83
	}
84 1
	if c.checkMin && count < c.min {
85 1
		return c.newViolation(count, c.min, code.CountTooFew, c.minMessageTemplate, scope)
86
	}
87
88 1
	return nil
89
}
90
91
func (c CountConstraint) SetUp(scope *validation.Scope) error {
92 1
	return nil
93
}
94
95
func (c CountConstraint) Name() string {
96
	return "CountConstraint"
97
}
98
99
func (c CountConstraint) newViolation(
100
	count, limit int,
101
	violationCode, message string,
102
	scope validation.Scope,
103
) validation.Violation {
104 1
	if c.checkMin && c.checkMax && c.min == c.max {
105 1
		message = c.exactMessageTemplate
106 1
		violationCode = code.CountExact
107
	}
108
109 1
	return scope.BuildViolation(violationCode, message).
110
		SetPluralCount(limit).
111
		SetParameters(map[string]string{
112
			"{{ count }}": strconv.Itoa(count),
113
			"{{ limit }}": strconv.Itoa(limit),
114
		}).
115
		GetViolation()
116
}
117