Crate jsonschema[][src]

jsonschema

A crate for performing fast JSON Schema validation. It is fast due to schema compilation into a validation tree, which reduces runtime costs for working with schema parameters.

Supports:

  • JSON Schema drafts 4, 6, 7 (except some optional test cases);
  • Loading remote documents via HTTP(S);

Usage Examples:

A schema can be compiled with two main flavours:

  • using default configurations
let compiled_schema = JSONSchema::compile(&schema)?;
  • using custom configurations (such as define a Draft version)
let compiled_schema = JSONSchema::options()
    .with_draft(Draft::Draft7)
    .compile(&schema)?;

Example (CLI tool to highlight print errors)

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

fn main() -> Result<(), CompilationError> {
    let schema = json!({"maxLength": 5});
    let instance = json!("foo");
    let compiled = JSONSchema::options()
        .with_draft(Draft::Draft7)
        .compile(&schema)?;
    let result = compiled.validate(&instance);
    if let Err(errors) = result {
        for error in errors {
            println!("Validation error: {}", error)
        }
    }
    Ok(())
}

Structs

CompilationOptions

Full configuration to guide the JSONSchema compilation.

JSONSchema

The structure that holds a JSON Schema compiled into a validation tree

ValidationError

An error that can occur during validation.

Enums

CompilationError

The error type that happens when the input schema is not valid.

Draft

JSON Schema Draft version

Functions

is_valid

A shortcut for validating instance against schema. Draft version is detected automatically.

Type Definitions

ErrorIterator

An iterator over instances of ValidationError that represent validation error for the input instance.