Registry

A registry is a collection of registrations keyed by $type. A registration pairs a $type with a schema for its configuration and with metadata, so a node in a document that names a $type can be looked up, validated and handled. The forms engine, the action system and the layout themes all store their pieces in registries of this kind. See Core concepts for the shared vocabulary.

Install

pnpm add @schema-engine/registry zod

@schema-engine/registry has no schema-library dependency. Install the Standard Schema validator you prefer: the examples use zod/mini.

Quick start

1. Define a registration type and build the registry

Extend BaseRegistration with what your kind of registration needs. Here a badge renders itself to a string. createRegistry builds a read-only registry from a list.

import * as z from 'zod/mini';
import { createRegistry, type BaseRegistration } from '@schema-engine/registry';

z.config(z.locales.en());

interface Badge extends BaseRegistration {
	render(config: unknown): string;
}

const badgeSchema = z.object({
	$type: z.literal('badge'),
	label: z.string(),
});

const badge: Badge = {
	$type: 'badge',
	schema: badgeSchema,
	metadata: { name: 'Badge', description: 'A short status label.' },
	render({ label }: z.output<typeof badgeSchema>) {
		return `<span class="badge">${label}</span>`;
	},
};

export const badges = createRegistry('Badge', [badge]);

Zod Mini ships no error messages, so the first lines load the English ones.

2. Resolve a node from JSON

A node arrives as JSON, so its content is unknown until a schema has checked it. safeParseNode looks the $type up in the registry and validates the node with the registration's schema. It returns the registration with the validated configuration, or every issue. A $type that is not registered is an issue, not an exception:

import { safeParseNode } from '@schema-engine/registry';
import { badges } from './registry';

export function resolve(json: string): string {
	const parsed = safeParseNode(badges, JSON.parse(json));
	if (!parsed.success) return parsed.issues.map((issue) => issue.message).join('; ');

	return parsed.registration.render(parsed.data);
}

safeParseNode never throws for a bad node, and it supports synchronous schemas only. Each issue has a path. Pass where the node sits in its document as a third argument and every path starts with it, which is how safeParseTree reports the issues of a whole tree by position.

3. Run it

import { resolve } from './resolve';

console.log(resolve('{ "$type": "badge", "label": "New" }'));
console.log(resolve('{ "$type": "badge", "label": 5 }'));
console.log(resolve('{ "$type": "chip", "label": "New" }'));
<span class="badge">New</span>
Invalid input: expected string, received number
No registration for $type 'chip'.

The first node resolves and renders. The second names a registered $type with a configuration its schema rejects. The third names a $type nobody registered. To resolve a whole tree of nodes and collect every issue with its path, see Resolve a document.

What createRegistry guarantees

function createRegistry<const TRegistration extends BaseRegistration>(
	kind: string,
	registrations: readonly TRegistration[],
	options?: CreateRegistryOptions,
): ReadOnlyRegistry<TRegistration>;
BehaviorDescription
Last winsA later registration with the same $type replaces the earlier one, with no warning (duplicates: 'error' throws instead).
Checked at buildA winning schema without ~standard.validate throws, naming the kind and the registration's $type.
FrozenNo register or clear. The registry, its arrays and its stored copies are frozen. Your objects are untouched.

kind is the label the registry uses in errors. The registry has get, has, all, keys, size and kind. Read-only and mutable registries compares it with BaseRegistry, the mutable form for registrations that arrive at runtime.

The registration types

The types are exactly the ones in the package:

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

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

interface BaseRegistration<_TConfig = unknown, TMeta extends RegistrationMetadata = ComponentMetadata> {
	$type: string;
	schema: StandardSchemaV1;
	metadata: TMeta;
	readonly childPaths?: readonly (readonly string[])[];
}

interface ComponentRegistration<
	TType extends string = string,
	TConfig = unknown,
	TProps = unknown,
	TMeta extends ComponentMetadata = ComponentMetadata,
> extends BaseRegistration<TConfig, TMeta> {
	render: (props: TProps) => unknown;
	$type: TType;
}
  • $type is the stable key. schema is any Standard Schema value, and Zod, Valibot and Effect Schema all qualify. metadata carries the human-readable part.
  • BaseRegistration takes a config type parameter, _TConfig, that the interface does not use today. schema is a plain StandardSchemaV1, so a registration's config type does not constrain it, and its input and output stay distinct.
  • childPaths says where a node of this $type holds child nodes: [['blocks'], ['engine', 'controls']] is node.blocks and node.engine.controls, each one node or an array of nodes. safeParseTree reads it to validate a whole document; createRegistry checks it when it builds.
  • A ComponentRegistration adds a render function whose props you type with the third parameter. A registry that mixes several of them holds render functions of different props, so a kind of your own, as in the quick start, is often simpler.

Where to go next