Go to file
Wolfgang Walther 17643fac76 Fetch git submodule with https instead of ssh 2021-02-15 10:07:35 +01:00
.github ci: Add coverage job 2021-02-03 14:41:09 +01:00
bindings/python docs(python): Fix the mentioned `fastjsonschema` version 2021-02-03 15:56:48 +01:00
jsonschema chore(rust): Release 0.6.0 2021-02-03 14:50:18 +01:00
perf-helpers fix: Sdist Python packaging 2021-01-29 09:52:12 +01:00
.gitignore fix: Sdist Python packaging 2021-01-29 09:52:12 +01:00
.gitmodules Fetch git submodule with https instead of ssh 2021-02-15 10:07:35 +01:00
.pre-commit-config.yaml chore: Update pre-commit 2020-08-28 21:39:41 +02:00
.yamllint feat: FFI-based Python bindings 2020-06-09 17:47:49 +02:00
CHANGELOG.md chore(rust): Release 0.6.0 2021-02-03 14:50:18 +01:00
CODE_OF_CONDUCT.md
LICENSE feat: FFI-based Python bindings 2020-06-09 17:47:49 +02:00
README.md docs(rust): Update README 2021-02-04 11:47:04 +01:00
codecov.yaml chore: Disable codecov action checks 2021-01-28 14:48:04 +01:00

README.md

jsonschema

ci codecov Crates.io docs.rs gitter

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, float_overflow.json and format_email.json test cases)
  • Draft 6 (except optional float_overflow.json and format_email.json test cases)
  • Draft 4 (except optional bignum.json, float_overflow.json and format_email.json test cases)
# Cargo.toml
jsonschema = "0.6"

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)
        }
    }
    Ok(())
}

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(())
}

Bindings

  • Python - See the ./bindings/python directory
  • Ruby - a crate by @driv3r

Performance

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 and swagger.json files)
  • Small valid input (small_schema.json and small_valid.json)
  • Small invalid input (small_schema.json and small_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 (x13.07) 7.264 ms 5.974 ms (x0.82)
Small valid 2.04 us (x5.45) 3.67 us (x9.81) 373.91 ns 119.02 ns (x0.31)
Small invalid 397.52 ns (x0.82) 3.73 us (x7.74) 481.33 ns 5.26 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.

Support

If you have anything to discuss regarding this library, please, join our gitter!