Validation Utilities
These functions can be used to ensure the validity of data and datatypes, and ensure environments are set up correctly.
etc.check_depends
Usage:etc.check_depends(...)
Takes a list of 'dependecies' in the format specified below, and checks if all are satisfied. If so, return true; else return false and the dependency (including the special character) that failed to resolve.
Dependency Format
Each dependency is a string in the form of a name optionally preceded by a special control character that specifies the type of dependency to test for. The following formats are allowed:<name>
: Test if the mod calledname
exists and is currently enabled.$<name>
: Test if there is a non-nil global variable calledname
.@<modname>:<item_id>
: Test if there is a registered item under the namemodname:item_id
.@<alias>
: Test if there is a registered item aliased to the namealias
.&<module>
: Test if the Etc module namedmodule
exists and is currently loaded.
etc.validate_table_struc
Usage:etc.validate_table_struc(table, template[, table_name])
Takes a table and recursively tests if it matches the structure specified in a template. Returns a boolean indicating whether the match was successful, and if false, a 'key path' representing the part of the table that failed (formatted like
table.<key>.<key>...
). table_name
is optional, and if set will change the name of the root table in the key path.Note that keys not specified in the template are allowed to exist in the table; the table can contain arbitrary keys as long as they don't conflict with any defined key types.
Template Format
The template is a table with the exact structure the tested table is expected to have, with its' bottom-level keys containing a string specifying the type they should have. If the string is prepended with a question mark (?
) it is optional; however it is still required to match the type (so it can be of the correct type or nil
).
This example template...
{ foo = { bar = 'string', baz = '?number' }, qux = 'table' }Will match any table with the following:
- A sub-table called
foo
that contains:- A key called
bar
that holds a string, - A key called
baz
that holds a number ornil
,
- A key called
- A sub-table called
qux
that is allowed to contain anything (or nothing) but must be present.
etc.is_*
Usage:etc.is_<type>(...)
Returns true if all members of
...
are of a specified type. See the table below for a list of options for type
.
etc.is_number(...)
: All arguments must be of the Lua number type.etc.is_string(...)
: All arguments must be of the Lua string type.etc.is_bool(...)
: All arguments must be of the Lua boolean type.etc.is_table(...)
: All arguments must be of the Lua table type.etc.is_integer(...)
: All arguments must be of the Lua number type, and have no decimal component.etc.is_function(...)
: All arguments must be of the Lua function type.etc.is_dict(table)
: One argument only; must be a table whose keys are only strings.etc.is_array(table)
: One argument only; must be a table whose keys are only integer numbers.etc.is_vector(...)
: All arguments must be Minetest vector objects with the vector metatable.etc.is_itemstack(...)
: All arguments must be Minetest ItemStack userdata objects.etc.is_itemstring(...)
: All arguments must be strings pointing to a registered item.