Read-only and mutable registries
Everything a Schema Engine package lets you extend is a registration in a registry: an element, a recipe, an action, an appearance axis, a validation rule. They all share one shape and one mechanism, so overriding a default works the same way for each. A registry is built as read-only from a list, or as mutable when registrations arrive at runtime.
Read-only or mutable
| Question | createRegistry | BaseRegistry |
|---|---|---|
| When are registrations known? | At build, from a list | Any time, after construction |
| Can it change? | No. It is frozen, with no register or clear | Yes: register, clear |
A second $type of one key | Replaces the first silently (or throws with duplicates: 'error'); the key keeps its first position | Replaces it and logs a warning |
| Is the schema checked? | Yes, for each winner, at build | No |
| Use it for | Elements, recipes, actions, axes and validations of an engine | A plugin system that registers at runtime |
createRegistry is the default. Reach for BaseRegistry only when registrations arrive after the registry exists; the forms engine never does.
import * as z from 'zod/mini';
import { createRegistry } from '@schema-engine/registry';
import type { BaseRegistration } from '@schema-engine/registry';
const defaults: BaseRegistration[] = [
{ $type: 'card', schema: z.object({ title: z.string() }), metadata: { name: 'Card' } },
];
const custom: BaseRegistration = {
$type: 'card',
schema: z.object({ title: z.string(), tone: z.string() }),
metadata: { name: 'Tonal card' },
};
export const widgets = createRegistry('Widget', [...defaults, custom]);
export const name = widgets.get('card')?.metadata.name; // 'Tonal card': last winsWhere a repeated $type is always a mistake, such as the component keys of a catalog, pass { duplicates: 'error' } and the build throws Widget 'card' is registered twice. instead of replacing the first registration. The default, 'override', is what [...defaults, custom] relies on.
import * as z from 'zod/mini';
import { createRegistry } from '@schema-engine/registry';
import type { BaseRegistration } from '@schema-engine/registry';
const card: BaseRegistration = { $type: 'card', schema: z.object({ title: z.string() }), metadata: { name: 'Card' } };
export function build() {
// Throws: Widget 'card' is registered twice.
return createRegistry('Widget', [card, card], { duplicates: 'error' });
}Each winning schema must be a Standard Schema, so any provider works. A value without ~standard.validate throws at build and names the registry and the key: Widget 'card' schema must implement Standard Schema (~standard.validate). An overridden registration is not checked.
One contract, a binding per renderer
When one set of $types is drawn by several renderers, define each $type and its schema once as a registry of contracts, and let each renderer add only its part with bindRegistry. The schema is shared by identity, and a renderer that forgets a $type fails to compile.
import * as z from 'zod/mini';
import { bindRegistry, createRegistry } from '@schema-engine/registry';
import type { RegistryBindings } from '@schema-engine/registry';
const contracts = createRegistry('Widget', [
{ $type: 'card', schema: z.object({ title: z.string() }), metadata: { name: 'Card' } },
{ $type: 'badge', schema: z.object({ label: z.string() }), metadata: { name: 'Badge' } },
]);
interface ReactBinding {
render: (props: never) => string;
}
export const reactWidgets = bindRegistry(contracts, {
card: { render: (props: { title: string }) => `<h1>${props.title}</h1>` },
badge: { render: (props: { label: string }) => `<b>${props.label}</b>` },
} satisfies RegistryBindings<typeof contracts, ReactBinding>);
export const radiantWidgets = bindRegistry(contracts, {
card: { tag: 'se-card' },
badge: { tag: 'se-badge' },
});
export const sameSchema = reactWidgets.get('card').schema === radiantWidgets.get('card').schema; // true
export const tag = radiantWidgets.get('badge').tag; // 'se-badge'Leave out badge, add a crad key or set schema in a binding and TypeScript refuses it. With contracts typed only as BaseRegistration, the build throws instead and names the registry and the key: Widget 'badge' has no binding. See the API reference.
The mutable registry
Use BaseRegistry when plugins register themselves after startup:
import * as z from 'zod/mini';
import { BaseRegistry } from '@schema-engine/registry';
import type { BaseRegistration } from '@schema-engine/registry';
interface PluginRegistration extends BaseRegistration {
execute: () => Promise<void>;
}
class PluginRegistry extends BaseRegistry<string, PluginRegistration> {
get registryName(): string {
return 'PluginRegistry';
}
}
const plugins = new PluginRegistry();
plugins.register({
$type: 'sync-cache',
schema: z.strictObject({ enabled: z.boolean() }),
metadata: { name: 'Sync cache' },
execute: async () => {},
});It does not check schemas and it warns on an override, so it suits a small set of known plugins. The actions package supports both: ActionRegistry is read-only, and a BaseRegistry subclass can implement ActionRegistryProvider for registration at runtime.
See the API reference for every member.