Schema referenceMIT
The structure of a form schema, from form settings and pages to the properties every element and field accepts, and how answers map to the data object.
A form is described by a FormSchema: a plain, JSON-serializable object. This page covers the structure that is shared by every form. The properties of each field type are on Field types.
Form settings#
A form has either pages, for a multi-page form, or elements, for a single page. A schema with elements is treated as one page named page1.
import type { FormSchema } from '@birchui/forms';
const equipmentCheckout: FormSchema = {
title: 'Equipment checkout',
description: 'Borrow a laptop, projector or test kit from the Harbour Street depot.',
submitText: 'Request equipment',
completedMessage: 'Your request is in. Collect the equipment from the depot desk.',
elements: [
{ type: 'text', name: 'employeeId', title: 'Employee ID', required: true, pattern: 'E[0-9]{5}', patternMessage: 'Use the format E12345.' },
{ type: 'select', name: 'item', title: 'Item', required: true, choices: ['Laptop', 'Projector', 'Gas detector', 'Thermal camera'] },
{ type: 'date', name: 'collectOn', title: 'Collect on', required: true, width: 'half' },
{ type: 'date', name: 'returnBy', title: 'Return by', required: true, width: 'half' },
],
};| Setting | Effect |
|---|---|
title, description |
Shown above the form. The title also labels the <form> for assistive technology. |
locale |
The default language for translated text and built-in messages, unless the locale option overrides it. |
showProgressBar |
Shows a progress bar on multi-page forms. On by default; set false to hide it. |
showPageTitles |
Shows each page's title as a heading. On by default. |
nextText, prevText, submitText |
Button labels. They default to the built-in messages "Next", "Back" and "Submit". |
completedMessage |
Shown in place of the form after a successful submission. Without it, the form stays on screen. |
Pages#
Page names must be unique. A page with visibleIf is skipped while its condition is false, and its fields are neither validated nor submitted. See Multi-page forms.
Elements#
An element is a field, which holds an answer, or a layout element:
- Fields:
text,email,number,textarea,select,multiselect,radio,checkbox,checkboxgroup,toggle,date,time,datetime,rating,slider,file,signature,matrix,hiddenandcomputed. - Layout:
markdown(a block of formatted text),panel(a group of fields) anddynamicpanel(a repeating section).
The lists are exported as FIELD_TYPES and ELEMENT_TYPES for tooling.
Properties of every element#
width lays out elements side by side in a row: 'half', 'third' and 'two-thirds' combine as you would expect, and 'full' is the default. When the form itself is narrower than 540 pixels, for example on a phone or in a sidebar, every element takes the full width.
Properties of every field#
| Property | Effect |
|---|---|
name |
The key of the answer in the data object. Required, and unique across the whole form. |
title |
The visible label. Without a title, the name is used. |
description |
Help text below the label, announced together with the field. |
placeholder |
Placeholder text for text inputs and the empty option of a dropdown. Use it for examples, not instructions. |
defaultValue |
The initial value, used when data does not contain one. |
required |
The field must be answered. For a checkbox or toggle, it must be on. |
requiredIf |
An expression that makes the field required while it is true. |
requiredMessage |
Replaces the "This field is required." message for this field. |
readOnly |
Shows the value but prevents changes. |
visibleIf |
Shows the field only while the expression is true. |
enableIf |
Enables the field only while the expression is true. |
validators |
Extra validation rules. |
Names and the data object#
Names start with a letter or underscore and contain only letters, digits, _ and -, such as contactEmail or serial_no. Each answer is stored under its field's name:
form.getData();
// {
// employeeId: 'E20417',
// item: 'Thermal camera',
// collectOn: '2026-11-30',
// returnBy: '2026-12-04'
// }- Panels group fields visually but do not nest the data. Fields inside a
panelwrite to the same object as the fields around it, so a panel's fields share the form-wide namespace. - Repeating sections write an array of objects under their own name, for example
{ dependants: [{ name: 'Ava', dateOfBirth: '2019-04-02' }] }. Field names only need to be unique within one entry. - Empty answers are removed: clearing a field, or setting it to
''ornull, deletes its key. - Hidden questions are left out of the submitted data by default. See
clearHiddenValues.
Values use plain JSON types, except file fields, which hold File objects. The value type of each field is listed on Field types.
Choices#
Choice fields (select, multiselect, radio, checkboxgroup) take a choices list, and matrix questions take rows and columns lists in the same format. Each entry is a string, a number, or an object:
{
type: 'radio',
name: 'coverLevel',
title: 'Cover level',
choices: [
{ value: 'basic', text: 'Basic: accidental damage only' },
{ value: 'plus', text: 'Plus: accidental damage and theft' },
{ value: 'premium', text: 'Premium: worldwide cover', visibleIf: "{region} = 'EU'" },
],
}The value is stored in the data; the text is shown. A choice with visibleIf disappears while its condition is false.
Translatable text#
Every user-facing text, including title, description, placeholder, choice texts and button labels, accepts either a string or an object of translations:
{ type: 'date', name: 'dateOfLoss', title: { en: 'Date of loss', de: 'Schadendatum', fr: 'Date du sinistre' } }See Translations.
Check a schema#
validateSchema(schema) reports problems in a schema before you render it, for example in a CI test or when saving from an editor. It returns a list of issues, each with a path, a message and a severity:
import { validateSchema } from '@birchui/forms';
const issues = validateSchema(equipmentCheckout, { functions: ['businessDaysBetween'] });
for (const issue of issues) console.log(`${issue.severity} at ${issue.path || 'form'}: ${issue.message}`);It checks unknown element types, missing or duplicate names, names that do not follow the naming rule, choice fields without choices, matrices without rows or columns, computed fields without an expression, min greater than max, invalid pattern regular expressions, and every expression: syntax errors, unknown functions and references to fields that do not exist. Pass the names of your custom functions in functions so they are not reported. See Validation for more.
Working with schemas in code#
| Export | Purpose |
|---|---|
normalizeSchema(schema) |
Returns the schema with elements turned into a single page and page names filled in. |
walkSchema(schema, visit) |
Visits every element of a normalized schema, with its page index, path and parent. |
collectFieldNames(schema) |
The names of all fields of a normalized schema outside repeating sections, including fields inside panels. |
NAME_PATTERN |
The regular expression names must match. |
Change a rendered form's schema with form.setSchema(schema) or form.update({ schema }). The current data is kept, default values are applied to new fields, and the form returns to its first page.