← Runtime data validation

Data.optional

luastra/data
Data.optional(schema: Schema): Schema

Wraps another schema so nil is accepted in addition to the wrapped value. A non-nil value still passes through the complete nested validation contract.

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

This pure constructor returns an immutable schema synchronously; Data.decode performs the actual validation later.

When to use it

Use Data.optional for fields that may be absent by design, not merely because validation is inconvenient. Keep required identifiers, security decisions, and version fields non-optional.

Parameters

ParameterType / valuesDescription
schemaSchemaSchema to validate whenever the value is not nil.

Returns

Schema — an immutable runtime schema that can be composed or passed to Data.decode.

What happens next

An immutable schema ready to compose or pass to Data.decode.

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 note = Data.optional(Data.string { maxBytes = 240 })

Continue learning

Prerequisites

Data.string

Complete inventory

View all pages in Runtime data validation
← Previous: Data.objectNext: Data.decode →