Code Monkey home page Code Monkey logo

jsonschema-rs's Introduction

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.