Skip to main content

generate_validated

Function generate_validated 

Source
pub fn generate_validated<F>(
    count: usize,
    seed: u64,
    element_factory: F,
) -> Result<String, String>
where F: FnMut(&mut Rng) -> String,
Expand description

Like generate but validates the output as JSON before returning it.

Performs a structural validation pass: every byte of the output must form a syntactically valid JSON value. Returns Err(message) if validation fails — typically because the element_factory returned malformed JSON.

Useful as a defensive guard when the factory produces JSON from external/untrusted templates. Adds linear-in-output-size overhead.

§Example

use dev_fixtures::mock::json_array::generate_validated;

let json = generate_validated(3, 0, |rng| format!("{{\"v\":{}}}", rng.range(10))).unwrap();
assert!(json.starts_with("["));

// A broken factory triggers a validation error.
let err = generate_validated(1, 0, |_| "not_json".to_string()).unwrap_err();
assert!(err.contains("invalid"));