← Runtime data validation

Data.decode

luastra/data
Data.decode(schema: Schema, value: any): Result

Validates an unknown runtime value against a Schema and returns a discriminated Data.Result instead of throwing for ordinary invalid input. Success contains the trusted value; failure contains a bounded code and path.

Before you use it

Add luastra/data to this module's dependencies in luastra.json, then import it with require("luastra/data"). No host capability is required. Define schemas outside render when they are reused.

How it fits

Data.decode validates an untrusted value synchronously. Branch on result.success before reading result.value or result.error.

When to use it

Use Data.decode immediately after receiving untrusted form, URL, storage, or server data and before casting it to an application type. Branch on result.success and present or log only appropriate bounded failure details.

Parameters

ParameterType / valuesDescription
schemaSchemaRuntime schema that defines the trusted result shape.
valueanyUnknown value received from a runtime boundary.

Returns

Result — a discriminated validation result; branch on success before reading value or error.

What happens next

A tagged success containing a trusted value, or a failure containing a bounded code and path.

Failures and fixes

Ordinary invalid input is a Data.decode failure, not an exception. Invalid schema options are programmer errors and fail immediately. String bounds count UTF-8 bytes; Data.string has no pattern option in this alpha.

Availability

Public-source alpha API in 0.5.0-alpha; host-independent and synchronous.

Focused example

local Data = require("luastra/data")
local validatedScore: number? = nil
local result = Data.decode(
    Data.number { integer = true, min = 0 },
    42
)
if result.success then
    validatedScore = result.value
end

Continue learning

Prerequisites

Data.ResultData.number

Complete inventory

View all pages in Runtime data validation
← Previous: Data.optional