Form engine setup

createFormEngine is the public entry point for assembling a form engine. Pass declarative registration arrays; the factory returns an immutable FormEngine.

Register capabilities

The engine has three independent registries:

  • Elements render fields and static content inside a step.
  • Recipes compose the form shell and optional header, progress, and action blocks.
  • Actions respond to form and step lifecycle triggers.
import { createFormEngine } from '@schema-engine/forms';

const engine = createFormEngine({
	elements: elementRegistrations,
	recipes: recipeRegistrations,
	actions: actionRegistrations,
});

Registration IDs are the $type values used in serialized configuration. Each list is stored in a read-only registry from @schema-engine/registry. A registration whose schema is not a Standard Schema throws when the engine is built, naming its $type. Only the winning registration of each $type is checked. The engine stores a frozen shallow copy of each winner, taken when it is built. Your objects are left untouched, but changing or spying on them afterwards does not reach the engine: spy or patch before building, or build a new engine. Duplicate $type values follow last-wins semantics, with no warning. Spread shared barrels and append overrides:

const engine = createFormEngine({
	elements: [...schemaFormRegistrations, customInput],
});

Use engine.extend({ actions: [extraAction] }) to derive a new engine without re-spreading the full registration lists.

Register a custom field

Pass the schema directly. defineInput checks that the component accepts schema output (defaults and transforms applied). Form authors still write schema input, so defaulted properties stay optional in defineForm.

import { defineInput, createFormEngine } from '@schema-engine/forms';
import * as z from 'zod/mini';

const RatingSchema = z.object({
	$type: z.literal('rating'),
	id: z.string(),
	name: z.string(),
	max: z._default(z.number(), 5),
});

function RatingField(config: { name: string; max: number }) {
	return <input name={config.name} type="range" max={config.max} />;
}

export const RatingRegistration = defineInput({
	$type: 'rating',
	schema: RatingSchema,
	metadata: { name: 'Rating', category: 'input' },
	render: RatingField,
});

const engine = createFormEngine({ elements: [RatingRegistration] });

engine.defineForm({
	id: 'review',
	steps: [
		{
			id: 'main',
			title: 'Review',
			elements: [{ $type: 'rating', id: 'score', name: 'score' }],
		},
	],
});

Register a container

Containers declare children with elementChildrenSchema. Nested authoring uses the same engine registrations as top-level fields. Field paths are a render-time scope: pass namePrefix / instanceId to FormElements instead of cloning child configs.

import { defineInput, elementChildrenSchema, objectFieldScope } from '@schema-engine/forms';
import { FormElements } from '@schema-engine/forms-react/primitives';
import * as z from 'zod/mini';
import { BaseFieldSchema } from '@schema-engine/forms/schemas/base-field';

const GroupSchema = z.extend(BaseFieldSchema, {
	$type: z.literal('group'),
	elements: elementChildrenSchema.check(z.minLength(1)),
});

function GroupField(config: {
	name: string;
	elements: ReadonlyArray<import('@schema-engine/forms').NormalizedElementConfig>;
	instanceId?: string;
}) {
	const scope = objectFieldScope(config.name, config);
	return <FormElements elements={config.elements} namePrefix={scope.namePrefix} instanceId={scope.instanceId} />;
}

export const GroupRegistration = defineInput({
	$type: 'group',
	schema: GroupSchema,
	metadata: { name: 'Group', category: 'input' },
	render: GroupField,
});

Arrays use arrayItemScope(name, index, instanceId) and pass instanceId — a stable item id, not the current index — so ids survive item removal. Scalar array items set { scalar: true } so the child binds to the item path itself.

React engine

React apps use createFormEngine from @schema-engine/forms; it binds no application field components or visual recipes: install Schema Engine registry items and pass their generated registrations explicitly.

import { createFormEngine } from '@schema-engine/forms';
import { schemaFormRegistrations } from '@/components/schema-engine/forms/registrations';
import { schemaFormRecipeRegistrations } from '@/components/schema-engine/forms/recipes/registrations';

const engine = createFormEngine({
	elements: schemaFormRegistrations,
	recipes: schemaFormRecipeRegistrations,
});

Radiant uses the same composition model for elements: register them explicitly. createRadiantFormEngine() already binds the canonical view recipes because Radiant has no shadcn registry. Specs still come from @schema-engine/forms/recipes/built-in.

Author form configuration

When configuration is authored in TypeScript rather than loaded as JSON, pass a typed object to engine.defineForm():

const form = engine.defineForm({
	id: 'contact',
	title: 'Contact',
	view: { $type: 'card', shell: { appearance: { size: 'default' } } },
	steps: [
		{
			id: 'details',
			title: 'Contact details',
			elements: [
				{
					$type: 'input',
					id: 'email',
					name: 'email',
					label: 'Email',
					inputType: 'email',
				},
			],
		},
	],
});

defineForm validates against the engine's registered elements, recipes, and actions. It applies schema defaults and transforms and returns a frozen NormalizedFormConfig. Use engine.validateForm() for untyped or external JSON. Use a plain FormConfig when configuration must remain portable or editable by another system, then pass it to validateForm or to FormRenderer (which validates at the form boundary).

Composition rules

There is one view recipe per form. The recipe is selected at the form level:

const config: FormConfig = {
	id: 'contact',
	steps: [{ id: 'details', title: 'Contact details', elements: [] }],
	view: {
		$type: 'card',
		shell: { appearance: { size: 'default' } },
	},
};

Do not place shell, header, progress, or action configuration on individual steps. Wizard recipes are for multi-step forms; single-step recipes cannot declare a progress slot, but they do render a submit-only actions bar. See View recipes for the available compositions and custom recipe API.