introspect.ts

Reading a schema as data: walking through its wrappers to the schema they hold, finding metadata and defaults on the way, listing an object's entries, and rendering a schema's input type as text for help output.

A module of its own, so a bundle that never reads a schema pays nothing, and each export is dropped when unused. It reads schemas and makes none.

The layers of a schema are the schemas a walk passes through to reach what the value is: optional, nullable, with_default, and with_default_fn lead to their def.schema, a pipe to its first schema, and a lazy to the schema its function returns, which is called to find it. Nothing else is called: the make of a with_default_fn never is. message, flavor, and brand are no layer: they keep the kind and def of what they were given. Metadata is found on any layer, as meta attaches it to one object, so with_default(meta(s.boolean, {description}), true) and meta(with_default(s.boolean, true), {description}) read the same. message carries the metadata of the schema it was given over to the one it returns, so it is found there too. find_schema_meta reads every schema a value can reach, not only the layers.

Import by name: import {get_schema_fields} from '@fuzdev/fuz_schema/introspect.ts'.

view source

Declarations
#

8 declarations

find_schema_meta
#

introspect.ts view source

find_schema_meta<TKey extends keyof SchemaMeta>(schema: AnySchema, key: TKey, options?: FindSchemaMetaOptions): Exclude<SchemaMeta[TKey], undefined>[]

Every value of metadata key other than undefined on any schema reachable from schema, for a key that must not be missed wherever it is attached, as a mark on a shared schema reused deep inside another. The caller decides what several values mean, as which of them is the strongest.

  • It goes through every layer, as get_schema_meta does, and also every schema and action of a pipe, not only its first, every option of a union, a memo_union, and a variant, and with structural the schemas of a value's parts.
  • A layer that has the key as undefined has cleared it for the layers under it, as get_schema_meta reads it, and not for what those hold: the options, the later steps of a pipe, and the parts are read as ever. A schema reached again past no such layer is read there.
  • Each schema object is read once for its value, and walked at most twice, under a layer that cleared the key and under none, a lazy's function called at each, so a recursive schema ends, lazy(() => optional(L)) as L included. A cycle that builds a new lazy each time round, as const f = () => optional(lazy(f)), never meets a schema it has read and ends in a stack overflow, though a parse of a finite value can end on it.
  • The values come depth first, a schema before what it holds, which comes in the order of its def: a pipe's steps, a union's options, an object's entries, then the rest. A value is there once for each schema object it is attached to, so the same value can come more than once.
  • flavor and brand return the schema they were given, and message carries its metadata over to the one it returns, so metadata on the schema given to any of them is found.
  • Nothing but a lazy's function is called, and a kind defined outside the library is read and not looked into.
import {find_schema_meta} from '@fuzdev/fuz_schema/introspect.ts';

parameters

key
: TKey
#

a key of SchemaMeta, description or one added by declaration merging

FindSchemaMetaOptions
#

introspect.ts view source

Options of find_schema_meta.

import type {FindSchemaMetaOptions} from '@fuzdev/fuz_schema/introspect.ts';

members

structural
?: boolean
#

Whether to look into the schemas of a value's parts too: the entries of every object schema, the rest of an object_with_rest, what an array and a record hold for each item, and the key schema and then the value schema of a keyed_record.

format_schema_type
#

introspect.ts view source

format_schema_type(schema: AnySchema): string

Renders the type schema accepts as TypeScript-like text, for help output: Array<string> | null, {name: string; port?: number}, "a" | "b".

  • A with_default and a with_default_fn render as their schema, leaving the default to be shown apart, and an optional adds | undefined.
  • An entry is marked ? when a layer is an optional, a with_default, or a with_default_fn. A union with an optional option lets the key be missing too, and isn't looked into.
  • The other keys of a loose_object render as [key: string]: unknown, and those of an object_with_rest as its rest, after the entries.
  • A pipe renders as its first schema, what it accepts.
  • A keyed_record renders as Record<K, V>, its key schema as K, a finite one too, whose keys are each optional.
  • A lazy renders as the schema its function returns, once, and one that returns a lazy as what that returns: a lazy inside that renders as ..., recursive or not. So a recursive schema shows one level of itself, whatever its functions build, short of a lazy that only returns a lazy, which a parse can't end either, and a lazy nested in another shows as ... though it would end.
  • A string literal is quoted as JSON, as the expected text of an issue has it, and a bigint ends in n.
  • A kind with no text of its own, an action or one defined outside the library, renders as its kind.
import {format_schema_type} from '@fuzdev/fuz_schema/introspect.ts';

get_schema_entries
#

get_schema_fields
#

introspect.ts view source

get_schema_fields(schema: AnySchema): SchemaField[]

Reads every entry of schema, in order, or none when it isn't an object schema under its wrappers.

import {get_schema_fields} from '@fuzdev/fuz_schema/introspect.ts';

get_schema_meta
#

introspect.ts view source

get_schema_meta<TKey extends keyof SchemaMeta>(schema: AnySchema, key: TKey): SchemaMeta[TKey] | undefined

The value of metadata key on the outermost layer of schema that has the key. A layer that has it as undefined has cleared it, as meta says, and gives undefined whatever a layer under it holds, as to_json_schema reads the layers. The later steps of a pipe are no layers and are not read, where to_json_schema writes what they hold over the first's. get_meta reads the one object it is given.

import {get_schema_meta} from '@fuzdev/fuz_schema/introspect.ts';

parameters

key
: TKey
#

a key of SchemaMeta, description or one added by declaration merging

SchemaField
#

introspect.ts view source

What get_schema_fields reads of one entry of an object schema.

import type {SchemaField} from '@fuzdev/fuz_schema/introspect.ts';

members

name
: string
#

The entry's key.

schema
: SchemaNode
#

The entry as given.

has_default
: boolean
#

Whether the outermost layer that is an optional, a with_default, or a with_default_fn is one of the two defaults: an optional outside one answers undefined before the default is asked. default is then the value of a with_default, even when that is undefined.

default_computed
: boolean
#

Whether that default is a with_default_fn's, made by its make on each parse that fills it in. default is then undefined, as make is not called to read it.

default
: unknown
#

The value of that with_default, before any step of a pipe it starts, and undefined when there is none or it is a with_default_fn.

description
: string | undefined
#

The description metadata, as get_schema_meta finds it.

unwrap_schema
#

introspect.ts view source

unwrap_schema(schema: AnySchema): SchemaNode

The innermost layer of schema, what the value is under its wrappers: string for optional(nullable(string)), and schema itself when it is no wrapper.

import {unwrap_schema} from '@fuzdev/fuz_schema/introspect.ts';

Depends on
#