-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid.lua
More file actions
503 lines (410 loc) · 14.3 KB
/
valid.lua
File metadata and controls
503 lines (410 loc) · 14.3 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
--[[
Copyright (c) 2024, Ben Wilber
https://github.com/benwilber/lua-valid
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]
local _M = {}
-- Local reference optimizations for non-JIT'd Lua versions.
local type = type
local pairs = pairs
local ipairs = ipairs
local string_match = string.match
local string_lower = string.lower
local math_huge = math.huge
-- The default validation function that's called
-- when no custom function is provided.
local function defaultfunc(val)
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return true, val, nil, nil
end
-- Helper that calls a custom validation function and
-- and ensures that meaningful values are returned.
local function callfunc(func, val)
local is_valid, val_or_err, badval_or_nil, path_or_nil = func(val)
-- checking for nil (not false)
if val_or_err == nil then
if is_valid then
val_or_err = val
badval_or_nil = nil
else
val_or_err = "func"
if badval_or_nil == nil then
badval_or_nil = val
end
end
end
return is_valid, val_or_err, badval_or_nil, path_or_nil
end
-- Validates that a value string_matches a specific literal.
local function literal(lit, opts)
opts = opts or {}
local icase = opts.icase or false
local func = opts.func or defaultfunc
return function(val)
if icase and type(lit) == "string" and type(val) == "string" then
if string_lower(val) ~= string_lower(lit) then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "literal", val, nil
end
elseif val ~= lit then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "literal", val, nil
end
return callfunc(func, val)
end
end
_M.literal = literal
-- Validates that a value is a number within an optional range.
local function number(opts)
opts = opts or {}
local min = opts.min or -math_huge
local max = opts.max or math_huge
local func = opts.func or defaultfunc
return function(val)
if type(val) ~= "number" then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "number", val, nil
end
if val < min then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "min", val, nil
end
if val > max then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "max", val, nil
end
return callfunc(func, val)
end
end
_M.number = number
-- Validates that a value is a string with optional length and pattern constraints.
-- (Don't shadow Lua's "string")
local function _string(opts)
opts = opts or {}
local minlen = opts.minlen or 0
local maxlen = opts.maxlen or math_huge
local pattern = opts.pattern
local func = opts.func or defaultfunc
return function(val)
if type(val) ~= "string" then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "string", val, nil
end
local len = #val
if len < minlen then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "minlen", val, nil
end
if len > maxlen then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "maxlen", val, nil
end
if pattern and not string_match(val, pattern) then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "pattern", val, nil
end
return callfunc(func, val)
end
end
_M.string = _string
-- Validates that a value is a table, with optional constraints for arrays and maps.
-- (Don't shadow Lua's "table")
local function table_(opts)
opts = opts or {}
local array = false
local map = false
local empty = false
local required = opts.required or {}
local func = opts.func or defaultfunc
local tabledef = opts.table
if opts.array then
array = true
end
if opts.map then
map = true
end
if opts.empty then
empty = true
end
return function(val)
if type(val) ~= "table" then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "table", val, nil
end
if array and #val == 0 and not empty then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "empty", val, nil
end
-- checking for nil (not false)
if map and next(val) == nil and not empty then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "empty", val, nil
end
if required ~= "all" then
-- specific required fields
for _, key in ipairs(required) do
-- checking for nil (not false)
if val[key] == nil then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "required", key, {key}
end
end
end
local is_valid
local val_or_err
local badval_or_nil
local path_or_nil
-- the value at key (or index) in the table.
local v
for key_or_idx, func_or_lit in pairs(tabledef) do
if type(func_or_lit) ~= "function" then
func_or_lit = literal(func_or_lit)
end
v = val[key_or_idx]
-- checking for nil (not false)
if v == nil and required == "all" then
-- every field is required
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "required", key_or_idx, {key_or_idx}
end
-- we already validated the required fields
-- checking for nil (not false)
if v ~= nil then
is_valid, val_or_err, badval_or_nil, path_or_nil = func_or_lit(v)
if not is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, val_or_err, badval_or_nil, {key_or_idx, path_or_nil}
end
end
end
return callfunc(func, val)
end
end
_M.table = table_
-- A shorthand for valid.table with opts.array set to true.
local function array(opts)
return table_ {
array = true,
empty = opts.empty,
required = opts.required,
func = opts.func,
table = opts.table
}
end
_M.array = array
-- Validates that a value is an array where each element string_matches a given definition.
local function arrayof(deffunc, opts)
opts = opts or {}
local empty = false
local unique = false
local minlen = opts.minlen or 0
local maxlen = opts.maxlen or math_huge
local func = opts.func or defaultfunc
if opts.empty then
empty = true
-- minlen doesnt matter if empty = true
minlen = 0
end
if minlen == 0 and not empty then
-- empty doesnt matter if minlen = 0
empty = true
end
if opts.unique then
unique = true
end
return function(val)
if type(val) ~= "table" then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "table", val, nil
end
local len = #val
if len == 0 and not empty then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "empty", val, nil
end
if len < minlen then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "minlen", val, nil
end
if len > maxlen then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "maxlen", val, nil
end
local is_valid
local val_or_err
local badval_or_nil
local path_or_nil
local set = {}
local prev_i
for i, v in ipairs(val) do
is_valid, val_or_err, badval_or_nil, path_or_nil = deffunc(v)
if not is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, val_or_err, badval_or_nil, {i, path_or_nil}
end
is_valid, val_or_err, badval_or_nil, path_or_nil = callfunc(func, v)
if not is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, val_or_err, badval_or_nil, {i, path_or_nil}
end
if unique then
prev_i = set[v]
-- checking for nil (not false)
if prev_i ~= nil then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "unique", v, {prev_i, i}
else
set[v] = i
end
end
end
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return true, val, nil, nil
end
end
_M.arrayof = arrayof
-- A shorthand for valid.table with opts.map set to true.
local function map(opts)
return table_ {
map = true,
empty = opts.empty,
required = opts.required,
func = opts.func,
table = opts.table
}
end
_M.map = map
-- Validates maps with specific type definitions for both keys and values.
local function mapof(deffuncs, opts)
opts = opts or {}
local empty = false
local keydeffunc = deffuncs[1] or defaultfunc
local valdeffunc = deffuncs[2] or defaultfunc
local keyfunc = defaultfunc
local valfunc = defaultfunc
if type(opts.func) == "table" then
keyfunc = opts.func[1] or defaultfunc
valfunc = opts.func[2] or defaultfunc
end
if opts.empty then
empty = true
end
return function(val)
if type(val) ~= "table" then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "table", val, nil
end
-- checking for nil (not false)
if next(val) == nil and not empty then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "empty", val, nil
end
local is_valid
local val_or_err
local badval_or_nil
local path_or_nil
for k, v in pairs(val) do
is_valid, val_or_err, badval_or_nil, path_or_nil = keydeffunc(k)
if not is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, val_or_err, badval_or_nil, {k, path_or_nil}
end
is_valid, val_or_err, badval_or_nil, path_or_nil = keyfunc(k)
if not is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, val_or_err, badval_or_nil, {k, path_or_nil}
end
is_valid, val_or_err, badval_or_nil, path_or_nil = valdeffunc(v)
if not is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, val_or_err, badval_or_nil, {k, v, path_or_nil}
end
is_valid, val_or_err, badval_or_nil, path_or_nil = valfunc(v)
if not is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, val_or_err, badval_or_nil, {k, v, path_or_nil}
end
end
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return true, val, nil, nil
end
end
_M.mapof = mapof
-- Validates that a value is a function.
local function func()
return function(val)
if type(val) ~= "function" then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "func", val, nil
end
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return true, val, nil, nil
end
end
_M.func = func
-- Validates that a value satisfies at least one of the given validation functions.
local function anyof(funcs_or_lits)
return function(val)
local is_valid
local val_or_err
local badval_or_nil
local path_or_nil
local errtabs = {}
for _, func_or_lit in ipairs(funcs_or_lits) do
if type(func_or_lit) ~= "function" then
func_or_lit = literal(func_or_lit)
end
is_valid, val_or_err, badval_or_nil, path_or_nil = func_or_lit(val)
if is_valid then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return true, val, nil, nil
else
errtabs[#errtabs + 1] = {val_or_err, badval_or_nil, path_or_nil}
end
end
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "any", val, errtabs
end
end
_M.anyof = anyof
-- Validates that a value satisfies all of the given validation functions.
local function allof(funcs_or_lits)
return function(val)
local is_valid
local val_or_err
local badval_or_nil
local path_or_nil
local errtabs = {}
for _, func_or_lit in ipairs(funcs_or_lits) do
if type(func_or_lit) ~= "function" then
func_or_lit = literal(func_or_lit)
end
is_valid, val_or_err, badval_or_nil, path_or_nil = func_or_lit(val)
if not is_valid then
errtabs[#errtabs + 1] = {val_or_err, badval_or_nil, path_or_nil}
end
end
if #errtabs == 0 then
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return true, val, nil, nil
else
-- is_valid, val_or_err, badval_or_nil, path_or_nil
return false, "all", val, errtabs
end
end
end
_M.allof = allof
-- Validates that a value is a literal boolean either true or false.
local function boolean()
return anyof {true, false}
end
_M.boolean = boolean
return _M