@tola/schema

Declare, compose, inspect, and run validation schemas over metadata and Typst values.

Import

typst
#import "@tola/schema:0.0.0": *

Pass a dictionary of field rules to schema(...). Each field accepts a Typst type (such as str or int) or a schema rule (such as array-of(str)). Wrappers evaluate from the inside out: non-empty(trim(str)) trims whitespace first, then rejects empty strings.

Use parse to return the validated value or raise an error; use try-parse to handle (ok:, value:) / (ok:, issues:) directly. For content file metadata, prefer @tola/source#parse-sources so errors point to the source file.

Examples

Validate page metadata with defaults and constraints

typst
#import "@tola/schema:0.0.0": array-of, date, non-empty, optional, parse, schema, trim

#let post-schema = schema((
  title: non-empty(trim(str)),
  draft: optional(bool, default: false),
  published: optional(date),
  tags: optional(array-of(non-empty(trim(str))), default: ()),
))

#let meta = parse((title: "  Notes  "), post-schema)

Exports

any

typc
any: value (defined by package)

Schema accepting any Typst value unchanged.

array-of

typc
let array-of(
  member: type | dictionary,
) = dictionary;

Validate an array whose elements each satisfy member.

Positional parameters

  • member type | dictionary required: Element type or schema.

at-least

typc
let at-least(
  member: type | dictionary,
  minimum: int | float,
) = dictionary;

Require a numeric value (int or finite float) to be greater than or equal to minimum.

Positional parameters

  • member type | dictionary required: Inner numeric schema.
  • minimum int | float required: Inclusive lower bound.

at-most

typc
let at-most(
  member: type | dictionary,
  maximum: int | float,
) = dictionary;

Require a numeric value (int or finite float) to be less than or equal to maximum.

Positional parameters

  • member type | dictionary required: Inner numeric schema.
  • maximum int | float required: Inclusive upper bound.

check

typc
let check(
  member: type | dictionary,
  inspect: function,
  on: none | array = none,
) = dictionary;

Attach custom multi-issue validation logic to member.

Positional parameters

  • member type | dictionary required: Inner type or schema run first.
  • inspect function required: Function (value) => array returning zero or more issue(...) records.

Named parameters

  • on none | array default: none: Optional field path or key where reported issues attach.

convert

typc
let convert(
  member: type | dictionary,
  convert-value: function | type,
  output: type | dictionary = any,
) = dictionary;

Validate member, then transform it with a fallible converter returning ok(...) or err(...).

Positional parameters

  • member type | dictionary required: Inner type or schema run first.
  • convert-value function | type required: Function (value) => ok(v) | err(...).

Named parameters

  • output type | dictionary default: any: Optional schema describing the converted output type for inspect.

date

typc
date: value (defined by package)

Built-in schema parsing a calendar date ("YYYY-MM-DD" or Typst datetime) into a Typst datetime date.

defaults

typc
let defaults(
  member: type | dictionary,
) = dictionary;

Materialize the default dictionary defined by an object schema (requires every field to be optional or have a default).

Positional parameters

  • member type | dictionary required: Object schema whose defaults to resolve.

describe

typc
let describe(
  member: type | dictionary,
  description: string,
) = dictionary;

Attach a human-readable description string to member for inspect.

Positional parameters

  • member type | dictionary required: Inner type or schema.
  • description string required: Non-empty description string.

dictionary-of

typc
let dictionary-of(
  member: type | dictionary,
) = dictionary;

Validate a dictionary whose values each satisfy member.

Positional parameters

  • member type | dictionary required: Value type or schema applied to every entry.

email

typc
email: value (defined by package)

Built-in string schema validating email address syntax.

enum-of

typc
let enum-of(
  values: array,
) = dictionary;

Match any value equal to one of the elements in values.

Positional parameters

  • values array required: Non-empty array of allowed values.

err

typc
let err(
  ..issues: arguments,
) = dictionary;

Wrap one or more validation issues returned from a convert callback.

