---
title: 'Overview and quick start'
description: 'A registry holds the registrations a document can name by $type. Install it, build one and resolve a JSON node through it.'
order: 1
---

# 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](/docs/schema-engine/core-concepts#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](/docs/schema-engine/core-concepts) for the shared vocabulary.

## Install

```bash
pnpm add @schema-engine/registry zod
```

`@schema-engine/registry` has no schema-library dependency. Install the [Standard Schema](https://standardschema.dev/) 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.

```ts title="registry.ts"
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:

```ts title="resolve.ts"
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`](/docs/registry/resolve-a-document) reports the issues of a whole tree by position.

### 3. Run it

```ts title="check.ts"
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" }'));
```

{/* prettier-ignore */}
```html
<span class="badge">New</span>
```

```text
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](/docs/registry/resolve-a-document).

## What `createRegistry` guarantees

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

| Behavior         | Description                                                                                                                  |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| Last wins        | A later registration with the same `$type` replaces the earlier one, with no warning (`duplicates: 'error'` throws instead). |
| Checked at build | A winning schema without `~standard.validate` throws, naming the `kind` and the registration's `$type`.                      |
| Frozen           | No `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](/docs/registry/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:

```ts
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`](/docs/registry/resolve-a-document) 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

- [Read-only and mutable registries](/docs/registry/registries): `createRegistry` against `BaseRegistry`, and overriding a default.
- [Resolve a document](/docs/registry/resolve-a-document): a tree of nodes, a registry per kind, issues with paths.
- [API reference](/docs/registry/api-reference): every export.
