Skip to main content

Random data generators

note

This functionality is only available on paid plans. Check our plans here.

Similarly to variables, you can use the $tweak. namespace to use chancejs random generators. This enables a wide variety of mock generation capabilities.

We plugged in the entire chancejs API surface in the $tweak. namespace that enables a myriad of use cases! You can literally pick any method directly from the chancejs API!

This feature is available for:

Variables allow you specify values to reuse across all your rules, without having to repeat that value more than necessary, making your rules cleaner and quicker to modify.

As an example let's look at how to generate a random country and city for a mocked response.

{
"location": {
"country": "$tweak.country",
"city": "$tweak.city"
}
}

In this particular request, tweak renders the following JSON.

{
"location": {
"country": "US",
"city": "Rihifkac"
}
}
tip

Data generators are a powerful feature to generate random mock data when combined with response hooks. In response hooks you have unlimited programmatic access to an instance of our internal data generator through context.

Types​

tweak will respect the data type returned by the data generation function. If a given random data generator returns a numerical value, then tweak will render a numerical value. For example the following JSON:

{
"name": "Joe",
"age": "$tweak.age"
}

Will yield the following result:

{
"name": "Joe",
"age": 44
}


Was this page helpful?