[][src]Function jtd_fuzz::fuzz

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.

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.

As an example of the sort of data this function may produce:

use std::convert::TryInto;
use serde_json::json;
use rand::SeedableRng;

// An example schema we can test against.
let schema: jtd::SerdeSchema = serde_json::from_value(json!({
    "properties": {
        "name": { "type": "string" },
        "createdAt": { "type": "timestamp" },
        "favoriteNumbers": {
            "elements": { "type": "uint8" }
        }
    }
})).unwrap();

let schema: jtd::Schema = schema.try_into().unwrap();

// A hard-coded RNG, so that the output is predictable.
let mut rng = rand_pcg::Pcg32::seed_from_u64(8927);

assert_eq!(jtd_fuzz::fuzz(&schema, &mut rng), json!({
    "name": "e",
    "createdAt": "1931-10-18T15:44:45-03:55",
    "favoriteNumbers": [166, 142]
}));