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.
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.
slotsis ordered: a spec lists the slots it declares in that order. A slot or kind outside the protocol is a type error indefineProtocolRecipeSpec(includingslotDefaultsandappearanceAxeskeys) and a thrown error for a hand-written registration.- A rule's
checkreturns the problem, orundefined. A failure readsRecipe 'hero' breaks rule 'section-needs-content': .... - Rules run in
defineProtocolRecipeSpecand, increateRecipeRegistry, on each winning registration of a$type(a recipe overridden later in the list is skipped).bindRecipecopies the spec'sslotSchemasonto the registration, andcreateRecipeRegistrychecks that they list exactly the recipe'sslots, 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).defineProtocolRecipeSpecparses each ofslotDefaultswith its slot schema and fails on an invalid one (Recipe 'hero' slotDefaults.content is invalid: columns: ...). A slot that declaresappearanceAxesmust take anappearancekey in its schema input, and the types reject one that does not (a slot written withstandardSchemastates its input type as the second type argument,standardSchema<Output, Input>). spec.schemais the view schema: a Standard Schema typed by the slots, not a Zod one, so it has no.parseor.shape. Validate withsafeParseStandardSchemafrom@schema-engine/registry. An omitted slot parses from{}, so each slot schema fills its own defaults, and a key outside$typeand the declared slots is an issue at that key. A domain infers the authoring type of a view from a registration withStandardSchemaV1.InferInputof itsschema. A slot schema that validates asynchronously throws where a view is resolved.- The protocol types are
RecipeProtocol,RecipeRuleandRecipeShape(what a rule reads), withSlotOfandKindOfto get the names back from a protocol, andProtocolRecipeSpecandProtocolRecipeRegistrationfor the spec and its binding. These carry theProtocolprefix because@schema-engine/formsexports narrowerRecipeSpecandRecipeRegistrationtypes 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'sappearance(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 ofissuesis aLayoutIssue,{ 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, andRecipeViewModelis 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 builds a card with no forms and binds it for React and Radiant.