jsonschema-schema
Typed Rust structs for JSON Schema (draft 2020-12) documents. Part of the Lintel project.
Unlike raw serde_json::Value, this crate gives you named fields for every
standard keyword — properties, items, allOf, $ref, format, and so
on — so you can pattern-match and navigate schemas without string lookups.
Features
- Strongly typed —
Schema,SchemaValue(object or boolean),TypeValue(single or union), andSimpleType(the seven primitive type names) with full serde round-tripping - All standard keywords — core identifiers, metadata, validation, applicators, composition, conditionals, content, and dependencies
- Editor extensions — first-class
x-taplo,x-tombi-*,x-intellij-*, andx-lintelextension structs - Catch-all — unknown properties are preserved in
extra: BTreeMap<String, Value> - Pointer navigation —
navigate_pointerwalks a JSON Pointer path through nested schemas, resolving$refalong the way - Helper utilities —
ref_name,resolve_ref,Schema::type_str,Schema::description, and more
Usage
Parsing a schema from JSON
use ;
let json = json!;
let schema: Schema = from_value.unwrap;
assert_eq!;
assert_eq!;
// type_str() produces a human-readable summary
assert_eq!;
// Access a nested property schema
let name_sv = schema.properties.get.unwrap;
let name = name_sv.as_schema.unwrap;
assert!;
Building a schema programmatically
use ;
use IndexMap;
let mut props = new;
props.insert;
let schema = Schema ;
let json = to_value.unwrap;
assert_eq!;
assert_eq!;
Navigating with JSON Pointers
navigate_pointer resolves a RFC 6901
JSON Pointer through the typed schema tree, automatically following $ref
references within the same document.
use ;
use BTreeMap;
// Build a schema with $defs and a $ref
let item = Schema;
let mut defs = new;
defs.insert;
let root = Schema;
let result = navigate_pointer.unwrap;
let tag = result.as_schema.unwrap;
assert_eq!;
Extracting a ref name
use ref_name;
assert_eq!;
assert_eq!;
License
Apache-2.0