Switch
Overview
Switch
is a step which allows execution of other steps by comparing an expression with one or many case conditions, as well as a defaulting case when there are no matches.
This is useful for redirecting logic depending on the contents of the flow document, and the step is very similar to the switch statement found in most programming languages.
Execution
When the switch
step is executed, it will try to match the expression to one of the defined case conditions. Once the first case is matched, it (meaning the steps contained within the case) will be executed. If no case is matched, the default case will be executed instead.
Compared to the conventional switch statement found in a programming language, switch
step does not allow fall-through. Which means after a case is matched and executed, the switch step is complete.
It is also possible to nest switch steps multiple levels.
Configuration
switchExpression
- the expression (e.g {{age}}) which will be matched to case conditionscases
- array of switch cases, each case contains acondition
and also asteps
arraycondition
- case condition (e.g{{35}}
,thirty-five
) which will be matched to theswitchExpression
steps
- array of steps to sequentially execute if the case is matched
defaultCase
- array of steps to sequentially execute if no cases match, can be empty
Sample step configuration
name: ProcessPolicyType
description: "Process two different policy types"
config:
switchExpression: "{{policyType}}"
stepType: switch
cases:
- condition: "life"
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "life policy type selected"}'
targetPath: result
stepType: jsonata
- condition: "{{customPolicyType}}"
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "custom policy type was selected"}'
targetPath: result
stepType: jsonata
defaultCase:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "no case matched the input"}'
targetPath: result
stepType: jsonata
Examples
Switch step used in flow to match categories to user messages
tags: []
sampleInputSchema: ""
sampleData: {}
description: ""
steps:
- name: SwitchBetweenCategories
description: Process supported categories
config:
switchExpression: "{{category}}"
stepType: switch
cases:
- condition: house
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "house was selected"}'
targetPath: result
stepType: jsonata
- condition: "{{customCategory}}"
steps:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "custom category was selected"}'
targetPath: result
stepType: jsonata
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '"sorry, this category is currently not available"'
targetPath: result.error
stepType: jsonata
defaultCase:
- name: Step jsonata
description: ""
config:
maps:
- jsonata: '{"message": "none of the available categories were selected"}'
targetPath: result
stepType: jsonata
This flow is capable of taking two inputs from the document - category
and customCategory
. The category
property represents the category selected by the user, and the customCategory
property represents a category which we will handle, but return an error message that it is not available.
The switch
statement in this flow is capable of handling three cases, case 1: house
, case 2: the case provided by {{customCategory}}
, and case 3: the default (fallback) case when nothing matches. Below are the sample input documents and expected results.
Document and result for case 1
{
"category": "house",
"customCategory": "pet"
}
{
"result": {
"message": "house was selected"
}
}
Document and result for case 2
{
"category": "pet",
"customCategory": "pet"
}
{
"result": {
"message": "custom category was selected",
"error": "sorry, this category is currently not available"
}
}
Document and result for default case
{
"category": "car",
"customCategory": "pet"
}
{
"result": {
"message": "none of the available categories were selected"
}
}