Go to file
Dmitry Dygalo f5be9508d0
fix: Sdist Python packaging
* chore: Move Python bindings to `bindings`

* chore: Move `jsonschema` into a separate directory

* chore: Move submodule

* ci: Fix the `coverage` job

* chore: Move submodule

* test: Disable some failing optional tests

* ci: Add tests for installing Python sdist

* fix: Sdist Python packaging

* chore: Remove coverage job

It doesn't support such project structure
Ref: https://github.com/actions-rs/grcov/issues/50
2021-01-29 09:52:12 +01:00
.github fix: Sdist Python packaging 2021-01-29 09:52:12 +01:00
bindings/python fix: Sdist Python packaging 2021-01-29 09:52:12 +01:00
jsonschema fix: Sdist Python packaging 2021-01-29 09:52:12 +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 fix: Sdist Python packaging 2021-01-29 09:52:12 +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 fix: Not necessary network requests for schemas with `$id` values with trailing `#` symbol 2021-01-28 16:59:06 +01:00
CODE_OF_CONDUCT.md
LICENSE feat: FFI-based Python bindings 2020-06-09 17:47:49 +02:00
README.md fix: Sdist Python packaging 2021-01-29 09:52:12 +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.4"

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 /python directory
  • Ruby - a crate by @driv3r

Performance

There is a comparison with other JSON Schema validators written in Rust - jsonschema_valid and valico.

Test machine i8700K (12 cores), 32GB RAM.

Performance of jsonschema::JSONSchema.is_valid. Ratios are given against compiled jsonschema:

  • Big valid input (canada_schema.json and canada.json)
  • Small valid input (small_schema.json and small_valid.json)
  • Small invalid input (small_schema.json and small_invalid.json)
Case jsonschema_valid valico jsonschema
Big valid 56.746 ms (x185.65) 149.49 ms (x489.07) 305.66 us
Small valid 2.23 us (x17.15) 3.87 us (x29.77) 129.97 ns
Small invalid 515.22 ns (x96.3) 4.08 us (x762.61) 5.35 ns

All libraries were used in their "compiled" form, where a validator is prepared before usage. Here is comparison when a validator is compiled every time.

Case jsonschema_valid valico jsonschema
Big valid 56.714 ms (x183.72) 146.82 ms (x475.62) 308.69 us
Small valid 3.02 us (x1.13) 118.09 us (x44.22) 2.67 us
Small invalid 1.17 us (x0.46) 81.95 us (x32.26) 2.54 us

You can find benchmark code in benches/jsonschema.rs, Rust version is 1.44

NOTE. This library is in early development.

Support

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