---
title: 'API reference'
description: 'Public surface of the registry package.'
order: 4
---

# API reference

Complete API documentation for `@schema-engine/registry`.

## createRegistry

```typescript
function createRegistry<const TRegistration extends BaseRegistration>(
	kind: string,
	registrations: readonly TRegistration[],
	options?: CreateRegistryOptions,
): ReadOnlyRegistry<TRegistration>;

interface CreateRegistryOptions {
	readonly duplicates?: 'override' | 'error';
}
```

Builds a frozen, read-only registry. By default (`duplicates: 'override'`) a later registration with the same `$type` replaces an earlier one without a warning and keeps its first position. With `duplicates: 'error'` a repeated `$type` throws `<kind> '<$type>' is registered twice.` before any schema is checked; use it for a catalog where a repeat is always a mistake. Throws `<kind> '<$type>' schema must implement Standard Schema (~standard.validate).` when a winning registration's schema is not a Standard Schema; an overridden registration is not checked. The registry stores a frozen shallow copy of each winner and leaves the objects you passed untouched.

### ReadOnlyRegistry

| Member     | Returns                      | Description                              |
| ---------- | ---------------------------- | ---------------------------------------- |
| `kind`     | `string`                     | The label passed to `createRegistry`     |
| `size`     | `number`                     | Count of distinct `$type` keys           |
| `get(key)` | `TRegistration \| undefined` | Gets a registration by `$type`           |
| `has(key)` | `boolean`                    | Checks if the key exists                 |
| `all()`    | `readonly TRegistration[]`   | Frozen array in first-registration order |
| `keys()`   | `readonly string[]`          | Frozen array of keys in the same order   |

## bindRegistry

```typescript
function bindRegistry<TContract extends BaseRegistration, const TBindings>(
	contracts: ReadOnlyRegistry<TContract>,
	bindings: TBindings,
): BoundRegistry<TContract, TBindings>;

type RegistryBindings<TRegistry extends ReadOnlyRegistry, TBinding extends object = object> = {
	readonly [K in TRegistry['$type']]: TBinding;
};

type BoundRegistration<TContract extends BaseRegistration, TBindings> = TContract & TBindings[TContract['$type']];

interface BoundRegistry<TContract extends BaseRegistration, TBindings> extends ReadOnlyRegistry<
	BoundRegistration<TContract, TBindings>
> {
	get<K extends TContract['$type']>(key: K): Extract<BoundRegistration<TContract, TBindings>, { $type: K }>;
	get(key: string): BoundRegistration<TContract, TBindings> | undefined;
}
```

Builds a renderer's registry from a registry of contracts and one binding per `$type`; `TBindings` is constrained to exactly one binding for each `$type` of the contracts, none of which may set a key its contract defines. A contract is a registration without the renderer's part (the `$type`, schema and metadata, plus what a domain fixes); a binding is renderer-only data such as a `render` function or a tag name, and never carries schema or metadata. The result is built with `createRegistry`, so it is frozen, keeps the order of the contracts and checks every schema again. Each registration is a shallow merge, so `schema` and every other value of a contract is shared by identity between the renderers that bind it.

For contracts typed by literal `$type` (an inline list, which `createRegistry` types that way), a missing binding, a binding for a `$type` no contract has and a binding that sets a key of its contract are compile errors, `get(key)` with a known `$type` returns that registration without `undefined`, and `RegistryBindings<typeof contracts, ReactBinding>` with `satisfies` fixes the shape every binding of one renderer takes. For contracts typed only as `BaseRegistration` the same three mistakes throw when the registry is built:

| Mistake                              | Error                                                                |
| ------------------------------------ | -------------------------------------------------------------------- |
| A contract has no binding            | `<kind> '<$type>' has no binding.`                                   |
| A binding names no contract          | `<kind> binding '<key>' names no registration.`                      |
| A binding sets a key of its contract | `<kind> '<$type>' binding sets '<key>', which the contract defines.` |

## assertStandardSchema

```typescript
function assertStandardSchema(schema: unknown, context?: string): StandardSchemaV1;
```

Throws `<context> must implement Standard Schema (~standard.validate).` unless `schema` has a `~standard.validate` function.

## safeParseStandardSchema

```typescript
function safeParseStandardSchema<TInput, TOutput>(
	schema: StandardSchemaV1<TInput, TOutput>,
	value: TInput,
): { success: true; data: TOutput } | { success: false; error: { issues: StandardSchemaIssue[] } };
```

