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 (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_name
en_us/addresses/division_abbreviation
en_us/addresses/division
en_us/addresses/postal_code
en_us/addresses/secondary_address
en_us/addresses/street_address
en_us/addresses/street_name
en_us/company/company_name
en_us/company/slogan
en_us/internet/domain
en_us/internet/email
en_us/internet/username
en_us/names/first_name
en_us/names/full_name
en_us/names/last_name
en_us/names/name_prefix
en_us/names/name_suffix
en_us/phones/phone_number
fr_fr/addresses/address
fr_fr/addresses/city_name
fr_fr/addresses/division
fr_fr/addresses/postal_code
fr_fr/addresses/secondary_address
fr_fr/addresses/street_address
fr_fr/addresses/street_name
fr_fr/company/company_name
fr_fr/internet/domain
fr_fr/internet/email
fr_fr/internet/username
fr_fr/names/first_name
fr_fr/names/full_name
fr_fr/names/last_name
fr_fr/names/name_prefix
fr_fr/phones/phone_number
lorem/word
lorem/sentence
lorem/paragraph
lorem/paragraphs
New acceptable values for fuzzHint
may be added to this crate within the
same major version.