compile.ts

Compiles a schema to a function of straight-line code: compile_guard to what is answers, and compile_parse to what parse returns. And to_guard_source writes the guard as the text of a module, for a build to write to a file.

The compiler reads a schema's kind and def, emits JavaScript source, and makes a function of it with new Function. The interpreter is the oracle: a compiled guard gives is's verdict, and a compiled parse gives parse's result, the same ok, an output that is deep-equal and is the input itself exactly where the interpreter's is, and on failure the interpreter's issues.

  • Issues are never compiled. Compiled code builds no issue. A compiled parse of an input that fails calls parse, so its issues are the library's own.
  • A kind the compiler has no code for runs in place. A schema of a kind defined outside the library, or of a library kind whose def is not what the kind holds, is run by its own run for that subtree, handed the object and the key as the library hands them, and the code around it is compiled. So is, in a compiled parse, a keyed_record, an object_with_rest, a union that may try an option again without its key, and a memo_union that can change a value.
  • A schema is read, never written to, and nothing is kept on one: the caller holds the function a compile returns. A lazy's function is called once, at the compile.
  • Where new Function is refused, as under a content security policy without unsafe-eval, the functions returned run the interpreter.
  • Where compiled code runs out of stack, the interpreter answers, or throws its own RangeError where it runs out too. A schema that holds a lazy, a memo_union, or a schema of a kind the compiler has no code for catches a RangeError at its root, any RangeError, and calls is or parse. Another schema has no catch.

What is refused. A failed parse is the library's walk and so is every subtree run in place, so compiled code can't bound what the library doesn't, and the compiler throws on a schema the library can walk in time exponential in the depth of an input, naming where it is:

  • a union on a cycle, two or more options of which reach a cycle and could take the same array, or the same object unless a literal entry at one key, with a value of its own in each of those options, comes before every entry that reaches a cycle. It is the rule by which the Rust crate keeps a union's verdicts, asked of a union that is on a cycle. What to write instead: a memo_union, or where each option is an object schema, a variant or those literal entries first.
  • a pipe on a cycle, two or more steps of which reach a cycle. What to write instead: the pipe as the one option of a memo_union.

A cycle is one only a lazy closes, and one a memo_union is on is none, as it is asked of a value once and then read back. So a memo_union, which is_memo_union tells, is never refused, and compiled code keeps its verdicts for the length of a call, as the library's does while it probes. The rule refuses some unions the library walks in good time, whose options differ below their first level. What it can't see is not refused, and is not bounded: a kind defined outside the library, a function a check or a transform calls, and a union given a memo_union's own options array, which is told as one though its own run keeps nothing.

The bound. What compiles is never exponential in the depth of an input that is a tree, in a guard, a parse that matches, or a parse that fails: the bound is stated in docs/design.md, under Compiling, with what it is not of.

What may differ from the interpreter, each said at more length in https://github.com/fuzdev/fuz_schema/blob/main/docs/design.md, which is where they are listed:

  • How often a user's function runs. A compiled parse of an input that fails has run each check, transform, and with_default_fn it reached before the failure, and parse runs them again, as it does for a call that ran out of stack. A RangeError one throws under a schema that has the catch is taken for the stack's, and the interpreter runs the function again. A compiled memo_union keeps its verdicts from its first value, where the library's keeps them only while it probes, so a check under one can run fewer times.
  • What is read of the input. A parse that matches first reads each value of the input in the library's order, an object's entries in the schema's and a union's options in theirs. It reads less often: a copy is filled from the values its entries ran on, and whether a key is present is asked only where the answer can change the result, after the entries. A guard makes no copy, so it reads nothing only a copy reads. A parse that fails reads the input again, in parse. A getter or a Proxy can tell.
  • A schema that takes a library kind's name and a run of its own is compiled as the library's kind, by its kind and def alone, but a uuid, an iso_datetime, and a to_integer, which are run by the schema's run.
  • The depth of input at which the stack runs out. Compiled code can answer an input so deep that the interpreter throws a RangeError, and never the reverse, but a guard to_guard_source writes, which has no interpreter to ask, can throw where the interpreter answers.

It is a module of its own, so a bundle that compiles no schema carries none of it.

view source

Declarations
#

4 declarations

compile_guard
#

compile.ts view source

compile_guard<T extends Schema>(schema: T): (value: unknown) => value is InferInput<T>