Runs a registration's schema without throwing. It supports only synchronous validation and throws for a schema that validates asynchronously.

```typescript
type StandardSchemaIssue = StandardSchemaV1.Issue;

type StandardSchemaSafeParseResult<T> =
	{ success: true; data: T } | { success: false; error: { issues: StandardSchemaIssue[] } };
```

`StandardSchemaIssue` is the issue of the Standard Schema spec (`message` and an optional `path`), and `StandardSchemaSafeParseResult<T>` is what `safeParseStandardSchema` returns.

## standardSchema

```typescript
function standardSchema<TOutput>(
	validate: (value: unknown) => StandardSchemaV1.Result<TOutput>,
): StandardSchemaV1<unknown, TOutput>;
```

Builds a Standard Schema from a function, for a registration whose check is not a schema library's. `validate` returns `{ value }` for an input that passes and `{ issues }` for one that does not, and it must be synchronous. The schema reports `schema-engine` as its vendor.

## safeParseNode

```typescript
function safeParseNode<TRegistration extends Pick<BaseRegistration, 'schema'>>(
	registry: NodeLookup<TRegistration>,
	node: unknown,
	path?: ReadonlyArray<PropertyKey | { key: PropertyKey }>,
): SafeParseNodeResult<TRegistration>;
```

Checks one node against the registration its `$type` names, without throwing. `registry` is anything whose `get(key)` returns a registration with a `schema`, so a `createRegistry` result, a `BaseRegistry` and a lookup over several registries all fit. `path` is where the node sits in its document, and every issue's `path` starts with it.

```typescript
interface NodeLookup<TRegistration extends Pick<BaseRegistration, 'schema'> = BaseRegistration> {
	get(key: string): TRegistration | undefined;
	hint?: string;
}

type SafeParseNodeResult<TRegistration extends Pick<BaseRegistration, 'schema'> = BaseRegistration> =
	| (TRegistration extends unknown
			? {
					success: true;
					$type: TRegistration extends { $type: infer TType extends string } ? TType : string;
					registration: TRegistration;
					data: StandardSchemaV1.InferOutput<TRegistration['schema']>;
				}
			: never)
	| { success: false; issues: StandardSchemaIssue[] };
```

The success branch has one member per registration of the registry, so a registry typed by its registrations gives `data` the schema output of the registration that `$type` names, with no cast. A registry typed as `ReadOnlyRegistry<BaseRegistration>` has no concrete schema to read, so its `data` is `unknown`. Narrow on `$type` of the result: TypeScript narrows a union only by a property of the union itself, so `result.registration.$type` alone does not narrow `data`.

```ts
import * as z from 'zod/mini';
import { createRegistry, safeParseNode } from '@schema-engine/registry';

const widgets = createRegistry('Widget', [
	{ $type: 'card', schema: z.object({ title: z.string() }), metadata: { name: 'Card' } },
	{
		$type: 'badge',
		schema: z.object({ label: z.string(), tone: z._default(z.string(), 'info') }),
		metadata: { name: 'Badge' },
	},
]);

export function describeNode(node: unknown): string {
	const parsed = safeParseNode(widgets, node);
	if (!parsed.success) return 'invalid';
	return parsed.$type === 'card' ? parsed.data.title : `${parsed.data.label} (${parsed.data.tone})`;
}
```

`createRegistry` keeps the literal `$type` of each inline registration, so the example needs no `as const`.

| Node                               | Result                                                                                                     |
| ---------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| Valid                              | `{ success: true, $type, registration, data }`, where `data` is what the schema returned, defaults applied |
| Not an object                      | One issue at `path`: `Expected an object with a string $type.`                                             |
| An object without a string `$type` | One issue at `[...path, '$type']`: `Expected a string $type.`                                              |
| A `$type` no registration answers  | One issue at `[...path, '$type']`: `No registration for $type '<type>'.` and the lookup's `hint`           |
| The schema refuses it              | The schema's issues, each with `path` in front of the path the schema reported                             |

`hint` is optional: a sentence that follows the message of a `$type` `get` does not answer, such as where to register it. With `hint: 'Register it with defineBadge.'`, an unregistered `chip` is `No registration for $type 'chip'. Register it with defineBadge.`.

