1
|
|
|
package validation |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"github.com/muonsoft/validation/code" |
5
|
|
|
"github.com/muonsoft/validation/generic" |
6
|
|
|
"github.com/muonsoft/validation/message" |
7
|
|
|
|
8
|
|
|
"time" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
// Constraint is the base interface to build validation constraints. |
12
|
|
|
type Constraint interface { |
13
|
|
|
Option |
14
|
|
|
// Name is a constraint name that can be used in internal errors. |
15
|
|
|
Name() string |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// NilConstraint is used for constraints that need to check value for nil. In common case |
19
|
|
|
// you do not need to implement it in your constraints because nil values should be ignored. |
20
|
|
|
type NilConstraint interface { |
21
|
|
|
Constraint |
22
|
|
|
ValidateNil(scope Scope) error |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// BoolConstraint is used to build constraints for boolean values validation. |
26
|
|
|
type BoolConstraint interface { |
27
|
|
|
Constraint |
28
|
|
|
ValidateBool(value *bool, scope Scope) error |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// NumberConstraint is used to build constraints for numeric values validation. |
32
|
|
|
// |
33
|
|
|
// At this moment working with numbers is based on reflection. |
34
|
|
|
// Be aware. This constraint is subject to be changed after generics implementation in Go. |
35
|
|
|
type NumberConstraint interface { |
36
|
|
|
Constraint |
37
|
|
|
ValidateNumber(value generic.Number, scope Scope) error |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// StringConstraint is used to build constraints for string values validation. |
41
|
|
|
type StringConstraint interface { |
42
|
|
|
Constraint |
43
|
|
|
ValidateString(value *string, scope Scope) error |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// IterableConstraint is used to build constraints for validation of iterables (arrays, slices, or maps). |
47
|
|
|
// |
48
|
|
|
// At this moment working with numbers is based on reflection. |
49
|
|
|
// Be aware. This constraint is subject to be changed after generics implementation in Go. |
50
|
|
|
type IterableConstraint interface { |
51
|
|
|
Constraint |
52
|
|
|
ValidateIterable(value generic.Iterable, scope Scope) error |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// CountableConstraint is used to build constraints for simpler validation of iterable elements count. |
56
|
|
|
type CountableConstraint interface { |
57
|
|
|
Constraint |
58
|
|
|
ValidateCountable(count int, scope Scope) error |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// TimeConstraint is used to build constraints for date/time validation. |
62
|
|
|
type TimeConstraint interface { |
63
|
|
|
Constraint |
64
|
|
|
ValidateTime(value *time.Time, scope Scope) error |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// CustomStringConstraint can be used to create custom constraints for validating string values |
68
|
|
|
// based on function with signature func(string) bool. |
69
|
|
|
type CustomStringConstraint struct { |
70
|
|
|
isIgnored bool |
71
|
|
|
isValid func(string) bool |
72
|
|
|
name string |
73
|
|
|
code string |
74
|
|
|
messageTemplate string |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// NewCustomStringConstraint creates a new string constraint from a function with signature func(string) bool. |
78
|
|
|
// Optional parameters can be used to set up constraint name (first parameter), violation code (second), |
79
|
|
|
// message template (third). All other parameters are ignored. |
80
|
|
|
func NewCustomStringConstraint(isValid func(string) bool, parameters ...string) CustomStringConstraint { |
81
|
1 |
|
constraint := CustomStringConstraint{ |
82
|
|
|
isValid: isValid, |
83
|
|
|
name: "CustomStringConstraint", |
84
|
|
|
code: code.NotValid, |
85
|
|
|
messageTemplate: message.NotValid, |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
if len(parameters) > 0 { |
89
|
1 |
|
constraint.name = parameters[0] |
90
|
|
|
} |
91
|
1 |
|
if len(parameters) > 1 { |
92
|
1 |
|
constraint.code = parameters[1] |
93
|
|
|
} |
94
|
1 |
|
if len(parameters) > 2 { |
95
|
1 |
|
constraint.messageTemplate = parameters[2] |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return constraint |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// SetUp always returns no error. |
102
|
|
|
func (c CustomStringConstraint) SetUp() error { |
103
|
1 |
|
return nil |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Name is the constraint name. It can be set via first parameter of function NewCustomStringConstraint. |
107
|
|
|
func (c CustomStringConstraint) Name() string { |
108
|
1 |
|
return c.name |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Message sets the violation message template. You can use template parameters |
112
|
|
|
// for injecting its values into the final message: |
113
|
|
|
// |
114
|
|
|
// {{ value }} - the current (invalid) value. |
115
|
|
|
func (c CustomStringConstraint) Message(message string) CustomStringConstraint { |
116
|
1 |
|
c.messageTemplate = message |
117
|
1 |
|
return c |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
121
|
|
|
// then the constraint will be ignored. |
122
|
|
|
func (c CustomStringConstraint) When(condition bool) CustomStringConstraint { |
123
|
1 |
|
c.isIgnored = !condition |
124
|
1 |
|
return c |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
func (c CustomStringConstraint) ValidateString(value *string, scope Scope) error { |
128
|
1 |
|
if c.isIgnored || value == nil || *value == "" || c.isValid(*value) { |
129
|
1 |
|
return nil |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return scope.BuildViolation(c.code, c.messageTemplate). |
133
|
|
|
AddParameter("{{ value }}", *value). |
134
|
|
|
CreateViolation() |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// ConditionalConstraint is used for conditional validation. |
138
|
|
|
// Use the When function to initiate a conditional check. |
139
|
|
|
// If the condition is true, then the constraints passed through the Then function will be applied. |
140
|
|
|
// Otherwise, the constraints passed through the Else function will apply. |
141
|
|
|
type ConditionalConstraint struct { |
142
|
|
|
condition bool |
143
|
|
|
thenConstraints []Constraint |
144
|
|
|
elseConstraints []Constraint |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// When function is used to initiate conditional validation. |
148
|
|
|
// If the condition is true, then the constraints passed through the Then function will be applied. |
149
|
|
|
// Otherwise, the constraints passed through the Else function will apply. |
150
|
|
|
func When(condition bool) ConditionalConstraint { |
151
|
1 |
|
return ConditionalConstraint{ |
152
|
|
|
condition: condition, |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Then function is used to set a sequence of constraints to be applied if the condition is true. |
157
|
|
|
// If the list is empty error will be returned. |
158
|
|
|
func (c ConditionalConstraint) Then(constraints ...Constraint) ConditionalConstraint { |
159
|
1 |
|
c.thenConstraints = constraints |
160
|
1 |
|
return c |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Else function is used to set a sequence of constraints to be applied if a condition is false. |
164
|
|
|
func (c ConditionalConstraint) Else(constraints ...Constraint) ConditionalConstraint { |
165
|
1 |
|
c.elseConstraints = constraints |
166
|
1 |
|
return c |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Name is the constraint name. |
170
|
|
|
func (c ConditionalConstraint) Name() string { |
171
|
1 |
|
return "ConditionalConstraint" |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// SetUp will return an error if Then function did not set any constraints. |
175
|
|
|
func (c ConditionalConstraint) SetUp() error { |
176
|
1 |
|
if len(c.thenConstraints) == 0 { |
177
|
1 |
|
return errThenBranchNotSet |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
return nil |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
func (c *ConditionalConstraint) validateConditionConstraints( |
184
|
|
|
scope Scope, |
185
|
|
|
validate ValidateByConstraintFunc, |
186
|
|
|
) (ViolationList, error) { |
187
|
1 |
|
violations := make(ViolationList, 0) |
188
|
1 |
|
var constraints []Constraint |
189
|
|
|
|
190
|
1 |
|
if c.condition { |
191
|
1 |
|
constraints = c.thenConstraints |
192
|
|
|
} else { |
193
|
1 |
|
constraints = c.elseConstraints |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
for _, constraint := range constraints { |
197
|
1 |
|
err := violations.AppendFromError(validate(constraint, scope)) |
198
|
1 |
|
if err != nil { |
199
|
|
|
return nil, err |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
1 |
|
return violations, nil |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
type notFoundConstraint struct { |
207
|
|
|
key string |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
func (c notFoundConstraint) SetUp() error { |
211
|
1 |
|
return ConstraintNotFoundError{Key: c.key} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
func (c notFoundConstraint) Name() string { |
215
|
1 |
|
return "notFoundConstraint" |
216
|
|
|
} |
217
|
|
|
|