-
Notifications
You must be signed in to change notification settings - Fork 14
/
validation.go
332 lines (274 loc) · 6.59 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package ar
type Rule interface {
Rule() *Validation
}
func MakeRule() *Validation {
v := &Validation{}
return v.OnSave()
}
func CustomRule(c CustomValidation) *Validation {
v := &Validation{}
return v.CustomRule(c).OnSave()
}
type CustomValidation func(errors *Errors)
type Validation struct {
presence *presence
length *length
format *format
numericality *numericality
inclusion []string
exclusion []string
custom CustomValidation
onCreate bool
onUpdate bool
}
func (v *Validation) Rule() *Validation {
return v
}
func (v *Validation) CustomRule(c CustomValidation) *Validation {
v.custom = c
return v
}
func (v *Validation) Presence() *presence {
if v.presence == nil {
v.presence = newPresence(v)
}
return v.presence
}
func (v *Validation) Length() *length {
if v.length == nil {
v.length = &length{Validation: v}
}
return v.length
}
func (v *Validation) Format() *format {
if v.format == nil {
v.format = &format{Validation: v}
}
return v.format
}
func (v *Validation) Numericality() *numericality {
if v.numericality == nil {
v.numericality = newNumericality(v)
}
return v.numericality
}
func (v *Validation) Inclusion(collection ...string) *Validation {
inclusion := []string{}
v.inclusion = append(inclusion, collection...)
return v
}
func (v *Validation) Exclusion(collection ...string) *Validation {
exclusion := []string{}
v.exclusion = append(exclusion, collection...)
return v
}
func (v *Validation) OnCreate() *Validation {
v.onCreate = true
v.onUpdate = false
return v
}
func (v *Validation) OnUpdate() *Validation {
v.onCreate = false
v.onUpdate = true
return v
}
func (v *Validation) OnSave() *Validation {
v.onCreate = true
v.onUpdate = true
return v
}
type presence struct {
*Validation
presence bool
message string
}
func newPresence(v *Validation) *presence {
return &presence{
Validation: v,
presence: true,
message: "can't be blank",
}
}
func (p *presence) Rule() *Validation {
return p.Validation
}
func (p *presence) Message(message string) *presence {
p.message = message
return p
}
type length struct {
*Validation
minimum *lengthNumber
maximum *lengthNumber
is *lengthNumber
}
type lengthNumber struct {
*length
number int
message string
}
func (l *lengthNumber) Rule() *Validation {
return l.length.Validation
}
func (l *lengthNumber) Message(message string) *lengthNumber {
l.message = message
return l
}
func (l *length) newLengthNumber(num int, message string) *lengthNumber {
return &lengthNumber{
length: l,
number: num,
message: message,
}
}
func (l *length) Rule() *Validation {
return l.Validation
}
func (l *length) Minimum(minimum int) *lengthNumber {
l.minimum = l.newLengthNumber(minimum, "is too short (minimum is %d characters)")
return l.minimum
}
func (l *length) Maximum(maximum int) *lengthNumber {
l.maximum = l.newLengthNumber(maximum, "is too long (maximum is %d characters)")
return l.maximum
}
func (l *length) Is(is int) *lengthNumber {
l.is = l.newLengthNumber(is, "is the wrong length (should be %d characters)")
return l.is
}
func (l *length) In(from, to int) *lengthNumber {
return l.Minimum(from).Maximum(to)
}
func (l *length) WithIn(from, to int) *lengthNumber {
return l.In(from, to)
}
type format struct {
*Validation
with *with
}
type with struct {
*format
regexp string
message string
}
func (f *format) Rule() *Validation {
return f.Validation
}
func (f *format) With(regexp string) *with {
f.with = &with{
format: f,
regexp: regexp,
message: "is invalid",
}
return f.with
}
func (w *with) Rule() *Validation {
return w.format.Validation
}
func (w *with) Message(message string) *with {
w.message = message
return w
}
type numericality struct {
*Validation
numericality bool
message string
onlyInteger *numericalityBool
greaterThan *numericalityNumber
greaterThanOrEqualTo *numericalityNumber
equalTo *numericalityNumber
lessThan *numericalityNumber
lessThanOrEqualTo *numericalityNumber
odd *numericalityBool
even *numericalityBool
}
func (n *numericality) newNumericalityBool(message string) *numericalityBool {
return &numericalityBool{
numericality: n,
bool: true,
message: message,
}
}
func (n *numericality) newNumericalityNumber(num int, message string) *numericalityNumber {
return &numericalityNumber{
numericality: n,
number: num,
message: message,
}
}
type numericalityBool struct {
*numericality
bool
message string
}
func (n *numericalityBool) Rule() *Validation {
return n.numericality.Validation
}
func (n *numericalityBool) Message(message string) *numericalityBool {
n.message = message
return n
}
type numericalityNumber struct {
*numericality
number int
message string
}
func (n *numericalityNumber) Rule() *Validation {
return n.numericality.Validation
}
func (n *numericalityNumber) Message(message string) *numericalityNumber {
n.message = message
return n
}
func newNumericality(v *Validation) *numericality {
return &numericality{
Validation: v,
numericality: true,
message: "is not a number",
}
}
func (n *numericality) Rule() *Validation {
return n.Validation
}
func (n *numericality) OnlyInteger() *numericalityBool {
n.onlyInteger = n.newNumericalityBool("must be an integer")
return n.onlyInteger
}
func (n *numericality) GreaterThan(num int) *numericalityNumber {
n.greaterThan = n.newNumericalityNumber(num, "must be greater than %d")
return n.greaterThan
}
func (n *numericality) GreaterThanOrEqualTo(num int) *numericalityNumber {
n.greaterThanOrEqualTo = n.newNumericalityNumber(num, "must be greater than or equal to %d")
return n.greaterThanOrEqualTo
}
func (n *numericality) EqualTo(num int) *numericalityNumber {
n.equalTo = n.newNumericalityNumber(num, "must be equal to %d")
return n.equalTo
}
func (n *numericality) LessThan(num int) *numericalityNumber {
n.lessThan = n.newNumericalityNumber(num, "must be less than %d")
return n.lessThan
}
func (n *numericality) LessThanOrEqualTo(num int) *numericalityNumber {
n.lessThanOrEqualTo = n.newNumericalityNumber(num, "must be less than or equal to %d")
return n.lessThanOrEqualTo
}
func (n *numericality) Odd() *numericalityBool {
n.odd = n.newNumericalityBool("must be odd")
return n.odd
}
func (n *numericality) Even() *numericalityBool {
n.even = n.newNumericalityBool("must be even")
return n.even
}
type inclusion struct {
*Validation
in []string
}
func (i *inclusion) In(collection ...string) *inclusion {
i.in = []string{}
i.in = append(i.in, collection...)
return i
}