`unregisteredTypeMessage(type, hint?)` writes that message, for a caller that has to throw for the same case instead of returning an issue.

An issue points at the value that is wrong, the way a schema reports a missing required key at the key, so an editor can mark it and [`parseYaml`](/docs/schema-engine/serialized-documents#yaml) finds its line. [`parseActions`](/docs/actions/api-reference#parseactions) checks each entry of a workflow with it, so an entry reports these issues at the same places.

It looks at one node; [`safeParseTree`](#safeparsetree) walks a whole tree with it. It throws, like `safeParseStandardSchema`, for a schema that validates asynchronously. The result has the `issues` shape of `parseActions`, so `(value) => safeParseNode(registry, value)` is a validator for [`parseYaml`](/docs/schema-engine/serialized-documents#yaml).

## safeParseTree

```typescript
function safeParseTree<TRegistration extends Pick<BaseRegistration, 'schema' | 'childPaths'>>(
	registry: NodeLookup<TRegistration>,
	root: unknown,
	path?: ReadonlyArray<PropertyKey | { key: PropertyKey }>,
): SafeParseTreeResult<TRegistration>;

type SafeParseTreeResult<TRegistration> =
	{ success: true; tree: ParsedNode<TRegistration> } | { success: false; issues: StandardSchemaIssue[] };

type ParsedNode<TRegistration> = {
	$type: string;
	registration: TRegistration;
	data: StandardSchemaV1.InferOutput<TRegistration['schema']>;
	path: ReadonlyArray<PropertyKey | { key: PropertyKey }>;
	children: Readonly<Record<string, ParsedNode<TRegistration> | readonly ParsedNode<TRegistration>[]>>;
};
```

Checks a whole document without throwing. Every node goes through `safeParseNode` with its full `path`, so an issue is the one `safeParseNode` reports, at the position the document has it, and `formatIssuePath(issue.path, root)` writes it as text. A registration says where its children are with `childPaths`, a list of key paths such as `[['blocks'], ['engine', 'controls']]`. Each path is read from the node as authored; a missing value is no children, an array is walked item by item (the index joins the path) and an object is one child. One lookup serves the whole tree, so which `$type`s a position accepts is the parent's schema to say.

| Case                                            | Result                                                                                            |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| Every node is valid                             | `{ success: true, tree }`, where `children` holds the nodes of each child path, in document order |
| A node's own schema fails                       | Its issues; the node is not walked further, but its siblings still are                            |
| A value at a child path that is no node or list | One issue at that path: `Expected a node or a list of nodes.`                                     |
| A node that contains itself                     | One issue at the node reached again: `A node cannot contain itself.`                              |
| Any issue anywhere                              | `{ success: false, issues }` with all of them, and no tree                                        |

`children` is keyed by the child path with its keys joined by dots (`['engine', 'controls']` is `'engine.controls'`). `createRegistry` checks `childPaths` when it builds: each path must be a non-empty list of non-empty keys, none may repeat and none may run through another (`[['engine'], ['engine', 'controls']]` would reach inside the `engine` child), or it throws `<kind> '<$type>' childPaths must hold non-empty lists of non-empty keys.`, `<kind> '<$type>' declares the child path '<path>' twice.` or `<kind> '<$type>' child path '<path>' runs through the child path '<other>'.` A child may be of any registration of the lookup, so `children` holds nodes of the whole union. Like `safeParseNode`, `$type` of the tree narrows `registration` and `data` for a registry typed by its registrations. It throws, as `safeParseNode` does, for a schema that validates asynchronously.

## formatIssuePath

```typescript
function formatIssuePath(path: StandardSchemaIssue['path'], source?: unknown): string;
```

Writes the `path` of an issue as text for a log or a message: keys are joined by dots, indexes are in brackets, and a missing or empty path is the empty string. A segment is a key or a Standard Schema `{ key }` object, so the `path` of any issue passes as it is.

| `path`                                    | Result                          |
| ----------------------------------------- | ------------------------------- |
| `['children', 1, 'children', 0, 'label']` | `children[1].children[0].label` |
| `[1, 0, 'amount']`                        | `[1][0].amount`                 |
| `[{ key: 'steps' }, { key: 0 }, 'id']`    | `steps[0].id`                   |
| `undefined`                               | `''`                            |

With the document as `source`, an array item that has a string `id` is followed by the id in parentheses, `steps[0] (main).elements[1] (email).id`, so a log of a large document names the node it means.

## BaseRegistry

Abstract, mutable class for plugin systems that register at runtime. Prefer `createRegistry` otherwise.

```typescript
abstract class BaseRegistry<TKey extends string, TValue extends BaseRegistration> {
	protected items: Map<TKey, TValue>;

	abstract get registryName(): string;
	protected getItemKey(item: TValue): TKey;
	protected getItemName(item: TValue): string;

	register(item: TValue): void;
	has(key: TKey): boolean;
	get(key: TKey): TValue | undefined;
	getAll(): TValue[];
	getAllKeys(): TKey[];
	get size(): number;
	clear(): void;
}
```

### Methods

| Method           | Returns               | Description                                  |
| ---------------- | --------------------- | -------------------------------------------- |
| `register(item)` | `void`                | Registers an item. Logs warning on override. |
| `has(key)`       | `boolean`             | Checks if key exists                         |
| `get(key)`       | `TValue \| undefined` | Gets item by key                             |
| `getAll()`       | `TValue[]`            | Returns all items                            |
| `getAllKeys()`   | `TKey[]`              | Returns all keys                             |
| `size`           | `number`              | Count of items                               |
| `clear()`        | `void`                | Removes all items                            |

### Protected methods

| Method              | Description                                    |
| ------------------- | ---------------------------------------------- |
| `getItemKey(item)`  | Extracts key from item (uses `$type`)          |
| `getItemName(item)` | Extracts name from item (uses `metadata.name`) |

## Types

### BaseRegistration

```typescript
import type { StandardSchemaV1 } from '@standard-schema/spec';

interface BaseRegistration<_TConfig = unknown, TMeta extends RegistrationMetadata = ComponentMetadata> {
	$type: string;
	schema: StandardSchemaV1;
	metadata: TMeta;
}
```

Zod Mini, Zod, Valibot, and Effect Schema values implement Standard Schema and remain valid `schema` values.

### RegistrationMetadata

```typescript
interface RegistrationMetadata {
	name: string;
	description?: string;
}
```

### ComponentRegistration

```typescript
interface ComponentRegistration<
	TType extends string = string,
	TConfig = unknown,
	TProps = unknown,
	TMeta extends ComponentMetadata = ComponentMetadata,
> extends BaseRegistration<TConfig, TMeta> {
	/** Framework-agnostic render function */
	render: (props: TProps) => unknown;
	/** Type identifier - enforced to match registry key */
	$type: TType;
}
```

### ComponentMetadata

```typescript
interface ComponentMetadata extends RegistrationMetadata {
	icon?: string;
	tags?: string[];
	version?: string;
}
```

### Brand

Utility type for compile-time type discrimination (erased at runtime):

```typescript
type Brand<T, TBrand> = T & { readonly [__brand]?: TBrand };
```

## YAML subpath

`@schema-engine/registry/yaml` parses YAML documents and validates them with any validator that reports
Standard Schema issues, then points each issue at a line and column. It needs the optional peer dependency
`yaml`; the root entry never imports it.

```typescript
parseYaml<T>(text: string, validate: YamlValidate<T>, options?: { filename?: string }): T; // throws YamlDocumentError
safeParseYaml<T>(
	text: string,
	validate: YamlValidate<T>,
	options?: { filename?: string },
): { success: true; data: T } | { success: false; issues: YamlIssue[] };
stringifyYaml(value: unknown, options?: { schemaUrl?: string }): string;

type YamlIssue = { message: string; path: PropertyKey[]; line: number; column: number; filename?: string };

type YamlValidate<T> = (
	value: unknown,
) =>
	| { success: true; data: T }
	| {
			success: false;
			issues: ReadonlyArray<{ message: string; path?: ReadonlyArray<PropertyKey | { key: PropertyKey }> }>;
	  };

class YamlDocumentError extends Error {
	issues: YamlIssue[];
}
```

`YamlValidate` is any synchronous function that returns Standard Schema issues: `engine.safeValidateForm` from
`@schema-engine/forms` fits as it is, and a Standard Schema's `~standard.validate` fits once wrapped.
`YamlDocumentError` is what `parseYaml` throws; its `message` lists one `file:line:column path: message` line per issue.

See [Serialized documents](/docs/schema-engine/serialized-documents#yaml) for recipes and the caveats, and [YAML forms](/docs/forms/yaml) for forms.
