Require a condition
When. An item may be chosen only if something is true: stock is left, the person answered a field, a value is in range. Core decides it itself, before it calls the tenant, whatever the model says.
Steps
- Present what the condition reads. A condition reads the item's fields: declare each one in
present.fields(from the tenant's row), with its type —integerfor a count,pricefor an amount,textfor a word. - To require an answer from the person, ask it in the
formguard withrequired: true. A form field may also carryhelp: {label, url}(a link under it),multiline: true(a long text),enum(a closed list). - Write the condition in a
requireguard, after the form it reads:name(how the refusal is called),require(the condition),reason(the sentence the person reads). - To apply a guard to some items only, add
when:— the guard runs only when it holds. - Check the config. A refused choice says the
reason, and the item does not enter.
The example
require-a-condition.yaml
# Require a condition: an item enters the selection only when a condition holds. A form field is
# required of the person (`required: true`); a `require` guard reads the item's fields and refuses
# with its own sentence; `when` applies a guard to some items only. Core reads the conditions
# itself, before it calls the tenant. Replace api.example.com by the API.
station:
id: bike-rental
name: Bike Rental
global:
language: en
currency: EUR
locale: en-GB
domain:
domain_type: retail
entity_name: bike
entity_name_plural: bikes
voice:
tone: friendly
persona: "a bike renter who knows every bike of the shop"
rules:
- "Never say a bike is booked before the submit returned it."
apis:
- name: rental
openapi_url: https://api.example.com/openapi.json
base_url: https://api.example.com
operations:
- {id: listBikes, access: read}
- {id: bookBike, access: write}
auth: {type: none}
profiles:
everyone:
when: "true"
grants: [rental.listBikes, rental.bookBike]
skills:
- name: bikes
cognitive:
objective: show the bikes to rent, for the person to choose one
invoke: {method: listBikes}
kinds: {bike: {}}
present:
fields:
name: {from: name, type: text, role: title}
price: {from: price, type: price, role: billed}
available: {from: available, type: integer} # bikes left: what the rule reads
category: {from: category, type: text, role: badge} # city, mountain, electric
select:
max_items: 1
guards:
- form: # what the PERSON answers, on the card
rider_height: {type: integer, required: true, label: "Your height (cm)"} # required
licence: {type: text, label: "Licence number (electric bikes)",
help: {label: "Why a licence?", url: "https://example.com/e-bike-rules"}}
- name: in-stock # always: the bike must be left
require: "$item.fields.available > 0"
reason: "This bike is all rented out today."
- name: tall-enough # a condition on what the person answered
require: "$item.fields.rider_height >= 150"
reason: "This bike is for riders of 150 cm and more."
- name: licence-for-electric # `when`: this guard applies to electric bikes only
when: "$item.fields.category == 'electric'"
require: "$item.fields.licence != null"
reason: "An electric bike needs a licence number."
submit:
- name: book
mode: request
tool: bookBike
args:
body.bike: "$item.ref"
body.rider_height: "$item.fields.rider_height"
body.licence: "$item.fields.licence"
body.idempotency_key: "$action.key"
render: {component: cards}
submit_guard:
confirm: true
The pitfalls
- A condition reads what the item carries, nothing else:
$item.fields.<name>(presented, or asked by a form placed before it),$item.price,$item.ref,$item.kind. Never$identity…: who may choose is a profile or apermission. - Compare a field with a value of its type:
available > 0on aninteger,zone == '149'on atext. - An absent value equals
null:!=andnot inare true for it. To require a value, write$item.fields.licence != null. - What cannot be known fails closed: a
requirenot known to hold refuses; awhenthat cannot be known applies its guard. - No
$item.pricebeside anupdate: the quote comes last; judge the priceat: submitin a station policy. - A condition used in several skills is written once: Reuse a policy.
What Core says, and the fix
Core refuses the config at the check, and says why. Each sentence below is Core's own, as it answers today (a test keeps them true):
- "reads $item.fields.colour, which is unknown — declare it in
present.fieldsor in the form" — fix: declare the field inpresent.fields, or ask it in theformplaced before. - "which is not one — it could never match: write it as a number (149)" — fix: write the value
in the field's type:
> 0, not> '0'. - "reads $identity.authenticated, which is unknown — a condition here reads
$item.fields.<name>" — fix: who may choose is a profile or apermissionguard, not arequire. - "which is not one of its params — declare it" — fix: declare the parameter under the
skill's
params, with adefault. - "5 is not a condition — compare it" — fix: write a comparison,
$item.fields.available > 5.
Reference: Conditions, Select guards.