json_schema_ast
Strict Draft 2020-12 JSON Schema documents, validation, and resolved schema IR.
Installation
Add to your Cargo.toml:
[]
= "0.3.0"
Usage
use ;
use json;
let raw = json!;
// Build a document. Construction canonicalizes the schema and checks keyword shapes.
let schema = from_json.unwrap;
// Validate instances with the raw-schema validator backend.
assert!;
assert!;
// Compile a validator directly if you need to own the backend type.
let validator = compile.unwrap;
assert!;
Public Interface
For validation callers:
SchemaDocument::from_json(&Value)builds a document from a raw Draft 2020-12 JSON Schema.SchemaDocument::is_valid(&Value)validates instances against the original raw schema.SchemaDocument::canonical_schema_json()exposes the canonicalized schema used for IR construction and debugging.compile(&Value)returns the underlyingjsonschema::JSONSchemavalidator after this crate's dialect checks.AstError,SchemaBuildError,SchemaError, andCompileErrorare the typed error surfaces.
For resolved-IR consumers:
SchemaDocument::root()returns the lazily resolved immutableSchemaNodegraph.SchemaNode::kind()exposes the non-exhaustiveSchemaNodeKindIR for downstream analyzers.SchemaNode::id()exposes opaque node identity for cycle guards.SchemaNode::accepts_value()is a low-level evaluator for resolved subgraphs; useSchemaDocument::is_valid()for user-visible validation.json_values_equal(&Value, &Value)compares JSON values using JSON Schema's numeric equality rule.
The supporting constraint types exposed through SchemaNodeKind are deliberately
structured: IntegerBounds, NumberBounds, and CountRange reject empty
intervals at construction; ContainsConstraint keeps the contains schema and
match-count range together; and PatternConstraint records whether the internal
Rust matcher can evaluate a JSON Schema pattern.
The resolved IR entrypoints are public because jsoncompat and
json_schema_fuzz are separate crates; most users only need the validation API.
If a schema document sets $schema, it must be exactly Draft 2020-12
(https://json-schema.org/draft/2020-12/schema, with an optional trailing
#). Omitting $schema is allowed and is interpreted as Draft 2020-12.
Same-document refs to "#" and "#/..." are supported, including recursive
graphs. Pure alias cycles, remote refs, plain-name fragments, and dynamic refs
are rejected with typed resolver errors.
License
Licensed under MIT. See LICENSE.