Values and conditions
What a person types and chooses, checking it, showing it, and the conditions that decide what happens next.
Fields
A component that holds a value, such as a text field or a toggle, takes a name, and what it holds is kept under that name for the rest of the journey:
{
"type": "text-field",
"attributes": { "label": "Postcode", "hint": "For example SW1A 2AA", "type": "postcode" },
"name": "postcode",
"validation": [
{ "rule": "required" },
{ "rule": "pattern", "value": "^[A-Za-z]{1,2}[0-9][A-Za-z0-9]? *[0-9][A-Za-z]{2}$", "message": "Enter a postcode in the correct format" }
]
}
A toggle or checkbox holds true or false, and keeps its state when its screen is drawn again.
Validation
validation is checked, in order, when the screen is submitted. The first rule that fails shows its message against the field, and the person stays on the screen. The rules are required, pattern, min-length and max-length. required without a message just does nothing, as the app does when no council is chosen.
Choices
A choice is one of several components with the same name, each with a value of its own, such as the segments of a segmented control or a list’s selectable rows. Choosing one sets the name to its value. Its chosen attributes are among its attributes only while it’s the one chosen. The one written with them already is where it starts:
{ "type": "segmented-control-item", "name": "topics", "attributes": { "value": "yours", "selected": true }, "chosen": { "selected": true }, "text": "Your topics" },
{ "type": "segmented-control-item", "name": "topics", "attributes": { "value": "all" }, "chosen": { "selected": true }, "text": "All topics" }
Conditions
A component’s when, and a next’s, is a condition. Exactly one of:
| Condition | Holds when |
|---|---|
{ "present": "council" } |
The path leads to a value |
{ "missing": "council" } |
It doesn’t |
{ "is": { "topics": "all" } } |
The value at the path is exactly that, text or true or false |
{ "contains": { "text": "asked", "words": ["caring", "carer"] } } |
The text at text has any of the words in it |
{ "all": [ … ] } |
All the conditions listed hold |
{ "any": [ … ] } |
Any of them does |
{ "one": [ … ] } |
Exactly one of them does |
{ "not": { … } } |
The condition doesn’t hold |
An empty list counts as missing, so missing holds for a search that found nothing.
{ "type": "grouped-list-row", "attributes": { "type": "topic", "icon": "directions_car" }, "text": "Driving", "when": { "is": { "topics": "yours" } }, "action": { "go": "driving" } }
Finding words in text
contains finds words in what someone typed:
- A word is found at the start of a word in the text, whatever its case and the punctuation around it, so
passportis found in “Passports?”. - Several words, such as
council tax, are found as those words, in that order. wordscan be a path instead of a list, such asitem.keywordswhen filtering a list.
It matches words, it doesn’t understand them.
Naming a condition
A derived condition is whether a condition holds, true or false, so it’s written once and used by name. Chat answers a question about one of three things, and offers questions to try for anything else:
"derived": {
"about-passport": { "condition": { "contains": { "text": "asked", "words": ["passport"] } } },
"about-caring": { "condition": { "contains": { "text": "asked", "words": ["caring", "carer"] } } },
"about-council-tax": { "condition": { "contains": { "text": "asked", "words": ["council tax"] } } },
"understood": { "condition": { "one": [{ "is": { "about-passport": true } }, { "is": { "about-caring": true } }, { "is": { "about-council-tax": true } }] } }
}
Each answer is shown when the question was understood and is about its topic. The reply for anything else is shown when it wasn’t, so a question about two of them, such as “Can I pay council tax at the passport office?”, gets that reply too:
{ "type": "chat-answer", "when": { "all": [{ "is": { "understood": true } }, { "is": { "about-passport": true } }] }, "children": [{ "type": "text", "text": "You can renew your adult passport online or with a paper form." }] },
{ "type": "chat-answer", "when": { "is": { "understood": false } }, "children": [{ "type": "text", "text": "Sorry, I can’t answer that in this prototype. I can tell you about passports, caring for someone or council tax." }] }
Keeping what was sent
What a field holds changes as the person types. To keep what they sent, such as a question, copy it with set when the screen is submitted, and show the copy:
"next": [{ "set": { "asked": "question" }, "go": "chat-generating", "history": "replace" }]
Chat shows {{ asked }}. Shown as {{ question }}, the question on screen would change as the person typed the next one.
A button that gives a value
A button with a name and a value of its own gives that value when it’s pressed, before its action, as a form’s submit button sends its own. Chat’s questions to try ask themselves this way:
{ "type": "chat-example-question", "name": "question", "attributes": { "value": "How do I renew my passport?" }, "text": "How do I renew my passport?", "action": "submit" }
Paths
A path names a value, with a dot to step inside it: council.parent.name. It looks among values kept by set, what the person typed and chose, and derived values. A path never computes anything: there’s no arithmetic, no function and no default.