---
title: 'Recipe protocol'
description: 'Declare the slots, kinds and rules of a domain once, define recipes against them and resolve the view a document selects.'
order: 7
---

# Recipe protocol

A recipe composes slots (a shell, a header, content, and so on) into a view. The spec, the binding, the slot defaults and the resolved view are the same for every domain, so they live in `@schema-engine/layout`. Each domain declares once the slots, kinds and rules its recipes use. A document then selects a recipe by `$type` and fills its slots. Forms is the first domain; this page builds a section domain with no forms in it.

```ts
import * as v from 'valibot';
import * as z from 'zod/mini';
import {
	AppearanceConfigSchema,
	bindRecipe,
	createRecipeRegistry,
	defineRecipeProtocol,
	defineProtocolRecipeSpec,
} from '@schema-engine/layout';

export const sectionProtocol = defineRecipeProtocol({
	slots: ['shell', 'media', 'content'],
	kinds: ['section'],
	rules: [
		{
			name: 'section-needs-content',
			check: (recipe) =>
				recipe.slots.includes('content') ? undefined : `kind 'section' must declare a content slot`,
		},
	],
});

const ContentConfigSchema = v.object({ columns: v.optional(v.number()) });
const MediaConfigSchema = z.object({ ratio: z.optional(z.string()), appearance: z.optional(AppearanceConfigSchema) });

const hero = defineProtocolRecipeSpec(sectionProtocol, {
	$type: 'hero',
	kind: 'section',
	metadata: { name: 'Hero' },
	slotSchemas: { media: MediaConfigSchema, content: ContentConfigSchema },
	slotDefaults: { content: { columns: 2 } },
	appearanceAxes: { media: ['size'] },
});

const HeroShell = (content: string) => `<section class="hero">${content}</section>`;

export const registry = createRecipeRegistry(sectionProtocol, [bindRecipe(hero, { shell: HeroShell })]);
```

The second argument of `bindRecipe` is the render payload, here `{ shell: HeroShell }`. It is whatever your renderers need, and this package never reads it.

- `slots` is ordered: a spec lists the slots it declares in that order. A slot or kind outside the protocol is a type error in `defineProtocolRecipeSpec` (including `slotDefaults` and `appearanceAxes` keys) and a thrown error for a hand-written registration.
- A rule's `check` returns the problem, or `undefined`. A failure reads `Recipe 'hero' breaks rule 'section-needs-content': ...`.
- Rules run in `defineProtocolRecipeSpec` and, in `createRecipeRegistry`, on each winning registration of a `$type` (a recipe overridden later in the list is skipped). `bindRecipe` copies the spec's `slotSchemas` onto the registration, and `createRecipeRegistry` checks that they list exactly the recipe's `slots`, each a Standard Schema; a hand-written registration may leave them out. The registry is the read-only one from `@schema-engine/registry`.
- A slot schema is any Standard Schema that validates synchronously: the example mixes a Zod Mini schema (`media`) and a Valibot one (`content`). `defineProtocolRecipeSpec` parses each of `slotDefaults` with its slot schema and fails on an invalid one (`Recipe 'hero' slotDefaults.content is invalid: columns: ...`). A slot that declares `appearanceAxes` must take an `appearance` key in its schema input, and the types reject one that does not (a slot written with `standardSchema` states its input type as the second type argument, `standardSchema<Output, Input>`).
- `spec.schema` is the view schema: a Standard Schema typed by the slots, not a Zod one, so it has no `.parse` or `.shape`. Validate with `safeParseStandardSchema` from `@schema-engine/registry`. An omitted slot parses from `{}`, so each slot schema fills its own defaults, and a key outside `$type` and the declared slots is an issue at that key. A domain infers the authoring type of a view from a registration with `StandardSchemaV1.InferInput` of its `schema`. A slot schema that validates asynchronously throws where a view is resolved.
- The protocol types are `RecipeProtocol`, `RecipeRule` and `RecipeShape` (what a rule reads), with `SlotOf` and `KindOf` to get the names back from a protocol, and `ProtocolRecipeSpec` and `ProtocolRecipeRegistration` for the spec and its binding. These carry the `Protocol` prefix because `@schema-engine/forms` exports narrower `RecipeSpec` and `RecipeRegistration` types of its own. `assertRecipeRules(protocol, recipe)` is the check both entry points run, for a domain that validates a recipe outside a registry.
- `resolveRecipeView(recipe, view, scope)` resolves the view a document selects, for a winning registration: the recipe defaults laid under the author's slot config, each slot's `appearance` (the scope's inherited picks for the axes the slot declares, the slot's own pick winning, invalid picks reported at `[slot, 'appearance', axis]`), the schema parse, and any key the recipe does not declare. It returns `{ issues, view }`, or `{ issues, schemaIssues }` when the schema rejects the view, so the domain formats schema issues its own way. Each of `issues` is a `LayoutIssue`, `{ message, path }`, with a path relative to the view that the domain lays under the view's own location. It does not change the view it is given. What needs the whole document (a kind that wants a number of steps, a default recipe) stays with the domain.
- `mergeSlotDefaults(view, slotDefaults)` lays a view's slot config over the recipe defaults, deep for plain objects. `readSlotConfig(viewConfig, slot, schema)` reads one slot of a resolved view, and `RecipeViewModel` is the part of a renderer's view model that every domain shares.

Forms declares its protocol in `@schema-engine/forms/recipes`, and the built-in form recipes stay there. [Recipes outside forms](/docs/layout/recipes-outside-forms) builds a card with no forms and binds it for React and Radiant.
