vērāx — the exact contract
What verax guarantees for any input, where it stops, and how each schema reads a value. The docs show it running.
Guarantees and limits
For any input, validation never throws and never returns a wrong verdict: a throwing
callback, hostile getter or proxy, or exhausted stack becomes an issue. Every limit below
fails closed: it is an issue (and is is
false), never an accept. For plain data — what
JSON.parse or structuredClone produce,
shared subtrees and holes included — the limits bound time and memory. Other objects cost
in proportion to their structure: verax reads only what the schema asks for, but an engine
may step over an object's non-enumerable or symbol keys, uncounted, on every visit.
-
Plain objects.
objectandrecordaccept an object of this realm whose prototype isObject.prototypeornull— not arrays, dates, class instances, or an object created beneath another. Bring another realm's objects over withstructuredClone; lists from any realm are lists. -
Too deep. Nesting past 512 objects, records and lists, the same on every engine, counted on through validations nested in callbacks. Inside a union it fails the member that met it; later members are still tried. A cyclic value is walked like any other, only as far as the schema goes: a schema that follows the cycle meetsToo deep, one that stops before it accepts. -
Stack. A stack exhausted anyway (a deep caller, or a dozen or more
wrappers per level on a small stack — node's default fits about 18) is an issue too. Met
by a validation nested in a callback, it fails the whole call (
Threw: stack exhausted), whatever the callback made of it. -
Too complex. Each pass of a call, with every validation nested in its callbacks, may take 10 million steps, whatever a callback made of a verdict. Entering an object, record or list is a step, and so is each field, key or item it holds, each key its shape lacks, and each union member tried and failed — on every attempt, and along every path to a shared object. A list of n items, an object of n fields and a record of n keys are each n + 1: a million rows of 3 fields is 5 million; a list of 10 million numbers is past it, and so are 100,000 items that each fail 99 members of a union tried in turn before one accepts. A list that fails counts all its items, read or not (its output was sized for them), and a record that fails every key it enumerates — except that a leaf value failing within its first 9 keys costs nothing, as a leaf field does. -
Waste is
Too complextoo: failed union members and a nested validation's rejected fast pass may exceed 16× the useful steps by a million, no more. A member failing on a leaf field (enum, literal, scalar — read before nested ones) or an unknown key wastes nothing: the leaf fields it read count toward the 10 million past the first 8, and a shape of more than 8 leaf fields reads enums and literals first. So a discriminant tried in turn costs one step per member tried, wherever its key sorts; found by lookup, one step if no member holds its value, whatever the union's width. A member failing later costs one step plus each field it read (and what that held), and wastes what it costs past 8; untagged unions whose failures cost under 16× the match never trip it. Waste that compounds does: a recursive union whose members differ only below the recursion tries every path, so input some 20 levels deep isToo complexeven when valid — give its members a discriminant. -
A union over budget reports
Too complexat its own path, not where it was met. Explaining a failed union tries each member's full check, charged as failed members are, and each issue's path is work; an explanation that runs past the budget is undone — the mismatch stands — and is the call's last. -
Memory.
parseandvalidatecopy each path: an object shared along many paths is copied along each, so their memory, like their time, grows with paths — within the budget, up to roughly 600 MB for one large shared object or record. - Issues stop at 100, and so does the walk.
-
Schemas from elsewhere — another copy of verax, another library
speaking Standard Schema — are black boxes, run through their synchronous
~standard.validatewith their own depth and budget. A result must be an object with an array ofissuesor avalue; anything else, or a thenable, is an issue. Their issue paths become property keys.
Semantics
- Lists are walked by index to their length, holes included.
- Objects read their shape's keys from their own properties — a known key even when non-enumerable — and report their other own enumerable string keys as unknown.
-
Records read every own enumerable string key, in the input's order, and
check its value — any key,
__proto__included. -
Absent keys. A key may be absent exactly when its schema accepts
undefined—optional(x),unknown(),literal(undefined)— and its type is thenkey?: T | undefined. -
Messages read
Expected Int, received "x". A required key that is absent isExpected Int, missing, an explicitundefinedstillreceived undefined; a nullable leaf names null,Expected Int | null; and an object that is not plain — another realm's, or one beneath another — isreceived non-plain object, unless its constructor names it (received Date). -
Types. A schema has an output and an input type;
coercemakes the inputunknown.isandassertnarrow to the input type;parsereturns the output type.isnarrowsfalsetoo: anumberfailingint()becomesnever— guardunknown. -
Defaults.
optionalshort-circuitsundefinedbefore an innercoerceruns; write a default ascoerce(optional(x), (v) => v ?? d)— its output type still admitsundefined. -
Unions try members in order and the first to accept wins. A member
stops at its first failure, checking leaf fields, then unknown keys, before nested ones
— so a discriminant field makes recursive unions linear regardless of key order. A
member that throws failed, except on stack exhaustion (a
RangeErrorabout the call stack), which fails the whole check. -
Lookup. A discriminated union finds its member by lookup: when every
member is a verax
object(not refined or coerced) holding a definedliteralat one key, a distinct value in each (0and-0count as one), read after only built-in leaf fields, the value's key is read once, first, and only the member holding it is tried. For plain data the verdict is the one trying each would reach — every other member fails at that key. A getter, proxy or callback that changes the value mid-check can change the outcome either way. On first use such a union reads every member's shape; a shape getter that throws leaves its members tried in turn until it reads — unless it exhausts the stack, which fails the check. -
A union that rejects says why, in a top-level call only — a validation
nested in a callback reports the mismatch alone, as its caller's verdict is still open.
Tagged — every member an
object, or arefineof one (never acoerce, which may rewrite the tag), holding a distinct literal at one key — it reports the member the value's tag names, or one issue at that key naming the tags it accepts:__typename: Expected "Ping" | "Track", received "Pong". Otherwise it reports its closest member: of those failing below the value itself and knowing every key it holds, the one with the fewest issues, the first on a tie. Only the outermost failing untagged union explains itself: comparing members, a union nested in one is taken to pass, and in the closest member's issues a nested untagged union is its mismatch, at its path (a tagged one still reports its member). When no member qualifies, it isExpected User, received object(orToo deep) at the union's path. A member's throw is its issue, at its path. -
Snapshot.
parseandvalidateread each field and item once, and every object, record and list the schema checks is a fresh plain one — objects in shape key order and records in the input's, integer-like keys first, as JavaScript does — so no getter, proxy, prototype or later mutation of the input reaches the output. Values underunknown()or a custom scalar — acoerceinto one included — pass through as they are, so an output may hold the input's cyclic references.isbuilds only what arefinetest must see. -
Rejection costs a second pass. A fast check decides, then a full check
collects the issues — so on invalid input
refine/coercecallbacks and input getters run again, and inside a union once per member tried — then, to explain a union that rejects, once more per member compared and again for the closest. -
Schemas come from verax's factories; a spread or copy of one is that
schema. A Standard Schema from elsewhere nests as a field, list item, union member, or
under
nullable/optional, typed by its own~standard.types;refine,coerceandsdl()need verax's own.
Recursive schemas
Make the field a getter and annotate the schema with its type, which TypeScript cannot
infer through a cycle — a bare Schema infers
unknown. The shape is read once, on first use: its getters run
then, def.shape is a frozen copy, and later changes to the
object passed in have no effect.
import type { Schema } from 'verax';
type Node = {
next: Node | null;
note?: string | undefined;
};
const Node: Schema<Node> = object('Node', {
get next() {
return nullable(Node);
},
note: optional(string()),
});