Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.sure-developer.com/llms.txt

Use this file to discover all available pages before exploring further.

Validator

The validator module creates small schema objects that expose parse(data). A successful parse returns true. A failed parse raises Validation Error: ..., so wrap user-controlled inputs with pcall when you want to handle errors without stopping execution.
local v = sure.getModule('validator')

Basic schema

local itemSchema = v.object({
  name = v.string().required('Item name is required'),
  amount = v.integer().min(1).max(100),
  metadata = v.object({
    label = v.string(),
    temporary = v.boolean()
  })
})

Rules

required(message?)
function
Marks a value as required. When message is provided, that message overrides the default missing-field error.
message(message)
function
Sets a custom error message for the rule. The message is used when type or custom checks fail.
min(value)
function
Requires a numeric value to be greater than or equal to value.
max(value)
function
Requires a numeric value to be less than or equal to value.
between(min, max)
function
Applies both min(min) and max(max).
oneOf(values)
function
Requires the parsed value to equal one entry in the given array.

Types

BuilderAccepts
v.object(fields)A Lua table with named fields.
v.array(itemRule)A Lua array table where every item passes itemRule.
v.string()Lua string.
v.number()Lua number.
v.integer()Lua number with no fractional part.
v.boolean()Lua boolean, including false.
v.callback()Lua function.

Nested example

local rewardSchema = v.object({
  account = v.string().oneOf({ 'money', 'bank', 'black_money' }).required(),
  items = v.array(v.object({
    name = v.string().required(),
    amount = v.integer().min(1)
  }))
})

rewardSchema.parse({
  account = 'bank',
  items = {
    { name = 'bread', amount = 2 },
    { name = 'water', amount = 1 }
  }
})
parse raises errors by design. Use pcall when validating network input, command arguments, or config edited by other people.