Skip to main content

A form and its guards

When. Before an item enters the selection, the person must answer something (an address), a condition must hold, or the tenant must say yes and give a price.

Steps​

  1. Write the guards in select.guards, an ordered list; each guard is one of: form, require, check, update, permission, policy.
  2. form: what the person fills in on the card. Each field has a type (from the table of types), and required, label, placeholder, enum; multiline: true for a long text; value: "$fields.<field>" prefills it from the card; help: {label, url} adds a link. The answers become the item's fields: $item.fields.<name>.
  3. require: a condition Core reads itself, with a name and a reason (the sentence the person and the agent read).
  4. check: a read operation of the tenant says whether the item is eligible; it receives the item's fields, with its args over them.
  5. update: the tenant sets the price (update: {price: <operation>}) or fields of the line (update: {fields: {status: <operation>.<key>}}), last.
  6. The submit sends what the guards gave: $item.fields.<name>, and $item.price for an update's price.

Core runs them by phase: who chooses, then the form and the conditions, then what calls the tenant. The tenant is never called for an item already refused.

The example​

form-and-guards.yaml
# A form and its guards: before a delivery slot enters the selection, the person answers a form,
# a condition must hold, the tenant checks the address, then quotes the price. Guards run by
# phase — the form and the conditions first, then what calls the tenant — so the tenant is never
# called for a slot already refused. Replace api.example.com by the API.
station:
id: green-grocer
name: Green Grocer

global:
language: en
currency: EUR
locale: en-GB

domain:
domain_type: retail
entity_name: slot
entity_name_plural: slots

voice:
tone: friendly
persona: "a grocer who delivers in town"
rules:
- "Never promise a slot or a price the tool did not give."

apis:
- name: grocer
openapi_url: https://api.example.com/openapi.json
base_url: https://api.example.com
operations:
- {id: listSlots, access: read}
- {id: checkAddress, access: read} # a check is a read: it changes nothing
- {id: quoteDelivery, access: read} # so is a quote
- {id: bookSlot, access: write} # the one write: only a confirmed submit calls it
auth: {type: none}

profiles:
everyone:
when: "true"
grants: [grocer.listSlots, grocer.checkAddress, grocer.quoteDelivery, grocer.bookSlot]

skills:
- name: delivery-slots
cognitive:
objective: offer the delivery slots, for the person to choose one
invoke: {method: listSlots}
kinds: {slot: {}}
present:
fields:
name: {from: label, type: text, role: title}
zone: {from: zone, type: text}
select:
max_items: 1 # one slot: choosing another replaces it
guards:
- form: # what the PERSON answers, on the card
customer_name: {type: text, required: true, label: "Your name"} # never `name`
address: {type: text, required: true, label: "Delivery address"}
note: {type: text, multiline: true, label: "For the courier"}
- name: served-zone # a condition Core reads itself
require: "$item.fields.zone in ['north', 'centre']"
reason: "We do not deliver to this zone yet."
- check: checkAddress # the tenant says yes or no, in its own words
args: {body.address: "$item.fields.address"}
- update: {price: quoteDelivery} # the tenant quotes THIS delivery's price, last
args: {body.address: "$item.fields.address"}
submit:
- name: book
mode: request
tool: bookSlot
args:
body.slot: "$item.ref"
body.customer_name: "$item.fields.customer_name"
body.address: "$item.fields.address"
body.note: "$item.fields.note"
body.price: "$item.price" # the quoted price leaves with the line
body.idempotency_key: "$action.key"
render: {component: cards}

submit_guard:
confirm: true

The pitfalls​

  • Do not call a form field name. It overwrites the item's displayed name: the line would show the customer's name instead of the slot. Write customer_name. Core does not refuse it.
  • $item.fields.<name>, never $intake.<name>. $intake is not a source: it would reach the tenant as that text. The args of a check or an update are a literal, $params.<name> or $item.fields.<name>.
  • An update: {price: …} without $item.price in the submit is refused: the person would see a price the tenant never receives.
  • No $item.price in a condition beside an update (it would read the price from before the quote), and no $item.quantity.
  • A literal of the field's type: $item.fields.zone == 149 on a text field is refused; write '149'. An absent value is null: != and not in are true for it.
  • A check or an update calls a read. A write operation called when an item is chosen is refused.
  • At most one form and one update per list. update.fields cannot target name, price, image_url, code, slug or id.
  • multiline goes on a text only; value only in the form of a select guard (not the submit_guard's).
  • A line that carries a filled form counts one: its quantity is 1.

Reference: Select guards, Conditions.