| 1 | # jsonschema |
| 2 | |
| 3 | [<img alt="crates.io" src="https://img.shields.io/crates/v/jsonschema.svg?style=flat-square&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/jsonschema) |
| 4 | [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-jsonschema-66c2a5?style=flat-square&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/jsonschema) |
| 5 | [<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/Stranger6667/jsonschema/ci.yml?branch=master&style=flat-square" height="20">](https://github.com/Stranger6667/jsonschema/actions?query=branch%3Amaster) |
| 6 | [<img alt="codecov.io" src="https://img.shields.io/codecov/c/gh/Stranger6667/jsonschema?logo=codecov&style=flat-square&token=B1EnafGlRL" height="20">](https://app.codecov.io/github/Stranger6667/jsonschema) |
| 7 | [<img alt="Supported Dialects" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fbowtie.report%2Fbadges%2Frust-jsonschema%2Fsupported_versions.json&style=flat-square">](https://bowtie.report/#/implementations/rust-jsonschema) |
| 8 | |
| 9 | A high-performance JSON Schema validator for Rust. |
| 10 | |
| 11 | ```rust |
| 12 | use serde_json::json; |
| 13 | |
| 14 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 15 | let schema = json!({"maxLength": 5}); |
| 16 | let instance = json!("foo"); |
| 17 | |
| 18 | // One-off validation |
| 19 | assert!(jsonschema::is_valid(&schema, &instance)); |
| 20 | assert!(jsonschema::validate(&schema, &instance).is_ok()); |
| 21 | |
| 22 | // Build & reuse (faster) |
| 23 | let validator = jsonschema::validator_for(&schema)?; |
| 24 | |
| 25 | // Fail on first error |
| 26 | assert!(validator.validate(&instance).is_ok()); |
| 27 | |
| 28 | // Iterate over errors |
| 29 | for error in validator.iter_errors(&instance) { |
| 30 | eprintln!("Error: {error}"); |
| 31 | eprintln!("Location: {}", error.instance_path()); |
| 32 | } |
| 33 | |
| 34 | // Boolean result |
| 35 | assert!(validator.is_valid(&instance)); |
| 36 | |
| 37 | // Structured output (JSON Schema Output v1) |
| 38 | let evaluation = validator.evaluate(&instance); |
| 39 | for annotation in evaluation.iter_annotations() { |
| 40 | eprintln!( |
| 41 | "Annotation at {}: {:?}", |
| 42 | annotation.schema_location, |
| 43 | annotation.annotations.value() |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | Ok(()) |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | You also can use it from the command line via the [jsonschema-cli](https://github.com/Stranger6667/jsonschema/tree/master/crates/jsonschema-cli) crate. |
| 52 | |
| 53 | ```console |
| 54 | $ jsonschema-cli schema.json -i instance.json |
| 55 | ``` |
| 56 | |
| 57 | See more usage examples in the [documentation](https://docs.rs/jsonschema). |
| 58 | |
| 59 | > ⚠️ **Upgrading from older versions?** Check our [Migration Guide](https://github.com/Stranger6667/jsonschema/blob/master/MIGRATION.md) for key changes. |
| 60 | |
| 61 | ## Highlights |
| 62 | |
| 63 | - 📚 Full support for popular JSON Schema drafts |
| 64 | - 🔧 Custom keywords and format validators |
| 65 | - 🌐 Blocking & non-blocking remote reference fetching (network/file) |
| 66 | - 🎨 Structured Output v1 reports (flag/list/hierarchical) |
| 67 | - ✨ Meta-schema validation for schema documents, including custom metaschemas |
| 68 | - 🔗 Bindings for [Python](https://github.com/Stranger6667/jsonschema/tree/master/crates/jsonschema-py) |
| 69 | - 🚀 WebAssembly support |
| 70 | - 💻 Command Line Interface |
| 71 | |
| 72 | ### Supported drafts |
| 73 | |
| 74 | The following drafts are supported: |
| 75 | |
| 76 | - [](https://bowtie.report/#/implementations/rust-jsonschema) |
| 77 | - [](https://bowtie.report/#/implementations/rust-jsonschema) |
| 78 | - [](https://bowtie.report/#/implementations/rust-jsonschema) |
| 79 | - [](https://bowtie.report/#/implementations/rust-jsonschema) |
| 80 | - [](https://bowtie.report/#/implementations/rust-jsonschema) |
| 81 | |
| 82 | You can check the current status on the [Bowtie Report](https://bowtie.report/#/implementations/rust-jsonschema). |
| 83 | |
| 84 | ## Notable Users |
| 85 | |
| 86 | - Tauri: [Config validation](https://github.com/tauri-apps/tauri/blob/c901d9fdf932bf7c3c77e9d3097fabb1fe0712af/crates/tauri-cli/src/helpers/config.rs#L173) |
| 87 | - Apollo Router: [Config file validation](https://github.com/apollographql/router/blob/855cf6cc0757ca6176970ddf3ae8c98c87c632d1/apollo-router/src/configuration/schema.rs#L120) |
| 88 | - qsv: [CSV record validation with custom keyword & format validator](https://github.com/jqnatividad/qsv/blob/6b6985065a1270f767d881b13aa2a27fae1958fb/src/cmd/validate.rs#L630) |
| 89 | |
| 90 | ## Performance |
| 91 | |
| 92 | `jsonschema` outperforms other Rust JSON Schema validators in most scenarios: |
| 93 | |
| 94 | - Up to **20-470x** faster than `valico` and `jsonschema_valid` for complex schemas |
| 95 | - Generally **3-20x** faster than `boon` |
| 96 | |
| 97 | For detailed benchmarks, see our [full performance comparison](https://github.com/Stranger6667/jsonschema/tree/master/crates/benchmark-suite). |
| 98 | |
| 99 | ## Minimum Supported Rust Version (MSRV) |
| 100 | |
| 101 | This crate requires Rust 1.83.0 or later. |
| 102 | |
| 103 | ## Acknowledgements |
| 104 | |
| 105 | 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. |
| 106 | |
| 107 | ## Support |
| 108 | |
| 109 | If you have questions, need help, or want to suggest improvements, please use [GitHub Discussions](https://github.com/Stranger6667/jsonschema/discussions). |
| 110 | |
| 111 | ## Sponsorship |
| 112 | |
| 113 | If you find `jsonschema` useful, please consider [sponsoring its development](https://github.com/sponsors/Stranger6667). |
| 114 | |
| 115 | ## Contributing |
| 116 | |
| 117 | We welcome contributions! Here's how you can help: |
| 118 | |
| 119 | - Share your use cases |
| 120 | - Implement missing keywords |
| 121 | - Fix failing test cases from the [JSON Schema test suite](https://bowtie.report/#/implementations/rust-jsonschema) |
| 122 | |
| 123 | See [CONTRIBUTING.md](https://github.com/Stranger6667/jsonschema/blob/master/CONTRIBUTING.md) for more details. |
| 124 | |
| 125 | ## License |
| 126 | |
| 127 | Licensed under [MIT License](https://github.com/Stranger6667/jsonschema/blob/master/LICENSE). |