Rest parameters

  • ..issues arguments: One or more issue(...) records (or message strings).

format-issues

typc
let format-issues(
  issues: array,
) = string;

Format an array of issues from try-parse into readable multi-line text.

Positional parameters

  • issues array required: Array of issue records.

http-url

typc
http-url: value (defined by package)

Built-in string schema validating absolute http:// or https:// URLs.

https-url

typc
https-url: value (defined by package)

Built-in string schema validating absolute https:// URLs.

inspect

typc
let inspect(
  member: type | dictionary,
  input: bool = false,
) = dictionary;

Return a structured description of a schema’s fields, types, defaults, and descriptions without parsing data.

Positional parameters

  • member type | dictionary required: Schema to inspect.

Named parameters

  • input bool default: false: When true, describe the accepted input shape before conversions; false (default) describes the output shape.

ip

typc
ip: value (defined by package)

Built-in string schema accepting either IPv4 or IPv6 addresses.

ipv4

typc
ipv4: value (defined by package)

Built-in string schema validating dotted-quad IPv4 addresses.

ipv6

typc
ipv6: value (defined by package)

Built-in string schema validating IPv6 addresses (full or ::-compressed).

issue

typc
let issue(
  message: string,
  code: string = "custom",
  notes: array = (),
  path: array = (),
) = dictionary;

Construct a structured validation issue for check or convert.

Positional parameters

  • message string required: Human-readable problem description.

Named parameters

  • code string default: "custom": Optional machine-readable issue code.
  • path array default: (): Optional array of path segments locating the offending field or index.
  • notes array default: (): Optional array of explanatory note strings.

lazy

typc
let lazy(
  factory: function,
) = dictionary;

Defer schema construction to support recursive data structures.

Positional parameters

  • factory function required: Zero-argument function () => schema evaluated on demand.

literal

typc
let literal(
  value: any,
) = dictionary;

Match only a single exact Typst value.

Positional parameters

  • value any required: Exact expected value.

map

typc
let map(
  member: type | dictionary,
  convert-value: function | type,
  output: type | dictionary = any,
) = dictionary;

Transform a validated value with convert-value (cannot fail with schema issues).

Positional parameters

  • member type | dictionary required: Inner type or schema run first.
  • convert-value function | type required: Function (value) => new-value applied after member succeeds.

Named parameters

  • output type | dictionary default: any: Optional schema describing the mapped output type for inspect.

matches

typc
let matches(
  member: type | dictionary,
  expression: regex,
) = dictionary;

Require a string value to match a regular expression.

Positional parameters

  • member type | dictionary required: Inner string schema.
  • expression regex required: Typst regex(...) or pattern string.

max-length

typc
let max-length(
  member: type | dictionary,
  maximum: int,
) = dictionary;

Require a string, array, dictionary, or bytes value to have length at most maximum.

Positional parameters

  • member type | dictionary required: Inner type or schema.
  • maximum int required: Non-negative maximum length.

min-length

typc
let min-length(
  member: type | dictionary,
  minimum: int,
) = dictionary;

Require a string, array, dictionary, or bytes value to have length at least minimum.

Positional parameters

  • member type | dictionary required: Inner type or schema.
  • minimum int required: Non-negative minimum length.

non-empty

typc
let non-empty(
  member: type | dictionary,
) = dictionary;

Reject empty strings, arrays, dictionaries, or bytes after validating member.

Positional parameters

  • member type | dictionary required: Inner type or schema.

nullable

typc
let nullable(
  member: type | dictionary,
) = dictionary;

Allow none in addition to values matching member.

Positional parameters

  • member type | dictionary required: Inner type or schema checked when the value is not none.

ok

typc
let ok(
  value: any,
) = dictionary;

Wrap a successful converted value returned from a convert callback.

Positional parameters

  • value any required: Converted value.

one-or-many

typc
let one-or-many(
  member: type | dictionary,
) = dictionary;

Accept either a single member value or an array of member values, normalizing to an array.

