Skip to main content

Comparison & Set Operators

You can use comparison operators within an if/else block to create dynamic responses based on incoming request parameters. In Beeceptor, comparison expressions follow a prefix-based notation, where the operator is placed before the arguments. Refer to the supported operators and their usage to craft dynamic mock responses.

Note:

  1. By default, template engine is off. You need to explicitly mark a rule to enable usage of operators and If/Else constructs.
  2. If you have a use-case for an operator that isn't listed below, please contact our Support Team. They can assist you in prioritizing your request.

Comparison Operators

eq (equals compare)

The eq (equals) operator checks if two arguments are equal, returning a boolean value. It supports both number and string data types and returns false if the types or the values don't match.

The following example demonstrates how to use the eq operator to return a specific response when both arguments are the same.

{{#if (eq 100 100)}}
This text block is always qualified for the response.
{{/if}}

You can also use the eq operator to conditionally send a response based on the value of a field in the request body. In the example below, the response block varies based on the value of stage field in the request body.

{{#if (eq (body 'stage') 1)}}
{
"message": "This JSON is qualified for the response if the stage field is sent as 1."
}
{{else}}
{
"message": "This JSON is sent if the stage field is not 1."
}
{{/if}}

Explore more operators and their usage to craft flexible and dynamic mock responses that fit your specific needs.

gt (greater than)

The gt (greater than) operator checks if the first argument is greater than the second, returning a boolean value.

The example below shows how to use the gt operator to send a conditional response based on the creditScore field in the request body. If the creditScore exceeds 700, the application status is marked as "APPROVED".

{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (gt (body 'creditScore') 700 )}}
"status": "APPROVED",
{{else}}
"status": "REJECTED",
{{/if}}
}

lt (less than)

The lt (less than) operator checks if the first argument is lesser than the second, returning a boolean value.

The example below shows how to use the lt operator to send a conditional response based on the creditScore field in the request body. If the creditScore is less than 700, the application status is marked as "REJECTED."

{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (lt (body 'creditScore') 700)}}
"status": "REJECTED",
{{else}}
"status": "APPROVED",
{{/if}}
}

lte (less than or equal to)

The lte (less than or equal to) operator checks if the first argument is less than or equal to the second, returning a boolean value.

The example below shows how to use the lte operator to send a conditional response based on the creditScore field in the request body. If the creditScore is less than or equal to 700, the application status is marked as "REVIEW."

{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (lte (body 'creditScore') 700)}}
"status": "REVIEW",
{{else}}
"status": "APPROVED",
{{/if}}
}

gte (greater than or equal to)

The gte (greater than or equal to) operator checks if the first argument is greater than or equal to the second, returning a boolean value.

The example below shows how to use the gte operator to send a conditional response based on the creditScore field in the request body. If the creditScore is greater than or equal to 700, the application status is marked as "APPROVED."

{
"applicationId": "{{faker 'random.uuid'}}",
"creditScore": {{body 'creditScore'}},
{{#if (gte (body 'creditScore') 700)}}
"status": "APPROVED",
{{else}}
"status": "REJECTED",
{{/if}}
}

Type Checking

With the dynamic templates, sometimes it is required to check for datatypes of some fields before applying any logic. Beeceptor comes with isNumber, isInteger, and isDate operators to provide you a an easy way to perform type checks. These helpers return either true or false for the given value. Combining these operators with #if, you can dynamically validate and build a service response.

Example usage:

{{#if (isNumber '42')}} 42 is a number{{else}}This isn't a number{{/if}}
{{#if (isInteger (body 'count'))}}The value in the count field of the request payload is a valid integer{{else}}Invalid integer{{/if}}
{{#if (isDate (data-store 'get' 'date'))}}The stored value in field data is a valid date{{else}}Invalid date{{/if}}

isNumber

Checks whether the given value is a valid number (integer or float). Returns true for numeric values, including strings that represent numbers.

Syntax:

{{isNumber value_to_check}}  

Example Usage:

{{isNumber '42'}}                    → true  
{{isNumber '3.14'}} → true
{{isNumber 'hello'}} → false
{{isNumber ''}} → false
{{isNumber (body 'score')}} → true / false
{{isNumber (queryParam 'value')}} → true / false
{{isNumber (data-store 'get' 'number')}} → true / false

Common Use Cases:

  • Validate query or body parameters before performing numeric operations.
  • Add conditional logic in templates for number-specific formatting.
  • Verify if user input can be parsed as a number.

isInteger

Checks whether the given value is a valid integer (whole number). Returns true for strings or values that represent integers, including negative numbers.

Syntax:

{{isInteger value_to_check}}

Example Usage:

{{isInteger '42'}}                     → true  
{{isInteger '3.14'}} → false
{{isInteger '-5'}} → true
{{isInteger 'not-a-number'}} → false
{{isInteger (body 'count')}} → true / false
{{isInteger (queryParam 'qty')}} → true / false
{{isInteger (data-store 'get' 'integer')}} → true / false

Common Use Cases

  • Ensure body/query values are integers before use in loops, pagination, indexing, etc.
  • Prevent float values where only whole numbers are allowed.
  • Apply conditionals based on numeric precision.

isDate

Checks whether the given value is a valid date string. Accepts ISO 8601 formats and returns true if the input can be parsed into a valid JavaScript date.

Syntax:

{{isDate value_to_check}}

Example Usage:

{{isDate '2023-12-25'}}              → true  
{{isDate '2023-12-25T10:30:00Z'}} → true
{{isDate 'invalid-date'}} → false
{{isDate ''}} → false
{{isDate (body 'eventDate')}} → true / false
{{isDate (queryParam 'date')}} → true / false
{{isDate (data-store 'get' 'date')}} → true / false

Common Use Cases:

  • Validate date input from APIs or UI before parsing.
  • Add fallback logic for invalid date values.
  • Conditionally format or render date-dependent sections.