Compiles schema to a guard: a function that gives is(schema, value), from straight-line code that builds no issue, and no context or output but where a subtree runs in place, a memo_union keeps its verdicts, or a step of a pipe makes the value the next step is given.

Every kind of the library is compiled. A schema of a kind defined outside it, or whose def is not what its kind holds, is run in place by its own run, in probe mode, with the code around it compiled. Where new Function is refused, the function returned calls is.

A user's check, transform, and with_default_fn run as is runs them, and again in is where compiled code ran out of stack, see the module's comment for what may differ from is, what is refused, and the bound of a schema that compiles.

import {compile_guard} from '@fuzdev/fuz_schema/compile.ts';

parameters

schema
: T
#

read at the compile, by its kind and def, and never written to

returns

the guard, which the caller keeps: nothing is kept on the schema

throws

  • Error - naming the path where schema, or what a lazy's function returns, is not a schema, the kind and the path of a schema nested deeper than COMPILE_MAX_DEPTH, through every lazy, and the path of a union or a pipe the library can walk in time exponential in the depth of an input, with what to write instead

examples

const User = s.object({name: s.string, tags: s.array(s.string)}); const is_user = compile_guard(User); is_user({name: 'Ada', tags: []}); // true is_user({name: 'Ada'}); // false

COMPILE_MAX_DEPTH
#

compile.ts view source

127

The most schemas around one, through every lazy, that a compile follows. A schema nested deeper is refused, which is how a lazy whose function makes a new schema each time it is called ends.

import {COMPILE_MAX_DEPTH} from '@fuzdev/fuz_schema/compile.ts';

compile_parse
#

compile.ts view source

compile_parse<T extends Schema>(schema: T): (value: unknown) => SchemaResult<Infer<T>>

Compiles schema to a parse: a function that gives parse(schema, value).

For an input that matches, the result is built by compiled code: the input itself where nothing changed, and a copy only where the library makes one, with the library's keys, in its order. A keyed_record, an object_with_rest, a union that may try an option again without its key, a memo_union that can change a value, and a schema with no code of the compiler's are run in place, by their own run, for their output. For an input that fails, the function calls parse, so the issues are the library's. Where new Function is refused, it calls parse for every input.

A user's check, transform, and with_default_fn that ran before a failure run again in that parse, see the module's comment for what may differ from parse, what is refused, and the bound of a schema that compiles.

import {compile_parse} from '@fuzdev/fuz_schema/compile.ts';

parameters

schema
: T
#

read at the compile, by its kind and def, and never written to

returns

the parse, which the caller keeps: nothing is kept on the schema

throws

  • Error - naming the path where schema, or what a lazy's function returns, is not a schema, the kind and the path of a schema nested deeper than COMPILE_MAX_DEPTH, through every lazy, and the path of a union or a pipe the library can walk in time exponential in the depth of an input, with what to write instead

examples

const Settings = s.object({theme: s.with_default(s.string, 'dark')}); const parse_settings = compile_parse(Settings); parse_settings({}); // {ok: true, value: {theme: 'dark'}} parse_settings({theme: 1}); // {ok: false, issues: [...]}, from `parse`

to_guard_source
#

compile.ts view source

to_guard_source(schema: Schema, name: string, library?: string): string

Writes schema as the text of an ES module that exports a guard under name: a function that gives is(schema, value), for a build to write to a file, so the guard ships with no new Function and, where the schema holds no format action, without the library.

The text is the code compile_guard makes, with what that captures written out. A regex is written as its pattern and flags. A uuid, an iso_datetime, and a to_integer are imported from library and run as the library's, by their kind and def, so the text holds no copy of a grammar. A memo_union keeps its verdicts for the length of a call. What compile_guard refuses is refused, and so is what has no text: a check, a transform, a with_default_fn, a schema of a kind defined outside the library, and a subtree compile_guard hands to the interpreter.

The text is JavaScript, and starts with `//

import {to_guard_source} from '@fuzdev/fuz_schema/compile.ts';

parameters

schema
: Schema
#

read by its kind and def, and never written to

name
: string
#

what the module exports the guard as: an identifier but then, and default for the default export

library
?: string = '@fuzdev/fuz_schema/schema.js'
#

the specifier the module imports the format actions from, a string

returns

the text of the module

throws

  • Error - where name is no identifier or is then, where library is no string, naming the kind and the path of a schema that can't be written, and all that compile_guard throws

examples

const User = s.object({name: s.string, tags: s.array(s.string)}); to_guard_source(User, 'is_user'); // '//

Depends on
#