Add function/callable type validator to lua_shape core.
Implementation:
- check_callable(): Validates value is of type 'function'
- T.callable(opts): Constructor for callable type
- Returns type_mismatch error if value is not a function
This was missing from the initial implementation and is needed by
lua_meta.lua which validates callback functions in metafunction schemas.
Documentation added to README.md scalar types section.
- `T.number(opts)` - Number with optional range constraints (min, max)
- `T.integer(opts)` - Integer (number with integer constraint)
- `T.boolean()` - Boolean value
+- `T.callable()` - Function/callable value
- `T.enum(values)` - One of a fixed set of values
- `T.literal(value)` - Exact value match
})
end
+local function check_callable(node, value, ctx)
+ if type(value) ~= "function" then
+ return false, make_error("type_mismatch", ctx.path, {
+ expected = "function",
+ got = type(value)
+ })
+ end
+
+ return true, value
+end
+
+function T.callable(opts)
+ return make_node("scalar", {
+ kind = "callable",
+ opts = opts or {},
+ _check = check_callable
+ })
+end
+
function T.enum(values, opts)
opts = opts or {}
opts.enum = values