Expand description
Infers JSON Type Definition schemas from example inputs.
JSON Type Definition, aka RFC 8927, is an easy-to-learn, standardized way to define a schema for JSON data. You can use JSON Typedef to portably validate data across programming languages, create dummy data, generate code, and more.
This Rust crate can generate a JSON Typedef schema from example data. If you
are looking to use this package as a CLI tool, see this crate’s
README. The remainder of
these docs are focused on this crate as a Rust library, and so focuses on
the Rust API for using jtd_fuzz
.
§Quick start
Here’s how you can use this crate to infer a schema:
use serde_json::json;
use jtd_infer::{Inferrer, Hints, HintSet, NumType};
let mut inferrer = Inferrer::new(Hints::new(
NumType::Uint8,
HintSet::new(vec![]),
HintSet::new(vec![]),
HintSet::new(vec![]),
));
inferrer = inferrer.infer(json!({ "foo": true, "bar": "xxx" }));
inferrer = inferrer.infer(json!({ "foo": false, "bar": null, "baz": 5 }));
let inference = inferrer.into_schema();
assert_eq!(
json!({
"properties": {
"foo": { "type": "boolean" },
"bar": { "type": "string", "nullable": true },
},
"optionalProperties": {
"baz": { "type": "uint8" },
},
}),
serde_json::to_value(inference.into_serde_schema()).unwrap(),
)
Structs§
- HintSet
- A set of paths to parts of the input that are subject to a hint in
Hints
. - Hints
- Hints for
Inferrer
. - Inferrer
- Keeps track of a sequence of example inputs, and can be converted into an inferred schema.
Enums§
- NumType
- A type of number to infer by default.