Skip to main content

Module validate

Module validate 

Source
Expand description

Table validation module.

Validates Lua table structure against a schema definition. Schemas are plain Lua tables — no external schema language required.

§Schema formats

Shorthand — type name only (field is optional):

local schema = {name = "string", age = "number"}

Full — table with constraints:

local schema = {
    name   = {type = "string", required = true, min_len = 1},
    age    = {type = "number", min = 0, max = 150},
    status = {type = "string", one_of = {"active", "inactive"}},
    tags   = {type = "table"},
}

§Supported constraints

KeyApplies toDescription
typeallExpected Lua type name
requiredallField must be non-nil (default: false)
minnumber/integerMinimum value (inclusive)
maxnumber/integerMaximum value (inclusive)
min_lenstringMinimum string length
max_lenstringMaximum string length
one_ofstring/number/integer/booleanAllowed values list

§Usage

local ok, errors = std.validate.check(data, schema)
if not ok then
    for _, msg in ipairs(errors) do print(msg) end
end

Functions§

module