← Runtime data validation

Data.array

luastra/data
Data.array(item: Schema, optionsValue: ArrayOptions?): Schema

Builds a dense-array schema whose every element must satisfy the supplied item schema, with optional array-length bounds. Validation records the failing index so callers can identify malformed members.

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.array for ordered JSON-style collections with contiguous indexes and one element contract. Use Data.object for named fields or a custom migration when the input is a sparse keyed map.

Parameters

ParameterType / valuesDescription
itemSchemaSchema applied independently to every dense array element.
optionsValueArrayOptions?Optional minimum and maximum item counts.
optionsValue.minItemsinteger (0..256)?Minimum admitted item count; defaults to 0.
optionsValue.maxItemsinteger (0..256)?Maximum admitted item count; defaults to 256.

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 tags = Data.array(Data.string { maxBytes = 40 }, { maxItems = 12 })

Continue learning

Prerequisites

Data.string

Complete inventory

View all pages in Runtime data validation
← Previous: Data.booleanNext: Data.object →