Skip to main content

Crate vld_fake

Crate vld_fake 

Source
Expand description

§vld-fake

Generate fake / test data that satisfies a vld JSON Schema.

§Quick start — typed API

use vld::prelude::*;
use vld_fake::FakeData;

vld::schema! {
    #[derive(Debug)]
    pub struct User {
        pub name:  String      => vld::string().min(2).max(50),
        pub email: String      => vld::string().email(),
        pub age:   i64         => vld::number().int().min(18).max(99),
    }
}

vld_fake::impl_fake!(User);

let user = User::fake();
assert!(user.name.len() >= 2);
assert!(user.email.contains('@'));

// Multiple
let users = User::fake_many(5);

// Reproducible
let u1 = User::fake_seeded(42);
let u2 = User::fake_seeded(42);
assert_eq!(u1.name, u2.name);

§Low-level (untyped) API

use vld::prelude::*;

vld::schema! {
    #[derive(Debug)]
    pub struct User {
        pub name:  String      => vld::string().min(2).max(50),
        pub email: String      => vld::string().email(),
        pub age:   i64         => vld::number().int().min(18).max(99),
    }
}

let schema = User::json_schema();
let value  = vld_fake::fake_value(&schema);
// value is a random serde_json::Value

Modules§

prelude

Macros§

impl_fake
Implement FakeData for a vld::schema! struct.

Structs§

FakeGen
Stateful fake-data generator backed by any rand::Rng.

Traits§

FakeData
Trait for types that can generate fake instances of themselves.

Functions§

fake_json
Generate a JSON string conforming to the given JSON Schema.
fake_many
Generate count random values from the same schema.
fake_parsed
Generate a random value and parse it through T::vld_parse_value, returning a fully validated typed instance.
fake_value
Generate a single random Value conforming to the given JSON Schema.
fake_value_seeded
Generate with a specific seed for reproducible output.
try_fake_parsed
Same as fake_parsed, but returns a Result instead of panicking.