Skip to content

GoPlasmatic/datafake-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Plasmatic Logo

datafake-rs

A high-performance mock JSON data generation library for Rust.

Uses JSONLogic for flexible and powerful fake data generation.

🏒 Organization β€’ πŸ“– Docs β€’ πŸ› Report a Bug


datafake-rs is a Rust library for generating realistic mock JSON data. It uses JSONLogic for its configuration, allowing you to define complex data structures with ease. The library extends JSONLogic with a custom fake operator, powered by the fake-rs crate, to generate a wide variety of data types for testing, development, and other use cases.

πŸš€ Key Features

  • JSONLogic Based: Define data generation rules using JSONLogic expressions.
  • Rich Fake Data: Over 50 fake data types, including names, addresses, and financial data.
  • Variable System: Pre-generate and reuse values throughout your schema.
  • Type-Safe: Ensures strong typing with thorough validation.
  • Batch Generation: Easily create multiple, unique data records.
  • High Performance: Built on datalogic-rs for efficient evaluation.
  • Flexible Configuration: Use JSON for easy setup and integration.
  • Thread-Safe: Designed for safe concurrent data generation.

πŸ—οΈ How It Works

Configuration-Driven Design

You define your data schema using a JSONLogic configuration with custom operators.

use datafake_rs::DataGenerator;
use serde_json::json;

let config = json!({
    "variables": {
        "userId": {"fake": ["uuid"]},
        "country": {"fake": ["country_code"]}
    },
    "schema": {
        "id": {"var": "userId"},
        "profile": {
            "name": {"fake": ["name"]},
            "email": {"fake": ["email"]},
            "age": {"fake": ["u8", 18, 65]}
        },
        "location": {
            "country": {"var": "country"},
            "city": {"fake": ["city_name"]},
            "coordinates": {
                "lat": {"fake": ["latitude"]},
                "lng": {"fake": ["longitude"]}
            }
        }
    }
});

let generator = DataGenerator::from_value(config)?;
let mock_data = generator.generate()?;

JSONLogic Integration

The library integrates with datalogic-rs and adds a fake operator, while still supporting all standard JSONLogic operators.

{
    "schema": {
        "active": {"==": [{"var": "status"}, "active"]},
        "discount": {"if": [
            {">": [{"var": "age"}, 65]},
            0.2,
            0.0
        ]},
        "code": {"fake": ["u32", 1000, 9999]}
    }
}

🎯 Fake Data Generation

Supported Data Types

The fake operator can generate over 50 different types of data.

Numeric

{"fake": ["u8"]}
{"fake": ["u16", 100, 1000]}
{"fake": ["i32", -50, 50]}
{"fake": ["f64", 0.0, 1.0]}

Personal

{"fake": ["name"]}
{"fake": ["first_name"]}
{"fake": ["last_name"]}
{"fake": ["title"]}
{"fake": ["email"]}
{"fake": ["phone_number"]}

Address

{"fake": ["street_address"]}
{"fake": ["city_name"]}
{"fake": ["state_name"]}
{"fake": ["country_name"]}
{"fake": ["zip_code"]}
{"fake": ["latitude"]}
{"fake": ["longitude"]}

Financial

{"fake": ["bic"]}
{"fake": ["credit_card_number"]}
{"fake": ["currency_code"]}
{"fake": ["currency_symbol"]}

Internet

{"fake": ["username"]}
{"fake": ["password", 10, 20]}
{"fake": ["ipv4"]}
{"fake": ["ipv6"]}
{"fake": ["mac_address"]}
{"fake": ["user_agent"]}
{"fake": ["domain_suffix"]}

Company

{"fake": ["company_name"]}
{"fake": ["industry"]}
{"fake": ["profession"]}
{"fake": ["catch_phrase"]}

Content

{"fake": ["word"]}
{"fake": ["words", 5]}
{"fake": ["sentence", 5, 10]}
{"fake": ["paragraph", 3, 5]}
{"fake": ["uuid"]}

Variable System

You can pre-generate values and reuse them in your schema.

{
    "variables": {
        "sessionId": {"fake": ["uuid"]},
        "timestamp": {"fake": ["u64", 1600000000, 1700000000]},
        "userId": {"fake": ["u32", 1000, 9999]}
    },
    "schema": {
        "session": {"var": "sessionId"},
        "events": [
            {
                "id": {"fake": ["uuid"]},
                "session": {"var": "sessionId"},
                "user": {"var": "userId"},
                "timestamp": {"var": "timestamp"},
                "action": {"fake": ["word"]}
            }
        ]
    }
}

πŸ”§ Installation

Add datafake-rs to your Cargo.toml:

[dependencies]
datafake-rs = "0.1.0"

Or use cargo:

cargo add datafake-rs

πŸ“– Usage Examples

Basic Example

use datafake_rs::DataGenerator;

let config = r#"{
    "schema": {
        "id": {"fake": ["uuid"]},
        "name": {"fake": ["name"]},
        "email": {"fake": ["email"]},
        "age": {"fake": ["u8", 18, 65]},
        "active": {"fake": ["bool"]}
    }
}"#;

let generator = DataGenerator::from_json(config)?;
let mock_user = generator.generate()?;

println!("{}", serde_json::to_string_pretty(&mock_user)?);

Nested Structures

use datafake_rs::DataGenerator;
use serde_json::json;

let config = json!({
    "variables": {
        "companyId": {"fake": ["uuid"]},
        "createdAt": {"fake": ["u64", 1600000000, 1700000000]}
    },
    "schema": {
        "company": {
            "id": {"var": "companyId"},
            "name": {"fake": ["company_name"]},
            "employees": [
                {
                    "id": {"fake": ["uuid"]},
                    "name": {"fake": ["name"]},
                    "email": {"fake": ["email"]}
                }
            ]
        }
    }
});

let generator = DataGenerator::from_value(config)?;
let company_data = generator.generate()?;

Batch Generation

Generate a list of unique records.

let config = json!({
    "schema": {
        "id": {"fake": ["uuid"]},
        "transaction": {
            "amount": {"fake": ["f64", 10.0, 1000.0]},
            "currency": {"fake": ["currency_code"]}
        }
    }
});

let generator = DataGenerator::from_value(config)?;
let transactions = generator.generate_batch(100)?;

Conditional Logic

Use JSONLogic conditions to shape your data.

let config = json!({
    "variables": {
        "age": {"fake": ["u8", 10, 80]}
    },
    "schema": {
        "age": {"var": "age"},
        "ageGroup": {
            "if": [
                {"<": [{"var": "age"}, 18]}, "minor",
                {"<": [{"var": "age"}, 65]}, "adult",
                "senior"
            ]
        }
    }
});

πŸ§ͺ Testing

To run the test suite:

# Run all tests
cargo test

# Run a specific test with output
cargo test test_generate_batch -- --nocapture

🀝 Contributing

Contributions are welcome! If you're submitting a change, please ensure it has good test coverage and documentation.

🏒 About Plasmatic

datafake-rs is an open-source project from Plasmatic. We build developer tools that are performant and based on open standards.

πŸ“„ License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

πŸ”— See Also

  • datalogic-rs: A high-performance JSONLogic implementation for Rust.
  • fake-rs: A library for generating fake data in Rust.
  • JSONLogic: The official JSONLogic specification.

Built with ❀️ by the Plasmatic team

About

High-Performance JSON Mock Data Generation Library

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages