Add field rules and writability
Rules on operation inputs
Section titled “Rules on operation inputs”A capability’s inputs can carry the same rules as fields, and Mica checks them before the host is asked:
| Rule | Applies to | Meaning |
|---|---|---|
required |
any kind | the input must have a value |
requiredWhen |
any kind, inputs only | the input must have a value while a condition holds |
enum |
string, number, integer | the value must be one of the listed ones |
pattern |
string | a regular expression (compiled with the u flag; matches anywhere unless anchored with ^…$); at most 256 characters |
minLength, maxLength |
string | length in Unicode code points |
minimum, maximum |
number, integer | inclusive bounds |
The support ticket’s create uses most of them:
{ "inputs": { "email": { "kind": "string", "required": true, "format": "email", "pattern": "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$" }, "subject": { "kind": "string", "required": true, "minLength": 5, "maxLength": 80 }, "description": { "kind": "string", "required": true, "maxLength": 2000 }, "priority": { "kind": "string", "required": true, "enum": [ "normal", "urgent" ] }, "callbackPhone": { "kind": "string", "requiredWhen": { "$state": "/drafts/wantsCallback", "eq": true }, "pattern": "^[0-9+() -]{7,20}$" } }}How values are judged:
- An input that is absent or
""has no value, and so doesnull— what a cleared NumberField or Choice writes — on an input that is notnullable. It failsrequired(andrequiredWhenwhile its condition holds) and otherwise skips its other checks, so an optional field left empty is fine. - A
stringinput still sends""when it is given one, so an update can clear a field; other kinds sendnullwhennullable, and are otherwise left out. - When a rule fails, the write is blocked, the host is never called, and each
message lands at the same per-field error path a host rejection would use —
…/operations/create/error/fieldErrors/subjectshows must be at least 5 characters.
Import checks literal input values against the rules too, and refuses a
malformed rule (a pattern that is not a valid expression, minLength above
maxLength, a pattern on a number, and so on).
Require an input only sometimes
Section titled “Require an input only sometimes”requiredWhen takes a condition in the same grammar as Show when. The
purchase approval requires a comment only when the approver rejects:
{ "inputs": { "decision": { "kind": "string", "required": true, "enum": [ "approved", "rejected" ] }, "comment": { "kind": "string", "requiredWhen": { "$state": "/drafts/decision", "eq": "rejected" }, "maxLength": 280 } }}To show the Required marker under the same condition, give the Input a $cond
Required value that repeats it:
{ "comment": { "type": "Input", "props": { "label": "Comment for the requester", "placeholder": "Optional when approving", "inputType": "text", "lines": 3, "required": { "$cond": { "$state": "/drafts/decision", "eq": "rejected" }, "$then": true, "$else": false }, "helpText": "Required when you reject. Up to 280 characters.", "value": { "$bindState": "/drafts/comment" }, "error": { "$state": "/meta/objects/request/operations/update/error/fieldErrors/comment" } }, "children": [] }}In the Objects view, each field and input row has a rules button that opens its constraints and, for inputs, Required when, which matches All of or Any of its rows like Show when.
Check a form from the host without sending it
Section titled “Check a form from the host without sending it”A host can run the same rules on demand with the object runtime’s
validate(alias, operation?). It resolves the inputs the document would send
right now, checks them, and returns the failing fields — with no host call and
no state change:
const issues = runtime.validate("ticket", "create");// [{ operation: "create", field: "subject", message: "must be at least 5 characters" }, …]A binding inside a repeated template whose inputs read the row ($item) is
skipped: there is no row to check until its event fires.
The reference host’s Check new customer button is built on validate.
Rules without a business object
Section titled “Rules without a business object”A form that writes ordinary document state does not use this runtime
validate. Give the control Local rules (rules on Input, DateField,
NumberField, Slider or Choice) and dispatch the reserved Check local rules
action (validate, empty params). That action checks only the ruled
controls that are visible on the active page, writes each message to the
control’s error path, and stops the rest of the event if any of those fail.
Hidden controls and other pages keep their error slots. With none visible it
succeeds.
validate is a Mica verb. A host action of that name is refused at import
(Host action “validate” is a reserved Mica action name.). The host must
register createValidateHandler and call
preflightLocalActions(document, handlers) before rendering (as in
embed in a host): if the document
dispatches validate and that handler is missing, it reports
not-implemented.
See Check a form without a business object
and the validate action.
An input’s Required when may be a boolean $expr as the whole condition.
When it already is one, the Objects rules editor shows a Formula · …
summary and Remove only — it has no formula editor. Show when,
Required, Disabled and Busy on a selected component do.
Show a field read-only for some people
Section titled “Show a field read-only for some people”A host can report, per declared leaf field, whether the signed-in person may
edit it. The runtime projects the answer at
/meta/objects/{alias}/fields/{field} as { writable, reason? }; a field the
host says nothing about follows the update capability (writable exactly when
update is implemented and allowed). Bind an Input’s Writable prop to it and
show the reason when it is locked:
{ "email-input": { "type": "Input", "props": { "label": "Billing email", "placeholder": "billing@company.example", "inputType": "email", "required": true, "helpText": null, "value": { "$bindState": "/drafts/customer/contact/email" }, "writable": { "$state": "/meta/objects/customer/fields/contact.email/writable" }, "error": { "$state": "/meta/objects/customer/operations/update/error/fieldErrors/email" } }, "children": [] }, "email-locked": { "type": "Text", "props": { "content": { "$state": "/meta/objects/customer/fields/contact.email/reason" }, "size": "sm", "tone": "muted", "align": "left", "italic": false }, "children": [], "visible": { "$state": "/meta/objects/customer/fields/contact.email/writable", "eq": false } }}The inspector’s Writable → Field writability choice offers each declared field’s path. Writability is a courtesy for the person using the app; the host must still refuse a write it does not allow.