A JSON Schema validator implementation. It compiles schema into a validation tree to have validation as fast as possible.
Supported drafts:
- Draft 7 (except optional
idn-hostname.json
andformat_email.json
test cases) - Draft 6 (except optional
format_email.json
test case) - Draft 4 (except optional
bignum.json
andformat_email.json
test cases)
# Cargo.toml
jsonschema = "0.8"
To validate documents against some schema and get validation errors (if any):
use jsonschema::{JSONSchema, Draft, CompilationError};
use serde_json::json;
fn main() -> Result<(), CompilationError> {
let schema = json!({"maxLength": 5});
let instance = json!("foo");
let compiled = JSONSchema::compile(&schema)?;
let result = compiled.validate(&instance);
if let Err(errors) = result {
for error in errors {
println!("Validation error: {}", error);
println!("Instance path: {}", error.instance_path);
}
}
Ok(())
}
Each error has an instance_path
attribute that indicates the path to the erroneous part within the validated instance.
It could be transformed to JSON Pointer via .to_string()
or to Vec<String>
via .into_vec()
.
If you only need to know whether document is valid or not (which is faster):
use jsonschema::is_valid;
use serde_json::json;
fn main() {
let schema = json!({"maxLength": 5});
let instance = json!("foo");
assert!(is_valid(&schema, &instance));
}
Or use a compiled schema (preferred):
use jsonschema::{JSONSchema, Draft, CompilationError};
use serde_json::json;
fn main() -> Result<(), CompilationError> {
let schema = json!({"maxLength": 5});
let instance = json!("foo");
// Draft is detected automatically
// with fallback to Draft7
let compiled = JSONSchema::compile(&schema)?;
assert!(compiled.is_valid(&instance));
Ok(())
}
- Python - See the
./bindings/python
directory - Ruby - a crate by @driv3r
There is a comparison with other JSON Schema validators written in Rust - jsonschema_valid==0.4.0
and valico==3.5.0
.
Test machine i8700K (12 cores), 32GB RAM.
Schemas & input values:
- Big valid input. It is an Open API 2.0 schema for Kubernetes which is ~3.15 MB (
kubernetes.json
andswagger.json
files) - Small valid input (
small_schema.json
andsmall_valid.json
) - Small invalid input (
small_schema.json
andsmall_invalid.json
)
Ratios are given against compiled JSONSchema
using its validate
. The is_valid
method is faster, but gives only a boolean return value:
Case | jsonschema_valid | valico | jsonschema.validate | jsonschema.is_valid |
---|---|---|---|---|
Big valid | - | 95.008 ms (x12.46) | 7.62 ms | 5.785 ms (x0.75) |
Small valid | 2.04 us (x5.39) | 3.67 us (x9.70) | 378.21 ns | 113.3 ns (x0.29) |
Small invalid | 397.52 ns (x0.76) | 3.73 us (x7.19) | 518.70 ns | 5.53 ns (x0.01) |
Unfortunately, jsonschema_valid
mistakenly considers the Kubernetes Open API schema as invalid and therefore can't be compared with other libraries in this case.
You can find benchmark code in benches/jsonschema.rs
, Rust version is 1.49
.
NOTE. This library is in early development.
If you have anything to discuss regarding this library, please, join our gitter!