-
Notifications
You must be signed in to change notification settings - Fork 4
/
validate.r
367 lines (325 loc) · 8.32 KB
/
validate.r
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
Rebol [
Title: "Validate"
Author: "Christopher Ross-Gill"
Date: 26-Apr-2007
File: %validate.r
Version: 0.6.0
Purpose: {Validates user input data}
Rights: http://opensource.org/licenses/Apache-2.0
Type: 'module
Name: 'rgchris.validate
Exports: [validate]
History: [
23-Apr-2014 0.6.0 "Rearrange source to be more modular"
26-Apr-2007 0.5.0 "Original Version"
]
Notes: {Extracted from the QuarterMaster web framework}
]
do %as.r
envelop: func [val [any-type!]][
either any-block? val [val][reduce [val]]
]
wrap: func [body [block!]][
use collect [
parse body [
any [body: set-word! (keep to word! body/1) | skip]
]
] head body
]
add-to: func [series key val][
key: envelop key
map key func [key][as word! key]
if find key none! [return none]
until [
series: any [
find/tail series key/1
insert tail series key/1
]
key: next key
switch type?/word series/1 [
none! [unless tail? key [insert/only series series: copy []]]
string! [change/only series series: envelop series/1]
block! [series: series/1]
]
if tail? key [append series val]
]
]
get-from: func [series 'key][
key: copy envelop key
while [all [not tail? key any-block? series]][
series: select series take key
]
all [tail? key series]
]
validate: wrap [
interpolate: func [body [string!] escapes [any-block!] /local out][
body: out: copy body
parse/all body [
any [
to #"%" body: (
body: change/part body reduce any [
select/case escapes body/2 body/2
] 2
) :body
]
]
out
]
filter: [
result: errors: #[none]
messages: [
not-included "is not included in the list"
excluded "is reserved"
invalid "is missing or invalid"
not-confirmed "doesn't match confirmation"
not-accepted "must be accepted"
empty "can't be empty"
blank "can't be blank"
too-long "is too long (maximum is %d characters)"
too-short "is too short (minimum is %d characters)"
wrong-length "is the wrong length (should be %d characters)"
not-a-number "is not a number"
too-many "has too many arguments"
]
datatype: [
'any-string! | 'binary! | 'block! | 'char! | 'date! | 'decimal! | 'email! | 'file! |
'get-word! | 'integer! | 'issue! | 'lit-path! | 'lit-word! | 'logic! | 'money! |
'none! | 'number! | 'pair! | 'paren! | 'path! | 'range! | 'refinement! |
'set-path! | 'set-word! | 'string! | 'tag! | 'time! | 'tuple! | 'url! | 'word!
]
else: #[none]
otherwise: [
['else | 'or][
set else string! | copy else any [word! string!]
] | (else: #[none])
]
source: spec: rule: key: value: required: present: target: type: format: constraints: else: none
constraint: use [is is-not? is-or-length-is op val val-type range group][
op: val: val-type: none
is: ['is | 'are]
is-or-length-is: [
[
['length | 'size] (val: string-length? form value val-type: integer!)
| (val: :value val-type: :type)
] is
]
is-not?: ['not (op: false) | (op: true)]
[
is [
'accepted otherwise (
unless true = value [report not-accepted]
) |
'confirmed opt 'by set val get-word! otherwise (
val: to word! val
unless value = as/where :type get-from source :val format [
report not-confirmed
]
) |
is-not? 'within set group [any-block! | get-word!] otherwise (
if get-word? group [
unless all [
function? group: get :group
block? group: group
][
group: []
]
]
either case [
block? value [value = intersect value group]
true [found? find group value]
][
unless op [report excluded]
][
if op [report not-included]
]
)
] |
is-or-length-is [
is-not? 'between [set range [range! | into [2 val-type]]] otherwise (
either op [
case [
val < target: range/1 [report too-short]
val > target: range/2 [report too-long]
]
][
unless any [
val < range/1
val > range/2
][report excluded]
]
) |
'more-than set target val-type otherwise (
unless val > target [report too-short]
) |
'less-than set target val-type otherwise (
unless val < target [report too-long]
) |
set target val-type otherwise (
unless val = target [report wrong-length]
)
]
]
]
do-constraints: does [constraints: [any constraint]]
skip-constraints: does [constraints: [to set-word! | to end]]
valid?: func [value][any [value find type 'none!]]
humanize: func [key /local initial][
initial: true
if parse form key amend [
copy key any [
key: alpha (if initial [uppercase/part key 1] initial: false)
| ["-" | "_" | " "] (change key " " initial: true)
| skip (initial: false)
]
][
rejoin ["'" key "'"]
]
]
report: func ['message [word!]][
message: any [
all [string? else else]
all [block? else select else message]
reform [humanize key any [select messages message ""]]
]
unless select errors :key [repend errors [:key copy []]]
append select errors :key interpolate message [
#"w" [form key]
#"W" [humanize key]
#"d" [form target]
#"t" [form type]
]
]
engage: does [parse spec rule]
]
make-filter: func [source spec rule][
spec: context compose/deep [
(filter)
errors: copy []
result: copy []
rule: [(copy/deep rule)]
spec: [(spec)]
]
spec/source: copy source
spec
]
validate: func [
"Validates user input" [catch]
source [any-type!] {User input: [word! any-type! ...]}
spec [block!] {Validation Rules}
/block "Implied keys determined by order"
/loose "Ignores constraints"
/report-to "Recover exceptions"
errs [block!] "Block to hold exceptions"
][
unless block? source [return none]
spec: make-filter source compose/deep/only spec either loose [
[
(skip-constraints)
any [
set key set-word! (key: to word! key)
opt 'opt
[set type datatype (type: get type)
| set type ['dialect | 'match | 'object!] (type: :block!)]
set format opt [block! | get-word!]
otherwise
(
value: get-from source :key
present: not any [
none? value
empty? trim form value
]
if all [
present
not none? value: case [
:type = block! [value]
:type = logic! [as logic! value]
value [form value]
]
][
repend result [key value]
]
)
constraints
]
]
][
[
any [
set key set-word! (key: to word! key)
set required opt 'opt (required: required <> 'opt)
[
set type datatype (type: get type)
| set type ['dialect | 'match | 'object!]
]
set format opt [block! | get-word!]
otherwise
(
value: either block [
pick source 1
][
get-from source :key
]
either all [
present: not any [
all [
none? value
not find reduce [
logic! block! 'object!
] :type
]
empty? trim form value
]
not none? value: case [
find [dialect match] :type [
use [values][
all [
block? format
values: attempt [to block! value]
switch type [
dialect [parse values format]
match [match values format]
]
value
]
]
]
:type = 'object! [
value: envelop value
either block? format [
validate value format
][value]
]
:type = block! [
value: envelop value
either block? format [
all [parse value format value]
][value]
]
:type = logic! [
either as logic! value [true][false]
]
true [as/where :type value format]
]
][
do-constraints
if block [source: next source]
repend result [key value]
][
skip-constraints
case [
all [present not block] [report invalid]
required [report blank]
not required [repend result [key none]]
]
]
)
constraints
]
end (if all [block not tail? source empty? errors][key: 'validate report too-many])
]
]
unless spec/engage [do make error! "Could not parse Validate specification"]
all [block? errs insert clear errs spec/errors]
if empty? spec/errors [spec/result]
]
]