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