# jsonschema [crates.io](https://crates.io/crates/jsonschema) [docs.rs](https://docs.rs/jsonschema) [build status](https://github.com/Stranger6667/jsonschema/actions?query=branch%3Amaster) [codecov.io](https://app.codecov.io/github/Stranger6667/jsonschema) [Supported Dialects](https://bowtie.report/#/implementations/rust-jsonschema) A high-performance JSON Schema validator for Rust. ```rust use serde_json::json; fn main() -> Result<(), Box> { let schema = json!({"maxLength": 5}); let instance = json!("foo"); // One-off validation assert!(jsonschema::is_valid(&schema, &instance)); assert!(jsonschema::validate(&schema, &instance).is_ok()); // Build & reuse (faster) let validator = jsonschema::validator_for(&schema)?; // Fail on first error assert!(validator.validate(&instance).is_ok()); // Iterate over errors for error in validator.iter_errors(&instance) { eprintln!("Error: {error}"); eprintln!("Location: {}", error.instance_path()); } // Boolean result assert!(validator.is_valid(&instance)); // Structured output (JSON Schema Output v1) let evaluation = validator.evaluate(&instance); for annotation in evaluation.iter_annotations() { eprintln!( "Annotation at {}: {:?}", annotation.schema_location, annotation.annotations.value() ); } // Generated validators are significantly faster than runtime ones, // so prefer them when the schema is known at build time. // Requires the `macros` feature. // Inline schema, or `path = "schema.json"` to load from a file #[jsonschema::validator(schema = r#"{"maxLength": 5}"#)] struct Short; assert!(Short::is_valid(&instance)); Short::validate(&instance)?; Ok(()) } ``` You also can use it from the command line via the [jsonschema-cli](https://github.com/Stranger6667/jsonschema/tree/master/crates/jsonschema-cli) crate. ```console $ jsonschema-cli validate schema.json -i instance.json ``` See more usage examples in the [documentation](https://docs.rs/jsonschema). > ⚠️ **Upgrading from older versions?** Check our [Migration Guide](https://github.com/Stranger6667/jsonschema/blob/master/MIGRATION.md) for key changes. ## Playground If you'd like to try `jsonschema`, you can check the WebAssembly-powered [playground](https://jsonschema.dygalo.dev/) to see the results instantly. ## Highlights - 📚 Full support for popular JSON Schema drafts - 🔧 Custom keywords and format validators - ⚡ Compile-time validators for Rust, Python and Ruby extension modules - 🌐 Blocking & non-blocking remote reference fetching (network/file) - 📦 Schema bundling into Compound Schema Documents, and `$ref` dereferencing - 🎨 Structured Output v1 reports (flag/list/hierarchical) - ✨ Meta-schema validation for schema documents, including custom metaschemas - 🧮 Experimental schema canonicalization - 🧩 Validation of custom in-memory JSON representations - 🔗 Bindings for [Python](https://github.com/Stranger6667/jsonschema/tree/master/crates/jsonschema-py) and [Ruby](https://github.com/Stranger6667/jsonschema/tree/master/crates/jsonschema-rb) - 🚀 WebAssembly support - 💻 Command Line Interface ### Supported drafts The following drafts are supported: - [![Draft 2020-12](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Frust-jsonschema%2Fcompliance%2Fdraft2020-12.json)](https://bowtie.report/#/implementations/rust-jsonschema) - [![Draft 2019-09](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Frust-jsonschema%2Fcompliance%2Fdraft2019-09.json)](https://bowtie.report/#/implementations/rust-jsonschema) - [![Draft 7](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Frust-jsonschema%2Fcompliance%2Fdraft7.json)](https://bowtie.report/#/implementations/rust-jsonschema) - [![Draft 6](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Frust-jsonschema%2Fcompliance%2Fdraft6.json)](https://bowtie.report/#/implementations/rust-jsonschema) - [![Draft 4](https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Frust-jsonschema%2Fcompliance%2Fdraft4.json)](https://bowtie.report/#/implementations/rust-jsonschema) You can check the current status on the [Bowtie Report](https://bowtie.report/#/implementations/rust-jsonschema). ## Notable Users - Tauri: [Config validation](https://github.com/tauri-apps/tauri/blob/c901d9fdf932bf7c3c77e9d3097fabb1fe0712af/crates/tauri-cli/src/helpers/config.rs#L173) - Apollo Router: [Config file validation](https://github.com/apollographql/router/blob/855cf6cc0757ca6176970ddf3ae8c98c87c632d1/apollo-router/src/configuration/schema.rs#L120) - qsv: [CSV record validation with custom keyword & format validator](https://github.com/jqnatividad/qsv/blob/6b6985065a1270f767d881b13aa2a27fae1958fb/src/cmd/validate.rs#L630) ## Performance `jsonschema` outperforms other Rust JSON Schema validators in most scenarios. The numbers below are for the runtime validator, which compiles a schema into a reusable `Validator`: - **75-692x** faster than `valico` and **10-78x** faster than `jsonschema_valid` for complex schemas (**>1,000,000x** on FHIR) - Generally **2.8-40x** faster than `boon`, **>280x** on FHIR, and **>5000x** for recursive schemas When the schema is known at build time, the `#[jsonschema::validator]` macro (`macros` feature) generates a specialized validator at compile time. This avoids schema compilation at runtime and validates roughly **2.3-10x** faster than the runtime validator across our benchmark schemas. For detailed benchmarks, see our [full performance comparison](https://github.com/Stranger6667/jsonschema/tree/master/crates/benchmark-suite). ## Cargo features | Feature | Default | Enables | |---|---|---| | `resolve-http` | ✅ | Fetching remote references over HTTP | | `resolve-file` | ✅ | Reading references from the filesystem | | `tls-aws-lc-rs` | ✅ | TLS provider for `resolve-http` | | `idna` | ✅ | The `idn-hostname` and `idn-email` formats | | `tls-ring` | | `ring` as the TLS provider instead of `aws-lc-rs` | | `resolve-async` | | Non-blocking reference resolution | | `arbitrary-precision` | | Arbitrarily large integers and exact decimal values | | `macros` | | The `#[jsonschema::validator]` compile-time validator | | `pyo3` | | Validating Python objects in place, and `backend = Pyo3` for the macro | | `magnus` | | Validating Ruby objects in place, and `backend = Magnus` for the macro | | `jsonb` | | Validating Postgres `jsonb` values in place | Turning off `idna` drops the IDNA and ICU dependencies; `idn-hostname` and `idn-email` are then treated as unknown formats. ## Minimum Supported Rust Version (MSRV) This crate requires Rust 1.85.0 or later. ## TLS Configuration By default, `jsonschema` uses `aws-lc-rs` as the TLS cryptography provider, which is the default one in `reqwest`. ### Using Ring Instead You can opt into using `ring` as the TLS provider: ```toml [dependencies] jsonschema = { version = "0.42", default-features = false, features = ["resolve-http", "resolve-file", "tls-ring"] } ``` **NOTE:** If both `tls-aws-lc-rs` and `tls-ring` features are enabled, `aws-lc-rs` takes precedence. ## Acknowledgements This library draws API design inspiration from the Python [`jsonschema`](https://github.com/python-jsonschema/jsonschema) package. We're grateful to the Python `jsonschema` maintainers and contributors for their pioneering work in JSON Schema validation. ## Support If you have questions, need help, or want to suggest improvements, please use [GitHub Discussions](https://github.com/Stranger6667/jsonschema/discussions). ## Sponsorship If you find `jsonschema` useful, please consider [sponsoring its development](https://github.com/sponsors/Stranger6667). ## Contributing We welcome contributions! Here's how you can help: - Share your use cases - Implement missing keywords - Fix failing test cases from the [JSON Schema test suite](https://bowtie.report/#/implementations/rust-jsonschema) See [CONTRIBUTING.md](https://github.com/Stranger6667/jsonschema/blob/master/CONTRIBUTING.md) for more details. ## License Licensed under [MIT License](https://github.com/Stranger6667/jsonschema/blob/master/LICENSE).