Go to file
Dmitry Dygalo ce7ff12a83
Release 0.2.0
2020-03-30 10:30:53 +02:00
benches chore: Add more benchmarks 2020-03-29 14:52:26 +02:00
draft Update Cargo.toml 2020-03-29 11:08:50 +02:00
src chore: Expect `DEFAULT_ROOT_URL` to be valid 2020-03-30 10:00:10 +02:00
tests Initial commit 2020-03-25 18:04:54 +01:00
.gitignore Initial commit 2020-03-25 18:04:54 +01:00
.gitmodules Initial commit 2020-03-25 18:04:54 +01:00
Cargo.toml Release 0.2.0 2020-03-30 10:30:53 +02:00
LICENSE Add LICENSE 2020-03-25 18:08:05 +01:00
README.md docs: Add example 2020-03-29 10:50:28 +02:00

README.md

jsonschema

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

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");
    let compiled = JSONSchema::compile(&schema, None);  // Draft is detected automatically with fallback to Draft7
    assert!(compiled.is_valid(&instance));
}

NOTE. This library is in early development.