Form document schema
Overview
A form document is one JSON object that holds a whole form: its settings, sections, fields, logic,
confirmations, notifications and PDF documents. The REST API returns it (GET /V1/webforms/forms/{formId}/document,
the version and draft documents) and accepts it (form creation, a draft's replace-document). The form export file
in the admin has the same structure: the API document is that export plus schema_version, form_id and the
ids of fields, sections and logic rules (MageMe\WebForms\Model\Service\FormDocument\Exporter).
The structure is described by a JSON Schema, draft 2020-12, with $id
https://mageme.com/schemas/webforms/form-document-4.0.json. It comes in two places:
| Source | What it is |
|---|---|
schema/form-document-4.0.json in the WebForms module | A snapshot recorded by the module's integration test on MageMe's build install. It includes the field types of the add-ons installed there |
GET /V1/webforms/schema/document | The schema of the store you call, as a JSON string in schema_json, with schema_digest. See REST: Schema |
Both are generated from the same field-type catalog (MageMe\WebForms\Model\Service\FormDocument\SchemaGenerator),
so the live one reflects the add-ons installed on that store. Some settings have enums built from the store's own
data: source_attribute of Autocomplete and Product fields lists the store's catalog
attributes, and agreement_id of Terms lists its checkout agreements, so those three definitions differ from
the shipped file. Validate against the live schema of the store you write to.
Top-level keys
| Key | Type | Notes |
|---|---|---|
schema_version | "4.0" | Optional on write; a missing value is read as 4.0 with a warning, another value is refused |
form_id | integer, read-only | Present in documents the API returns |
name | string | The only required key. At most 255 characters |
code, url_key, meta_title, and every other form setting | per setting | One property per setting of the form, with x-class and x-translatable (see below). The old form-level title is gone; send the text as meta_title |
submitter_email_field_code, publish_title_field_code | string or null | Name a field by its code. Preferred over the matching *_field_id |
fields | array | Fields outside any section. Each item matches one field-type definition |
fieldsets | array | Sections: name (required), fieldset_id (read-only), their settings and their own fields |
logic | array | Rules: logic_condition and action required, and the trigger field by field_code (preferred) or field_id; aggregation, value, target, is_active |
store_data | object | Per store view code, the keys that differ from the default |
confirmations | array | What the visitor sees after submitting. name and type (message, redirect, page) required; checked in order, the default is the fallback |
notifications | array | name and trigger_code required. A notification arrives switched off; is_active is read-only. REST: Notifications describes the node |
defaults, defaults_locale | object, string; read-only | API only. In a document the API returns, the form and every section, field and confirmation carry defaults: for each translatable text left empty, the storefront phrase shown in its place, in the language of defaults_locale. %1 stays a placeholder. Dropped on write |
pdf_documents, pdf_bindings | array | PDF documents (template_ref, template_contract, slots, store_slots, page_policy, …) and which document prints for admin_print and customer_print. Both are empty lists while the form holds only the documents it was created with, unchanged (MageMe\WebForms\Model\Pdf\Document\PdfPortable). See PDF templates |
The schema still lists appearance_* keys besides appearance_max_width. They are accepted and ignored: those
looks are store view settings, set with REST: Appearance.
Field definitions
$defs holds one definition per field type, named field_<type>, for example field_email. Each requires type
(a constant) and name, and lists that type's settings. A field's code follows the rule fields are stored by: up to
255 bytes, starting with a letter, then letters, digits, underscores, hyphens and colons; a code of digits only is
refused. Options of list fields travel in choices: a list of {text, value, …} records and
{kind: "group", text} headers.
Annotations
| Annotation | Meaning |
|---|---|
readOnly: true | Ids (form_id, fieldset_id, logic_id) and state the API returns but doesn't take, such as a notification's is_active |
x-class: plain | An ordinary setting |
x-class: side_effect | A setting whose change reaches outside the form, such as redirect_url, captcha_mode, upload limits and cleanup periods |
x-translatable: true | The value can differ per store view in store_data |
description | Kept on the keys where the meaning isn't obvious, such as uuid ("Stable identity within the form; kept on import when free") |
The x- keywords are annotations, not validation keywords. A validator in strict mode, such as Ajv 8 by default,
refuses unknown keywords, so turn strict mode off or declare them.
What the server checks on top
The JSON Schema is permissive: it sets no additionalProperties, so it accepts keys the form doesn't have. The
server's own check (MageMe\WebForms\Model\Service\FormDocument\Validator) runs on every document the API
receives, at form creation and at a draft's replace-document, and is stricter. A refused document gets 400
VALIDATION_FAILED, "The document is not valid.", with the errors listed:
| Rule | Error text |
|---|---|
A key that is not a form setting, column or envelope key, title included | Unknown form key "title". |
| An unknown key on a section, field or logic rule | fieldsets[0]: unknown key "…". |
name of the form, a section or a field present and at most 255 characters | form.name is required. |
A field code that is an identifier of at most 100 characters | ….code must be an identifier of at most 100 characters. |
| An unknown field type | fields[0]: unknown field type "…". |
Logic targets and *_field_code references that point at fields or sections of the same document | … does not reference a field of this document. |
Each notification, PDF document and binding uuid once | notifications[1].uuid duplicates notifications[0]. |
A setting of class secret | ….may not be set through the API. |
| A setting's value of the wrong type or outside its list | ….key: "x" is not one of a|b., ….key must be an integer., ….key must be 0 or 1. |
Internal state, such as is_draft or deleted_at, with a value | ….is_draft is internal state and cannot be set. |
An unsupported schema_version, or a document too large | Unsupported schema_version "…" (expected 4.0)., The document exceeds … bytes. |
Before the check, read-only keys that an export writes are dropped, so a document read from the store can be sent
back. A null or an empty string means "not set" to the server.
Validating a document from the store
A document read from GET /V1/webforms/forms/{formId}/document doesn't validate against the JSON Schema as it is.
On a test store, both documents tried (a 14-field form and a 5-field form) failed with Ajv and with Python
jsonschema, for four reasons:
| What the document holds | What the schema expects |
|---|---|
null for settings that are not set, for example width_proportion_lg, pii_class, submit_button_position | A string or integer from the enum |
[] for an empty object: store_data, a confirmation's store_content, a notification's trigger_filter, reply_to_policy and attachment_policy, a PDF document's store_slots | An object |
"1" for a logic rule's is_active | An integer or boolean |
0 for is_success_session_displayed | A string or null |
To check your own documents with a schema validator, first drop null values, turn those empty lists into {},
send a rule's is_active as 1 and is_success_session_displayed as a string. The server doesn't apply these
four schema mismatches; it checks values with its own rules above, and those decide whether a write is accepted.
import Ajv2020 from 'ajv/dist/2020.js';
import addFormats from 'ajv-formats';
const { schema_json } = await getJson('/V1/webforms/schema/document'); // your signed REST call
const ajv = new Ajv2020({ strict: false, allErrors: true });
addFormats(ajv);
const validate = ajv.compile(JSON.parse(schema_json));
if (!validate(myDocument)) console.log(validate.errors);
What's next
- REST: Schema: read the schema and the settings catalog of a store.
- REST: Forms: read a form's document and create a form from one.
- REST: Drafts: replace a draft's document and validate it before publishing.
- Importing and exporting forms: the same document as a file in the admin.