Document format
The envelope
Section titled “The envelope”serializeDocument always writes, and parseDocument accepts, a versioned
envelope:
{ "envelopeVersion": 1, "ui": { "root": "app", "elements": { "app": { "type": "PageStack", "props": { "name": "My app" }, "children": ["page-1"] }, "…": {} }, "state": {} }, "meta": { "pack": { "id": "mica.core", "version": "1.0.0" } }, "objects": {}, "hostActions": {}, "lifecycle": { "startup": [] }}| Member | Required | Contents |
|---|---|---|
envelopeVersion |
yes | The number 1. Any other number is refused: Document requires Mica envelope version “2”; this editor supports version 1. |
ui |
yes | The json-render spec: root, elements, and optional state. Nothing else. |
meta |
no | { "pack": { "id", "version" } } — nothing else. Omitted means Signal (mica.core). |
objects |
no | Business-object declarations, keyed by alias. Written only when present. |
hostActions |
no | Named host verbs the host lists, keyed by name, each { "params": { <name>: field spec } }. Written only when present. See actions. |
lifecycle |
no | { "startup": [ …fetch actions ] }. Written only when present. |
Any other top-level member is refused by name — including fixtures and
graph, which belong in the {stem}.mica-design.json sidecar,
never in the envelope. root, elements or state at
the top level of an envelope is refused with a hint to move it inside ui.
Pre-envelope documents
Section titled “Pre-envelope documents”A document with root, elements, state and meta at the top level and no
envelopeVersion is still accepted; exporting it again writes an envelope. It
may not carry ui, objects, hostActions or lifecycle — those need
"envelopeVersion": 1.
Elements
Section titled “Elements”ui.elements is a flat map from element key to element:
"rsvp-submit": { "type": "Button", "props": { "label": "Count me in", "variant": "solid", "size": "md", "disabled": false, "fullWidth": false }, "children": [], "on": { "press": { "action": "setState", "params": { "statePath": "/guest/attending", "value": true } } }, "visible": { "$state": "/guest/name" }}| Field | Required | Contents |
|---|---|---|
type |
yes | A component of the document’s pack. See components. |
props |
yes | An object that passes the component’s schema. Every prop the schema does not know is refused by name. |
children |
yes | An array of element keys, possibly empty. |
on |
no | Event bindings: press on a Button, commit on a form control (Input, NumberField, Choice, Toggle, DateField, DateRange, Slider); no other component declares an event. See actions. |
visible |
no | A condition. Not allowed on the root or on pages. |
repeat |
no | Stack and Grid only: { "statePath": "/path", "key": "field" }. Renders the children once per row of the list. See lists. |
watch and slots — real json-render fields — are refused as not
enabled in this version of Mica. Any other element field is refused as
unsupported. Element keys may not be __proto__, prototype or constructor.
Structure rules
Section titled “Structure rules”ui.rootnames an existing element whose type isPageStack.- The root’s children are all
Pageelements, and there is at least one. - Every child key exists, and no element is the child of two parents.
- There are no cycles, and every element is reachable from the root.
- Every parent-child pair is one the designer itself could build: a
Pagesits only directly in the rootPageStack, aPageStackis only ever the root, and only components that accept children have any (the components reference says which). A Text holding a Button, a Page inside a Section and a PageStack inside a Page are all refused. The designer’s drag-and-drop follows the same rule.
ui.state is a JSON object: the starting values of the document’s state. Keys
are held to the state path rules,
state may nest at most 12 levels, and once the document declares a business
object it may not seed that object’s /objects/{alias} or
/meta/objects/{alias} subtree.
Round trip
Section titled “Round trip”Import rebuilds every member it accepts, so exporting a document that
parseDocument returned and importing it again gives back the same document. A
document with no meta gains meta.pack for Signal on the way. Props are written in the order their component schema lists
them, and business-object declarations keep the order the document gave. The
example documents are stored exactly in exported form,
and a test keeps them that way.
Using the functions
Section titled “Using the functions”import { MICA_ENVELOPE_VERSION, listDocumentIssues, parseDocument, serializeDocument } from "mica/runtime";
const result = parseDocument(text);if (result.ok) { result.spec; // MicaDocument: root, elements, state?, meta, objects?, lifecycle? result.pack; // the MicaPack named by meta.pack const again = serializeDocument(result.spec); // pretty-printed envelope, 2-space indent} else { result.error; // one message naming the first problem}parseDocument reports the first problem it finds. To see them all at
once, call listDocumentIssues(text): it returns every problem, in the order
import checks them, and an empty list when the document imports. Its first
entry’s message is always exactly the error parseDocument returns.
for (const issue of listDocumentIssues(text)) { issue.message; // the same text parseDocument would report issue.kind; // "document" | "element" | "props" | "structure" | "placement" // | "objects" | "lifecycle" | "interactions" issue.elementKey; // the element it is about, when it is about one issue.path; // a JSON Pointer into the document as written, // e.g. "/ui/elements/rsvp-name/props/value"}Some checks only make sense once earlier ones pass, so a fix can still reveal more: nothing past a broken envelope or pack is checked; reachability waits until every element is readable; and actions, bindings, conditions and state are checked only once every element, the element tree and every object declaration are sound (a refused prop or a misplaced child does not hold them back). An object declaration reports its first problem only.