YAML forms

A form config is plain data, so it can live in a .form.yaml file that people without TypeScript can edit. The @schema-engine/registry/yaml subpath parses the file, hands the value to your engine to validate, and turns every problem into a file, line and column.

A form is a document, so the YAML parsing, the errors and the caveats are the ones described there. This page covers what is specific to forms.

Install

yaml is an optional peer dependency of @schema-engine/registry. Install it in the app that imports the subpath:

pnpm add @schema-engine/forms @schema-engine/registry yaml zod

A form file

# contact.form.yaml
id: contact
steps:
    - id: details
      title: Your details
      elements:
          - $type: input
            id: name
            name: name
            label: Name
            grid: { width: half }
          - $type: input
            id: email
            name: email
            label: Email
            grid:
                width: half

Parse and validate

Pass the text and a validator. engine.safeValidateForm returns the Standard Schema issues parseYaml expects, so it is the validator as it is:

import { parseYaml } from '@schema-engine/registry/yaml';
import { engine } from './engine';

export function parseForm(text: string, filename: string) {
	return parseYaml(text, (value) => engine.safeValidateForm(value), { filename });
}

The result is the same frozen NormalizedFormConfig that engine.validateForm returns for the equivalent JSON. parseYaml throws a YamlDocumentError when the text is not valid YAML or the form does not validate; safeParseYaml takes the same arguments and returns { success: true, data } or { success: false, issues }.

Show errors with the file and line

A bad form is reported as a YamlDocumentError with one file:line:column path: message line per issue, as described in Serialized documents. With width: halve on line 17, the message reads:

contact.form.yaml:17:24 steps[0].elements[1].grid.width: Expected one of full, three-quarters, ...

The path is the form's config path. A missing id points at the element it belongs to, since the key itself is absent. Use safeParseYaml to render the issues yourself, for example in a form builder UI.

Editor autocomplete

Editors with the YAML language server (VS Code's YAML extension, Zed, Neovim) complete keys and flag unknown values from a JSON Schema named in a header comment. The engine generates that schema from what it has registered:

import { writeFile } from 'node:fs/promises';
import { engine } from './engine';

const schema = engine.generateFormConfigSchema({ io: 'input', target: 'draft-7' });
await writeFile('contact.schema.json', JSON.stringify(schema, null, 2));

The schema is generated from the authored side (io: 'input', the default), so fields with defaults stay optional. Use target: 'draft-7' because the YAML language server only learned JSON Schema 2019-09 and 2020-12 in version 1.20.0; an editor extension that bundles an older server reads the schema as draft-07, which every version supports. Then name the schema at the top of the YAML file:

# yaml-language-server: $schema=./contact.schema.json
id: contact

Regenerate the schema when you register another element, recipe or action.

Write a form as YAML

stringifyYaml writes a config back out, for a form builder or a migration. A short map of scalars under a key stays on one line (grid: { width: half }) and every other collection is written in block style. Pass schemaUrl to add the language-server header:

import { stringifyYaml } from '@schema-engine/registry/yaml';
import { engine } from './engine';

const form = engine.validateForm({
	id: 'contact',
	steps: [{ id: 'details', title: 'Details', elements: [] }],
});

export const text = stringifyYaml(form, { schemaUrl: './contact.schema.json' });

Caveats

The YAML rules are the ones in Serialized documents: YAML 1.2, quoting of values such as 1.10, one document per file, no tags and no loader. Anchors and merge keys let a field share another's options:

id: contact
steps:
    - id: details
      title: Details
      elements:
          - &input
            $type: input
            id: name
            name: name
            grid: { width: half }
          - <<: *input
            id: nickname
            name: nickname

An issue inside merged values points at the map that merges them, and a merge source that is not a map (<<: 5) is reported at line 1, column 1.