Validation
Use the Validator API to validate all inputs — config values, command arguments, net event data, and API requests.
Basic Validation
lua
local Validator = require('shiva-fw.validator')
local schema = Validator.schema({
playerId = { type = 'number', required = true, min = 1 },
item = { type = 'string', required = true, maxLength = 64 },
count = { type = 'number', required = true, min = 1, max = 100 },
})
local data, errors = schema:validate({
playerId = source,
item = 'fish_bass',
count = 1,
})
if errors then
-- errors is a table of { field, message } pairs
for _, err in ipairs(errors) do
print(err.field, err.message)
end
return
end
-- data is the validated and coerced valueField Types
| Type | Validates |
|---|---|
'number' | Lua number |
'string' | Lua string |
'boolean' | Lua boolean |
'table' | Lua table |
'enum' | One of a list of values |
Field Options
| Option | Type | Description |
|---|---|---|
required | boolean | Error if nil (default: false) |
optional | boolean | Alias for required = false |
min | number | Minimum value (numbers) or length (strings) |
max | number | Maximum value or length |
minLength | number | Minimum string length |
maxLength | number | Maximum string length |
values | table | Allowed values for enum |
pattern | string | Lua pattern for string matching |
default | any | Default value if nil |
Nested Schemas
lua
local schema = Validator.schema({
player = {
type = 'table',
schema = {
id = { type = 'number', required = true },
name = { type = 'string', required = true },
},
},
})Validating Net Events
Always validate data received from clients:
lua
RegisterNetEvent('my-module:castLine', function(rawData)
local source = source
local data, errors = castSchema:validate(rawData)
if errors then
-- silently ignore malformed client data
return
end
-- safe to use data now
end)