Function jtd_fuzz::fuzz[][src]

pub fn fuzz<R: Rng>(schema: &Schema, rng: &mut R) -> Value

Generates a single random JSON value satisfying a given schema.

The generated output is purely a function of the given schema and RNG. It is guaranteed that the returned data satisfies the given schema.

Invariants for generated data

The output of this function is not guaranteed to remain the same between different versions of this crate; if you use a different version of this crate, you may get different output from this function.

Some properties of fuzz which are guaranteed for this version of the crate, but which may change within the same major version number of the crate:

  • Generated strings (for type: string and object keys), arrays (for elements), and objects (for values) will have no more than seven characters, elements, and members, respectively.

  • No more than seven "extra" properties will be added for schemas with additionalProperties.

  • Generated strings will be entirely printable ASCII.

  • Generated timestamps will have a random offset from UTC. These offsets will not necessarily be "historical"; some offsets may never have been used in the real world.

Using fuzzHint

If you want to generate a specific sort of string from your schema, you can use the fuzzHint metadata property to customize output. For example, if you'd like to generate a fake email instead of a generic string, you can use a fuzzHint of en_us/internet/email:

use serde_json::json;
use rand::SeedableRng;

let schema = jtd::Schema::from_serde_schema(serde_json::from_value(json!({
    "type": "string",
    "metadata": {
        "fuzzHint": "en_us/internet/email"
    }
})).unwrap()).unwrap();

let mut rng = rand_pcg::Pcg32::seed_from_u64(8927);
assert_eq!(jtd_fuzz::fuzz(&schema, &mut rng), json!("prenner3@fay.com"));

fuzzHint will only be honored for schemas with type of string. It will not be honored for empty schemas. If fuzzHint does not have one of the values listed below, then its value will be ignored.

The possible values for fuzzHint are:

New acceptable values for fuzzHint may be added to this crate within the same major version.