pub fn fuzz<R: Rng>(schema: &Schema, rng: &mut R) -> ValueExpand description
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: stringand object keys), arrays (forelements), and objects (forvalues) 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:
en_us/addresses/city_nameen_us/addresses/division_abbreviationen_us/addresses/divisionen_us/addresses/postal_codeen_us/addresses/secondary_addressen_us/addresses/street_addressen_us/addresses/street_nameen_us/company/company_nameen_us/company/sloganen_us/internet/domainen_us/internet/emailen_us/internet/usernameen_us/names/first_nameen_us/names/full_nameen_us/names/last_nameen_us/names/name_prefixen_us/names/name_suffixen_us/phones/phone_numberfr_fr/addresses/addressfr_fr/addresses/city_namefr_fr/addresses/divisionfr_fr/addresses/postal_codefr_fr/addresses/secondary_addressfr_fr/addresses/street_addressfr_fr/addresses/street_namefr_fr/company/company_namefr_fr/internet/domainfr_fr/internet/emailfr_fr/internet/usernamefr_fr/names/first_namefr_fr/names/full_namefr_fr/names/last_namefr_fr/names/name_prefixfr_fr/phones/phone_numberlorem/wordlorem/sentencelorem/paragraphlorem/paragraphs
New acceptable values for fuzzHint may be added to this crate within the
same major version.