[][src]Crate jsonschema_equivalent


A JSON Schema optimiser library.

The main idea is to flatten the input schema and to remove keywords that are not providing any restriction on the schema. Possible examples are

  • {"type": "string", "minimum": 0} is equivalent to {"type": "string"} as minimum keyword applies only to numberic types
  • {"allOf": [{"type": "integer"}, {"type": "number"}]} is equivalent to {"type": "number"} as integer is included in number
  • {"allOf": [{"type": "integer"}, {"type": "string"}]} is equivalent to {"type": ["integer", "string"]}
  • ...

By flattening and removing extraneous/incongruent keywords we are able to provide a smaller and equivalent schema. Thanks to this, JSON validators can spend CPU cycles on verifying the components that are actually providing restriction on the schema instead of verifying conditions that we know a-priori not been applicable to certain contexts.

How to use

# Cargo.toml
jsonschema-equivalent = "0"

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

use jsonschema_equivalent::jsonschema_equivalent;
use serde_json::json;

let schema = json!({"type": "string", "minimum": 42});
println!("Original schema: {}", schema);
let equivalent_schema = jsonschema_equivalent(schema);
println!("Equivalent schema: {}", equivalent_schema);



Generate an equivalent schema to the schema provided as input


Optimise input schema by removing extraneous/incongruent keys replacing equivalent schemas with more performant ones to be validates against.