Variable System
Variables allow you to pre-generate values and reuse them across your schema. This is useful when you need the same value in multiple places or want to create relationships between fields.
Defining Variables
Variables are defined in the variables section of your configuration:
{
"variables": {
"userId": {"fake": ["uuid"]},
"createdAt": {"fake": ["datetime"]}
},
"schema": {
...
}
}
Each variable is evaluated once when generation begins, and the result is cached for reuse.
Referencing Variables
Use the var operator to reference a variable:
{"var": "variableName"}
Basic Example
In this example, userId appears in both id and audit.createdBy with the same value, and timestamp is used for both createdAt and updatedAt.
Use Cases
Consistent IDs Across Related Objects
Generating Related Data
Timestamps for Audit Trails
Variable Scope
Variables are evaluated in the order they are defined, and each variable can only reference variables defined before it.
{
"variables": {
"firstName": {"fake": ["first_name"]},
"lastName": {"fake": ["last_name"]},
"fullName": {"cat": [{"var": "firstName"}, " ", {"var": "lastName"}]}
},
"schema": {
"name": {"var": "fullName"},
"firstName": {"var": "firstName"},
"lastName": {"var": "lastName"}
}
}
Variables vs Schema Values
| Feature | Variables | Schema Values |
|---|---|---|
| Evaluated | Once at start | For each generation |
| Reusable | Yes, via var | No |
| Order-dependent | Yes | No |
| Best for | Shared values, relationships | Unique values per field |
Tips
- Use variables for shared IDs - When multiple fields need the same identifier
- Use variables for timestamps - When created/updated times should match
- Use variables for names - When you need to derive email from name
- Keep variables simple - Complex expressions are better in the schema