@@ -526,13 +526,23 @@ end
526
526
--- => error('arg1: expected even number, got 3')
527
527
--- </pre>
528
528
---
529
-
---@param opt Map of parameter names to validations. Each key is a parameter
529
+
--- If multiple types are valid they can be given as a list.
530
+
--- <pre>
531
+
--- vim.validate{arg1={{'foo'}, {'table', 'string'}}, arg2={'foo', {'table', 'string'}}}
532
+
--- => NOP (success)
533
+
---
534
+
--- vim.validate{arg1={1, {'string', table'}}}
535
+
--- => error('arg1: expected string|table, got number')
536
+
---
537
+
--- </pre>
538
+
---
539
+
---@param opt table of parameter names to validations. Each key is a parameter
530
540
--- name; each value is a tuple in one of these forms:
531
541
--- 1. (arg_value, type_name, optional)
532
542
--- - arg_value: argument value
533
-
--- - type_name: string type name, one of: ("table", "t", "string",
543
+
--- - type_name: string|table type name, one of: ("table", "t", "string",
534
544
--- "s", "number", "n", "boolean", "b", "function", "f", "nil",
535
-
--- "thread", "userdata")
545
+
--- "thread", "userdata") or list of them.
536
546
--- - optional: (optional) boolean, if true, `nil` is valid
537
547
--- 2. (arg_value, fn, msg)
538
548
--- - arg_value: argument value
571
581
end
572
582
573
583
local val = spec[1] -- Argument value.
574
-
local t = spec[2] -- Type name, or callable.
584
+
local types = spec[2] -- Type name, or callable.
575
585
local optional = (true == spec[3])
576
586
577
-
if type(t) == 'string' then
578
-
local t_name = type_names[t]
579
-
if not t_name then
580
-
return false, string.format('invalid type name: %s', t)
581
-
end
587
+
if type(types) == 'string' then
588
+
types = {types}
589
+
end
582
590
583
-
if (not optional or val ~= nil) and not _is_type(val, t_name) then
584
-
return false, string.format("%s: expected %s, got %s", param_name, t_name, type(val))
585
-
end
586
-
elseif vim.is_callable(t) then
591
+
if vim.is_callable(types) then
587
592
-- Check user-provided validation function.
588
-
local valid, optional_message = t(val)
593
+
local valid, optional_message = types(val)
589
594
if not valid then
590
595
local error_message = string.format("%s: expected %s, got %s", param_name, (spec[3] or '?'), val)
591
596
if optional_message ~= nil then
594
599
595
600
return false, error_message
596
601
end
602
+
elseif type(types) == 'table' then
603
+
local success = false
604
+
for i, t in ipairs(types) do
605
+
local t_name = type_names[t]
606
+
if not t_name then
607
+
return false, string.format('invalid type name: %s', t)
608
+
end
609
+
types[i] = t_name
610
+
611
+
if (optional and val == nil) or _is_type(val, t_name) then
612
+
success = true
613
+
break
614
+
end
615
+
end
616
+
if not success then
617
+
return false, string.format("%s: expected %s, got %s", param_name, table.concat(types, '|'), type(val))
618
+
end
597
619
else
598
-
return false, string.format("invalid type name: %s", tostring(t))
620
+
return false, string.format("invalid type name: %s", tostring(types))
599
621
end
600
622
end
601
623
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4