meta.ts

Metadata for schemas, kept beside them in a registry so a schema stays the three-field object it is and a bundle that attaches none pays nothing.

meta returns a copy of a schema with metadata attached, and get_meta reads it back. The metadata belongs to that one object:

  • flavor and brand return the object they were given, so its metadata is still there.
  • message builds a new schema and carries the metadata over to it, the same object, so it is found on either.
  • Every wrapper and container builds a new schema, which has none. A wrapper holds the schema it was given under def.schema, so metadata on a wrapped schema is found by walking to it.

SchemaMeta has description built in and takes other keys by declaration merging.

The registry is one for the whole JS realm, on globalThis under Symbol.for('@fuzdev/fuz_schema/meta'), so every copy of this module shares it: a second install of the package, a bundler's duplicate, another version that keeps it there. Metadata attached through one copy is read through any other. That makes the registry's shape a contract between versions: a WeakMap from schema object to its metadata object, which meta replaces with a merged copy and never mutates. A version that changes it takes a new key. message in schema.ts reads and writes it by the same key.

Import as a namespace or by name: import {meta, get_meta} from '@fuzdev/fuz_schema/meta.ts'.

view source

Declarations
#

3 declarations

get_meta
#

meta.ts view source

get_meta(schema: AnySchema): Readonly<SchemaMeta> | undefined

Reads the metadata attached to schema by meta, or undefined when it has none. Only that object is looked at, not the schemas inside it.

import {get_meta} from '@fuzdev/fuz_schema/meta.ts';

returns

the stored object, the same one on every call and the one a message built over schema carries, so a change made to it through a cast shows on both

meta
#

meta.ts view source

meta<T extends AnySchema>(schema: SchemaRefinement<TOutput, TInput, TKind, TDef>, meta: SchemaMeta): SchemaRefinement<TOutput, TInput, TKind, TDef>

Attaches metadata to a copy of schema, leaving schema as it was: one with no options is a constant every use shares. The copy has the same kind, def, and run, so it is otherwise indistinguishable from schema and can go anywhere schema can.

Metadata schema already has is merged key by key, the new keys winning. A key given as undefined wins too, which is how one is cleared.

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

parameters

schema
: SchemaRefinement<TOutput, TInput, TKind, TDef>
#

the schema to copy

#

copied one level deep, so changing its keys afterwards changes nothing

overloads

meta<TInput, TOutput, TKind extends string, TDef>(schema: SchemaRefinement<TOutput, TInput, TKind, TDef>, meta: SchemaMeta): SchemaRefinement<TOutput, TInput, TKind, TDef>
meta<TInput, TOutput, TKind extends string, TDef>(schema: SchemaAction<TOutput, TInput, TKind, TDef>, meta: SchemaMeta): SchemaAction<TOutput, TInput, TKind, TDef>
meta<T extends AnySchema>(schema: T, meta: SchemaMeta): T

SchemaMeta
#

meta.ts view source

What can be attached to a schema. Every key is optional.

Add keys by declaration merging, naming the module as it is imported:

declare module '@fuzdev/fuz_schema/meta.ts' { interface SchemaMeta { aliases?: Array<string>; } }
import type {SchemaMeta} from '@fuzdev/fuz_schema/meta.ts';

members

description
?: string
#

What the value is, for help text and generated docs.

Depends on
#

Imported by
#