Valid Expressions

Valid Expressions in mabl String Template

The mabl String template can be used to create strings from constant values, random values, variables, and/or mathematical expressions. mabl supports certain macros in the string templates to generate random strings and also supports embedding variable values into the string template. The macros and variables are delimited by double curly braces.

Variables in string templates are also prepended by @. Here are some examples of valid expressions using variables that can be used for string templates:

  • {{@variable}}
  • Viewed {{@two}} elements

Macros are followed by a colon and a number, where the number indicates the number of random characters to be created. Here is the list of supported macros that can be used for string templates:

  • alpha - upper and lower case letters
  • alphaLower - lower case letters (a-z)
  • alphaUpper - upper case letters (A-Z)
  • digit - numbers (0-9)
  • alnum - alphanumeric (i.e. alpha + digit)

Here are some examples of valid expressions using macros:

  • {{alpha:3}}
  • abc{{@alphaLower:4}}
  • ABC{{@alphaUpper:5}}
  • 123-{{@digit:4}}
  • random{{alnum:6}}

Valid Expressions for Generating Random Data

mabl can also generate realistic random data using the string template. The random data expressions have to be enclosed in double curly braces and have to be prepended by "fake.". The syntax for this is in the form {{fake.type.subtype}}, e.g. {{fake.name.lastName}}. Below are some examples of how to use the string template to create random data.

Creating a random first name

Creating a random first name

Random monetary values can also be generated as well:

Creating a random monetary value

Creating a random monetary value

Even random titles can be created:

Below are some of the more commonly used expressions for creating random data:

Expression
Expectation
Sample Result

{{fake.name.firstName}}

Random first name

"Garfield"

{{fake.name.lastName}}

Random last name

"Larson"

{{fake.address.streetAddress}}

Random street address

"840 Roberts Burg"

{{fake.address.city}}

Random city name

"Manteton"

{{fake.address.state}}

Random US state name

"North Carolina"

{{fake.address.zipCode}}

Random US zip code

"02160"

{{fake.address.countryCode}}

Random 2-letter country code

"TN"

{{fake.company.companyName}}

Random company name

"Sawayn Group"

{{fake.phone.phoneNumber}}

Random phone number in one of multiple formats

"1-968-628-9831 x738"

{{fake.phone.phoneNumberFormat}}

Random phone number in the format ###-###-####

"776-705-5652"

{{fake.lorem.text}}

Random filler text

"consequatur omnis neque"

{{fake.random.uuid}}

Random UUID

"71bcc5d3-7f24-4b08-bcf8-f9b8e0d6d913"

{{fake.finance.amount}}

Random financial amount

"17.78"

For the complete list of valid types and subtypes, visit the faker.js documentation.

Note: only the English locale is supported.

How to use faker.js documentation to build expression

faker.js documentation lists the valid types and subtypes in the form type.subtype. mabl expects the expression to be in the form {{fake.type.subtype}}.

Valid Mathematical Expressions

mabl can perform basic math operations within the string template and save the results to a new variable or overwrite an existing variable. Formulating the expression is important for the correct results. The mathematical expressions must be enclosed by the double curly braces to be evaluated. Here are examples of how to create valid math expressions in the string template:

  1. Below is an example of using a digit macro within a math calculation. This will evaluate the macro (9) and add 100 to the macro.
  1. Math expressions will be evaluated under the PEMDAS rules.
    Example: {{5 + 3 / 2}} will evaluate to 6.5
  2. Use parenthesis to evaluate math operations in the specified order.
    Example: {{(5 + 3) / 2}} will evaluate to 4
  3. Concatenate strings such as $ or % to create a string value from the results with units.
    Example: ${{(5 + 3 ) / 2}} will evaluate to $4

Advanced Math Expressions

mabl also supports certain math functions to format the numerical result from a math expression. Below are the functions that are supported:

  1. round(number, numberOfDecialPlaces) - Rounds the result of a math calculation to numberOfDecialPlaces number of decimal places.
  1. abs(number) - Returns the absolute value of a math calculation
    Example: {{round(1 / 3, 2)}} will evaluate to 0.33

Updated about a month ago

Valid Expressions


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.