Condition
Conditions are expressions that evaluates to either true or false. The location of the condition decides its purpose in the ZSPEC. When placed in a data path, it helps in filtering the records to narrow down the data to your desired . In the other locations, it serves as a mere conditional statement.
Possible Locations
- Data Path
- Property @condition-for-request
- Property @condition-for-response
- Property @condition-for-all
- Property @value-condition
- Argument @condition
- Error @condition
Syntaxes of a Condition
copy<left-operand> <relational-operator> <right-operand> // for simple conditions
copy<left-operand> <relational-operator> <right-operand> <relational-operator> <left-operand> <relational-operator> <right-operand> // for complex conditions
Left and Right Operands
The operands are the building blocks of a condition and our ZSPEC supports versatile operand choices. Learning about these operand choices will help in constructing conditions that accurately reflect your intended constraints.
Here are the available left and right operands:
- Static data: You can utilize static keys or values.
- Dynamic data: Employ the data path when you want to use a dynamic data.
- In the value-condition attribute of property, you need not provide the left operand. By default, the left operand is the property in which you are specifying the value-condition.
- The value-condition will support only >=, <=, > and < operators.
Relational Operators
Relational Operators in conditions are symbols used to compare values and determine logical relationship between them. They are crucial in creating expressions that evaluate to true or false.
Following is a set of relational operators that you can use in ZSPEC,
Operators | Description |
---|---|
== | It represents equals to and checks if two values are equal. |
!= | It represents not equals to and checks if two values are not equal. |
> | It indicates greater than and checks if the value on the left is greater than the value on the right. |
< | It indicates less than and checks if the value on the left is less than the value on the right. |
>= | It means greater than or equal to and checks if the value on the left is greater than or equal to the value on the right. |
<= | It means lesser than or equal to and Checks if the value on the left is less than or equal to the value on the right. |
~~ | It denotes like. The expression supports three comparisons - starts with(value*) , ends with(*value) and contains(*value*). Here, * stands for wildcard. |
!~ | It denotes not like. The expression supports three comparisons - not starts with(value*) , not ends with(*value) and not contains(*value*). Here, * stands for wildcard. |
:: | It stands for in and checks if the value on the left is present in the list of keys on the right operand. The right operand should always be an array for this operator i.e. A :: [A,B]. |
!: | It stands for not in. It checks if the value on the left is not present in the list of keys on the right operand. |
|| | It represents logical or and is used to combine multiple conditions. |
&& | It represents logical and. It is used to combine multiple conditions. |
If the value which has to be used for comparison is a string, it should always be represented within single quotes.
Samples
Let us have a common JSON body in the data manager for applying all these expressions and operand choices.
{
"trips": [
{
"trip_name": "London - Emily Jones",
"trip_id": "5545984000000006873",
"no_of_days": 10,
"travelers": [
{
"traveler_name":[
"Ms",
"Emily",
"Jones"
],
"traveler_email": "emily@qke.com"
}
],
"destination": "Westminster Abbey"
}
]
}
Using static data
Following is a condition with static data from the above JSON body.
trip_name ~~ 'London*'
Here are the details of this condition,
- Left operand - A static key from the JSON body.
- Operator - Like. In this condition, it serves the starts with comparison.
- Right operand - A known value.
Using Dynamic data
Consider that we need to verify whether the values of the ’traveler_email’ key match the values of the ’email’ key from another resource named ’travelers’. In this scenario, we are accessing the dynamic values from a different resource. So, the condition should be expressed like this,
traveler_email :: travelers:$.email
Following are the condition details,
- Left operand - A static key from the JSON body.
- Operator - in
- Right operand - Here, the value is dynamic and accessed from a different resource using data path.
Check here to learn more about constructing data path.
Relational Operators
We will see a few samples of how you can use the operators in constructing conditions.
Condition | Meaning |
---|---|
trip_name == ‘London - Emily Jones’ | The trip name is “London - Emily Jones”. |
trip_name != ‘London - Emily Jones’ | The trip name is not “London - Emily Jones”. |
no_of_days > 4 | The number of days in the trip is greater than 4. |
no_of_days < 14 | The number of days in the trip is less than 14. |
no_of_days >= 10 | The number of days in the trip should be greater than or equal to 10. Here, it is equal to 10. |
no_of_days <= 14 | The number of days in the trip should be less than or equal to 10. Here, it is less than 14. |
trip_id ~~ ‘*873’ | The trip ID ends with these three numbers “873”. |
trip_name ~~ ‘London*’ | The trip name starts with “London”. |
trip_id ~~ ‘984’ | The trip ID contains these three numbers “984”. |
trip_name !~ ‘*Jones’ | The trip name does not ends with “Jones”. |
‘Ms’ :: [‘Ms’,‘Mr’,‘Mrs’] | The “Ms” value is present in the given array. |
‘Mr’ !: [‘Ms’,‘Mr’,‘Mrs’] | The “Mr” value is not present in the given array. |
no_of_days <= 14 && name ~~ ‘London*’ | The number of days in the trip is less than or equal to 14 and the trip name starts with “London”. |
no_of_days == 5 || travelers != null | The number of days in the trip should be 5 or the travelers field should not be null. |
Last Updated 2025-05-30 16:54:59 +0530 +0530
Yes
No
Send your feedback to us