Skip to content

Schema and fields ​

A form is a plain JSON object of type FormSchema. Because it is only data, you can store it in a database, version it, and edit it with the visual builder.

The form ​

ts
interface FormSchema {
  id: string; // used for DOM ids
  version: 1; // schema format version, for future migrations
  title: I18nText;
  description?: I18nText;
  fields: FieldSchema[];
  steps?: StepSchema[]; // turns the form into a wizard
  submitLabel?: I18nText;
}

Common field options ​

Every field has these options:

OptionTypeDescription
namestringKey in the value object. Must be unique among its siblings.
typeFieldTypeSee the table below.
labelI18nTextShown above the input.
descriptionI18nTextHelp text under the label.
placeholderI18nTextPlaceholder of text-like inputs.
requiredboolean | Conditiontrue, or a condition that makes it needed.
visibleWhenConditionShow (and validate) the field only when this holds.
validationsValidationRule[]See Validation.
defaultValuedepends on the typeInitial value.
colSpannumberLayout hint: columns out of 12. Fields are full width by default.

Field types ​

typeValueExtra options
text, email, textareastring—
numbernumberstep
datestring (YYYY-MM-DD)—
select, radiostring | numberoptions (required)
checkboxArray<string | number>options (required)
switchboolean—
arrayArray<object>fields (required), itemLabel

Options are { label: I18nText, value: string | number }.

array fields are repeatable groups. See Steps and repeatable groups.

Translatable text ​

Every user-facing string is an I18nText: either one string, or one string per locale.

json
{ "label": { "zh-TW": "電子郵件", "en": "Email" } }

The built-in locales are zh-TW (the default) and en. Pass locale="en" to <SchemaForm> to switch. Built-in validation messages exist for both locales.

Checking a schema ​

Mistakes in a hand-written schema are easy to make. checkSchema reports them before you render:

ts
import { checkSchema } from '@schema-form-kit/core';

const issues = checkSchema(schema);
// [{ path: 'fields.2.visibleWhen', message: 'Unknown field "topc" in condition' }, ...]

It finds invalid or duplicate names, conditions that point at fields that do not exist, empty option lists, invalid regular expressions and steps that reference missing fields. The playground and the builder run it as you type.

Released under the MIT License.