Positional parameters

  • member type | dictionary required: Element type or schema.

optional

typc
let optional(
  member: type | dictionary,
  ..options: arguments,
) = dictionary;

Allow a dictionary field to be omitted, optionally supplying a default value when absent.

Positional parameters

  • member type | dictionary required: Inner type or schema validated whenever the key is present.

Rest parameters

  • ..options arguments: Optional named default: value used when the key is missing (must itself satisfy member). Invalid supplied values fail rather than falling back to default.

parse

typc
let parse(
  value: any,
  member: type | dictionary,
) = any;

Validate value against member and return the parsed result, raising a formatted error on failure.

Positional parameters

  • value any required: Input value to validate.
  • member type | dictionary required: Type or schema declaration.

partial

typc
let partial(
  member: type | dictionary,
  ..fixed: arguments,
) = function;

Make all fields of an object schema optional, except any field names listed in fixed.

Positional parameters

  • member type | dictionary required: Object schema created with schema(...).

Rest parameters

  • ..fixed arguments: Optional field names that remain required.

partial-date

typc
partial-date: value (defined by package)

Built-in schema parsing a year ("YYYY"), year-month ("YYYY-MM"), or full date into (year:, month:, day:, precision:, iso:).

refine

typc
let refine(
  member: type | dictionary,
  predicate: function,
  message: string = none,
) = dictionary;

Add a boolean predicate check to member.

Positional parameters

  • member type | dictionary required: Inner type or schema run first.
  • predicate function required: Function (value) => bool returning true when valid.

Named parameters

  • message string default: none: Optional error message when predicate returns false.

schema

typc
let schema(
  fields: dictionary,
  unknown: string = "error",
) = dictionary;

Declare a dictionary schema mapping field names to types or child schemas.

Positional parameters

  • fields dictionary required: Dictionary mapping field names to Typst types or @tola/schema rules.

Named parameters

  • unknown string default: "error": How undeclared keys are handled: "error" (default, reject), "strip" (drop), or "keep" (preserve unchanged).

timestamp

typc
timestamp: value (defined by package)

Built-in schema parsing an RFC 3339 timestamp string (with timezone offset) into (datetime:, offset:, rfc3339:).

Pass .rfc3339 directly to @tola/feed#feed or @tola/sitemap#sitemap.

trim

typc
let trim(
  member: type | dictionary,
) = dictionary;

Strip leading and trailing whitespace from a string before passing it to member.

Positional parameters

  • member type | dictionary required: Inner string schema (typically str).

try-parse

typc
let try-parse(
  value: any,
  member: type | dictionary,
) = dictionary;

Validate value against member without panicking, returning (ok: true, value: ...) or (ok: false, issues: ...) .

Positional parameters

  • value any required: Input value to validate.
  • member type | dictionary required: Type or schema declaration.

tuple

typc
let tuple(
  ..members: arguments,
  rest: none | type | dictionary = none,
) = dictionary;

Validate a fixed-position array with per-slot schemas and an optional trailing rest schema.

Rest parameters

  • ..members arguments: Positional schemas for each required index.

Named parameters

  • rest none | type | dictionary default: none: Optional schema for extra elements after members; none (default) rejects extra elements.

union

typc
let union(
  ..members: arguments,
) = dictionary;

Try candidate schemas in order and accept the first match.

Rest parameters

  • ..members arguments: Two or more candidate types or schemas tried from left to right.

url-path

typc
url-path: value (defined by package)

Built-in schema validating a site-root URL path (such as a permalink) and decoding it for output-of or route-url.

variant

typc
let variant(
  tag-key: string,
  ..branches: arguments,
) = dictionary;

Select a branch schema by matching a discriminator field (tag-key).

Positional parameters

  • tag-key string required: Field name holding the discriminator value.

Rest parameters

  • ..branches arguments: Candidate schema(...) declarations, each fixing tag-key with literal(...) or enum-of(...).

Search this site

Use the arrow keys to move through results.

Type to search.