noson
A Rust library that generates example JSON data from a JSON Schema.
Given a JSON Schema as a serde_json::Value, noson produces a random JSON
value that validates against it.
Installation
Usage
The caller provides the random number generator, so output is reproducible when a seeded RNG is used.
use SeedableRng;
use StdRng;
use json;
let schema = json!;
let mut rng = seed_from_u64;
let example = generate.unwrap;
// example is a random JSON value that validates against the schema
println!;
Required properties are always included; optional properties are included with 50% probability. Recursive schemas are supported up to a nesting depth of 16.
Supported Schema Features
- Types:
null,boolean,string,integer,number,object,array - Constraints:
minimum/maximum,exclusiveMinimum/exclusiveMaximum,minLength/maxLength,minItems/maxItems - Format:
date-time,date,time,duration - Enum / Const:
enum,const - Composition:
allOf,anyOf,oneOf - References:
$refresolved against$defs/definitions - Boolean schemas:
true(any value) andfalse(error)
Not Supported
The following JSON Schema features are not currently handled. Schemas using
them will either be silently ignored (the keyword has no effect on generation)
or, in the case of external $ref, return an error.
- String:
pattern,contentEncoding,contentMediaType - Numeric:
multipleOf - Object:
additionalProperties,patternProperties,propertyNames,minProperties,maxProperties,unevaluatedProperties - Array:
prefixItems,additionalItems,contains,minContains,maxContains,uniqueItems,unevaluatedItems - Composition:
not,if/then/else - Dependencies:
dependentRequired,dependentSchemas - References: external
$ref(http/file URIs),$dynamicRef,$anchor - Type unions:
"type": ["string", "null"](array form oftype)
Roadmap
- Builder/configuration API -- customizable string length ranges, array sizes, max recursion depth
formatgenerators --email,uri,uuid, etc.patternsupport -- regex-based string generation viarand_regexadditionalPropertiesgenerationprefixItems,containsminProperties/maxPropertiesmultipleOfif/then/elsenotdependentRequired/dependentSchemas- External
$refresolution (http/file references)
Development
This project was developed with the assistance of LLMs (Claude).