1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
//! # 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 6, 7 (all test cases); //! - Loading remote documents via HTTP(S); //! //! ## Example: //! //! ```rust //! 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, Some(Draft::Draft7))?; //! let result = compiled.validate(&instance); //! if let Err(errors) = result { //! for error in errors { //! println!("Validation error: {}", error) //! } //! } //! Ok(()) //! } //! //! ``` mod compilation; mod error; mod keywords; mod resolver; mod schemas; pub use compilation::JSONSchema; pub use error::{CompilationError, ErrorIterator, ValidationError}; pub use schemas::Draft; use serde_json::Value; #[macro_use] extern crate lazy_static; /// Validates `instance` against `schema`. Draft version is detected automatically. /// ```rust /// use jsonschema::is_valid; /// use serde_json::json; /// /// /// let schema = json!({"maxLength": 5}); /// let instance = json!("foo"); /// assert!(is_valid(&schema, &instance)); /// ``` pub fn is_valid(schema: &Value, instance: &Value) -> bool { let compiled = JSONSchema::compile(schema, None).expect("Invalid schema"); compiled.is_valid(instance) } #[cfg(test)] mod tests { use super::*; use serde_json::json; #[test] fn test_is_valid() { let schema = json!({"minLength": 5}); let valid = json!("foobar"); let invalid = json!("foo"); assert!(is_valid(&schema, &valid)); assert!(!is_valid(&schema, &invalid)); } }