Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

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

FeatureVariablesSchema Values
EvaluatedOnce at startFor each generation
ReusableYes, via varNo
Order-dependentYesNo
Best forShared values, relationshipsUnique values per field

Tips

  1. Use variables for shared IDs - When multiple fields need the same identifier
  2. Use variables for timestamps - When created/updated times should match
  3. Use variables for names - When you need to derive email from name
  4. Keep variables simple - Complex expressions are better in the schema