Basic Concepts
Configuration Structure
A datafake configuration is a JSON object with three optional sections:
{
"metadata": { ... },
"variables": { ... },
"schema": { ... }
}
Metadata
Optional information about the configuration:
{
"metadata": {
"name": "User Generator",
"version": "1.0.0",
"description": "Generates fake user data"
}
}
Variables
Pre-generate values that can be reused across the schema:
{
"variables": {
"userId": {"fake": ["uuid"]},
"createdAt": {"fake": ["datetime"]}
},
"schema": {
"id": {"var": "userId"},
"audit": {
"createdBy": {"var": "userId"},
"createdAt": {"var": "createdAt"}
}
}
}
Schema
The schema defines the structure of the generated output. It uses JSONLogic operators, with the fake operator being the most important.
The fake Operator
The fake operator generates fake data of a specified type:
{"fake": ["type"]}
{"fake": ["type", arg1, arg2]}
Simple Types
{"fake": ["uuid"]}
{"fake": ["name"]}
{"fake": ["email"]}
Types with Arguments
Some types accept arguments for customization:
{"fake": ["u8", 18, 65]} // Integer between 18 and 65
{"fake": ["password", 8, 16]} // Password with 8-16 characters
{"fake": ["words", 5]} // 5 random words
JSONLogic Operators
datafake-rs supports all standard JSONLogic operators. Here are some commonly used ones:
Variable Access
Reference variables with var:
{"var": "userId"}
{"var": "user.name"}
String Concatenation
Combine strings with cat:
Conditional Logic
Use if for conditional generation:
Arrays
Generate arrays with map:
{
"schema": {
"tags": {"map": [
[1, 2, 3],
{"fake": ["word"]}
]}
}
}
Nested Objects
Create complex nested structures:
Error Handling
Invalid configurations will result in clear error messages:
- ConfigParse - Invalid JSON syntax
- InvalidConfig - Missing required fields
- FakeOperatorError - Unknown fake type or invalid arguments
- VariableNotFound - Referenced variable doesn’t exist