Using the switch policy condition editor
The switch policy condition editor provides a user interface to help you build the
conditions for the case
clauses in a switch policy.
You build a condition script by using the Add Group and Add Condition options, together with operator selections, and pre-supplied function selections. The Output field shows the resultant condition script, and updates dynamically as you build your condition.
Adding conditions
When you open the policy condition editor, an initial condition is pre-supplied ready for you to configure.
Use the drop-down list to select the function that you want to begin your condition.
The following JSONata functions are supported:
- Aggregation functions:
$average(array)
$max(array)
$min(array)
$sum(array)
- Array functions:
$append(array1, array2)
$count(array)
$reverse(array)
$sort(array [, function])
$zip(array1, ...)
- Boolean functions:
$boolean(arg)
$exists(arg)
$not(arg)
- Numeric functions:
$abs(number)
$ceil(number)
$floor(number)
$formatBase(number [, radix])
$number(arg)
$power(base, exponent)
$round(number [, precision])
$sqrt(number)
- Object functions:
$keys(object)
$lookup(object, key)
$merge(array<object>)
$spread(object)
- String functions:
$contains(str, pattern)
$join(array[, separator])
$length(str)
$lowercase(str)
$match(str, pattern [, limit])
$pad(str, width [, char])
$replace(str, pattern, replacement [, limit])
$split(str, separator [, limit])
$string(arg)
$substring(str, start[, length])
$substringAfter(str, chars)
$substringBefore(str, chars)
$trim(str)
$uppercase(str)
You can use the following JSONata numeric operators:
+
(addition)-
(subtraction)-
*
(multiplication) - / (division)
- % (modulo)
You can use the following JSONata comparison operators for number values or strings:
=
!=
<
>
<=
>=
Extension | Variable | Description |
---|---|---|
$header(name) |
message.headers.name |
Message header |
$httpVerb() |
request.verb |
HTTP method of the request |
$operationID() |
api.operation.id |
ID of the operation |
$operationPath() |
api.operation.path |
Path of the operation |
$queryParameter('name') |
|
Returns the value of a request query parameter given the parameter name. The function
searches the |
$statusCode() |
message.status.code |
Status code |
$storageType([arg]) |
variable.body You can specify any variable in the API
context. When no variable is specified, the default variable |
Storage type of the message. The supported values are:
|
$urlParameter('name') |
|
Given a request parameter name, returns the parameter values for all occurrences of that
parameter as a path parameter or query parameter. The function searches the
For example, the following URL
contains both path and query parameter
values:
The
function call $urlParameter('breed') returns the following array of
values: In this example, the URL includes an API
path that is configured as /petstore/{breed}/{breed} , where breed
is configured to be a path parameter of the API path. As a result, cats and
adopt are included in the output. |
$xpath(path, xpathExpression) |
You can specify any writable variable in the API context. The
xpathExpression must be a literal string. |
Allows use of XPath expressions |
After you select the function, additional fields are displayed, depending on the selected
function, so that you can complete the condition. For example, if you select the function
$httpVerb()
, a comparison operator selection list is displayed (=
or !=
), together with a list of HTTP verbs to select on the right hand side of the
expression (GET
, PUT
, POST
,
DELETE
, HEAD
, OPTIONS
, or
PATCH
).
If you select NOT, your condition is negated with a $not
function.
To add further conditions, click Add Condition; you then select whether to insert an and
or an or
operator between this condition and the preceding one.
To group two or more conditions inside parentheses, click Add Group, then add your conditions to the group; you then select whether to insert an and
or an or
operator between this group and the preceding condition or group.
To write your own condition from scratch, select Custom, then enter your script in the field provided.
Example
($statusCode() != 200 and ($httpVerb() = 'GET' or $httpVerb() = 'PUT'))
complete these steps in the condition editor:Simple condition statements
The following examples show conditions that use a single function.
$httpVerb()
extension to specify the HTTP method of the
request.$httpVerb()="GET"
$operationPath()
extension to specify the path of the
operation.$operationPath()="/base/path-2"
$operationID()
extension to specify the operation
ID.$operationID()="test-gatewayscript-GET"
$statusCode()
extension to specify the message status
code.$statusCode()=200
$header(name)
extension to specify the content type of the
message header.$header("Content-Type")="application/json"
Combining conditions with logical operators
You can use and
and or
operators to combine multiple functions
in a single condition.
test-gatewayscript-GET
.$httpVerb()="GET" and $operationPath()="test-gatewayscript-GET"
POST
or PUT
request.$httpVerb()="POST" or $httpVerb()="PUT"
test-gatewayscript_POST
and
a message status code equal to 200
, or an API operation ID equal to
test-gatewayscript-GET
and a message status code equal to
500
.($operationID()="test-gatewayscript-POST" and $statusCode()=200) or ($operationID()="test-gatewayscript-GET" and $statusCode()=500)
test-gatewayscript-POST
and
a message status code equal to 200
with an API operation path equal to
/base/path-2
.($operationID()="test-gatewayscript-POST" and $statusCode()=200) and $operationPath()="/base/path-2"
text/plain
and a
message header length equal to 300
, or a message status code equal to
200
.($header("Content-Type")="text/plain" and $header("Content-Length")=300) or $statusCode()= 200