Go to file
Dmitry Dygalo 29c2dab3aa
Create CODE_OF_CONDUCT.md
2020-06-02 10:54:06 +02:00
.github/workflows feat: Allow github to run benchmarks if [bench] is present in the commit message 2020-05-23 20:23:56 +02:00
benches feat: Allow github to run benchmarks if [bench] is present in the commit message 2020-05-23 20:23:56 +02:00
draft style: A rust library should not pin exact versions but rather specify bounds 2020-05-24 01:34:13 +02:00
perf-helpers feat: Create perf-helpers subproject to help in running code profiling 2020-05-25 11:17:44 +02:00
src fix: Precision loss in `minimum`, `maximum`, `exclusiveMinimum` and `exclusiveMaximum` validators 2020-05-29 10:45:06 +02:00
tests chore: Update test suite 2020-05-23 21:36:55 +02:00
.gitignore
.gitmodules chore: Convert gitmodule to https 2020-05-22 21:27:52 +02:00
CHANGELOG.md fix: Precision loss in `minimum`, `maximum`, `exclusiveMinimum` and `exclusiveMaximum` validators 2020-05-29 10:45:06 +02:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md 2020-06-02 10:54:06 +02:00
Cargo.toml fix: Precision loss in `minimum`, `maximum`, `exclusiveMinimum` and `exclusiveMaximum` validators 2020-05-29 10:45:06 +02:00
LICENSE
README.md docs: Add link & badge for docs 2020-05-26 07:36:56 +02:00

README.md

jsonschema

ci codecov Crates.io docs.rs

A JSON Schema validator implementation. It compiles schema into a validation tree to have validation as fast as possible.

Supported drafts:

  • Draft 7
  • Draft 6
  • Draft 4 (except optional bignum.json test case)
# Cargo.toml
jsonschema = "0.2"

To validate documents against some schema and get validation errors (if any):

use jsonschema::{JSONSchema, Draft};
use serde_json::json;

fn main() {
    let schema = json!({"maxLength": 5});
    let instance = json!("foo");
    let compiled = JSONSchema::compile(&schema, Some(Draft::Draft7));
    let result = compiled.validate(&instance);
    if let Err(errors) = result {
        for error in errors {
            println!("Validation error: {}", error)
        }   
    }
}

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};
use serde_json::json;

fn main() {
    let schema = json!({"maxLength": 5});
    let instance = json!("foo");
    // Draft is detected automatically with fallback to Draft7
    let compiled = JSONSchema::compile(&schema, None);
    assert!(compiled.is_valid(&instance));
}

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 (not compiled) jsonschema (compiled)
Big valid 56.746 ms (x187.2) 149.49 ms (x493.17) 317.14 us (x1.04) 303.12 us
Small valid 2.23 us (x14.92) 3.87 us (x25.9) 3.76 us (x25.17) 149.38 ns
Small invalid 515.22 ns (x85.58) 4.08 us (x677.74) 3.63 us (x602.99) 6.02 ns

As you can see the compiled version is faster, especially for large inputs. However, not-compiled version is slower on smaller inputs than jsonschema_valid.

You can find benchmark code in benches/jsonschema.rs

NOTE. This library is in early development.