Module schema
Enables writing schemas and enforcing their validity on tables.
Functions
schema.assertValue(name, val, sch[, level]) | Assert that a value satisfies a schema. |
schema.checkValue(val, sch) | Check that a value satisfies a schema. |
schema.makeTable(sch[, path]) | Make a table that enforces the given schema on edits. |
Types
valueSchema | A table defining a set of allowed values. |
Functions
- schema.assertValue(name, val, sch[, level])
-
Assert that a value satisfies a schema.
Parameters:
- name string The name of the value, for the error message.
- val The value to check
- sch valueSchema The schema to assert.
- level
number
The level to error at. Interpreted equivalently to
error
's level parameter. (optional)
- schema.checkValue(val, sch)
-
Check that a value satisfies a schema.
Parameters:
- val The value to check.
- sch valueSchema The schema to validate against.
- schema.makeTable(sch[, path])
-
Make a table that enforces the given schema on edits.
The table may or may not initially satisfy the schema, depending on the default field values specified by the schema.
Parameters:
- sch valueSchema The schema to enforce.
- path
string
The path to this table, for error messages. Empty or
nil
if this is a top-level table. (optional)
Types
- valueSchema
-
A table defining a set of allowed values. All operations are performed in the order they are defined here, so e.g. you do not need to check
type(v)
in_check
if you have specified_type
, but_fields
will not have been checked yet.- _default The default value if the target is nil. If a function, it will be invoked with no arguments each time a default is required. Default values are not exempt from further validation. (optional)
- _optional
boolean
If true, the target may be
nil
without triggering further validation. (optional) - _type
string
The type that the target must have:
type(val) == _type
(optional) - _ge
The target must be greater than or equal to this value:
val >= _ge
(optional) - _gt
The target must be greater than this value:
val > _gt
(optional) - _le
The target must be less than or equal to this value:
val <= _le
(optional) - _lt
The target must be less than this value:
val < _lt
(optional) - _check
function(val)
A function to check the target:
local v = _check(val); v == nil or v == true
(optional) - _fields table Ignored if the target is not a table. A map from keys (which must equal the keys of the target) to valueSchemas for the corresponding value in the target. (optional)
- _keys valueSchema Ignored if the target is not a table. A schema for the keys of the target. (optional)
- _values valueSchema Ignored if the target is not a table. A schema for the values of the target. (optional)