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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! # 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);
//!
//! This library is functional and ready for use, but its API is still evolving to the 1.0 API.
//!
//! ## Usage Examples:
//! A schema can be compiled with two main flavours:
//!  * using default configurations
//! ```rust
//! # use jsonschema::JSONSchema;
//! # use serde_json::json;
//! # fn foo() {
//! # let schema = json!({"maxLength": 5});
//! let compiled_schema = JSONSchema::compile(&schema).expect("A valid schema");
//! # }
//! ```
//!  * using custom configurations (such as define a Draft version)
//! ```rust
//! # use jsonschema::{Draft, JSONSchema};
//! # use serde_json::json;
//! # fn foo() {
//! # let schema = json!({"maxLength": 5});
//! let compiled_schema = JSONSchema::options()
//!     .with_draft(Draft::Draft7)
//!     .compile(&schema)
//!     .expect("A valid schema");
//! # }
//! ```
//!
//! ## Example (CLI tool to highlight print errors)
//! ```rust
//! use jsonschema::{Draft, JSONSchema};
//! use serde_json::json;
//!
//! let schema = json!({"maxLength": 5});
//! let instance = json!("foo");
//! let compiled = JSONSchema::options()
//!     .with_draft(Draft::Draft7)
//!     .compile(&schema)
//!     .expect("A valid schema");
//! let result = compiled.validate(&instance);
//! if let Err(errors) = result {
//!     for error in errors {
//!         println!("Validation error: {}", error);
//!         println!("Instance path: {}", error.instance_path);
//!     }
//! }
//! ```
//! Each error has an `instance_path` attribute that indicates the path to the erroneous part within the validated instance.
//! It could be transformed to JSON Pointer via `.to_string()` or to `Vec<String>` via `.into_vec()`.
#![warn(
    clippy::cast_possible_truncation,
    clippy::doc_markdown,
    clippy::explicit_iter_loop,
    clippy::map_unwrap_or,
    clippy::match_same_arms,
    clippy::needless_borrow,
    clippy::needless_pass_by_value,
    clippy::print_stdout,
    clippy::redundant_closure,
    clippy::trivially_copy_pass_by_ref,
    clippy::missing_const_for_fn,
    clippy::unseparated_literal_suffix,
    missing_debug_implementations,
    missing_docs,
    trivial_casts,
    trivial_numeric_casts,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications,
    unreachable_pub,
    variant_size_differences
)]
#![allow(
    clippy::unnecessary_wraps,
    clippy::upper_case_acronyms,
    clippy::needless_collect
)]
#![cfg_attr(not(test), allow(clippy::integer_arithmetic, clippy::unwrap_used))]
mod compilation;
mod content_encoding;
mod content_media_type;
pub mod error;
mod keywords;
pub mod output;
pub mod paths;
pub mod primitive_type;
mod resolver;
mod schema_node;
mod schemas;
mod validator;

pub use compilation::{options::CompilationOptions, JSONSchema};
pub use error::{ErrorIterator, ValidationError};
pub use resolver::{SchemaResolver, SchemaResolverError};
pub use schemas::Draft;

use serde_json::Value;

/// A shortcut for validating `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));
/// ```
///
/// This function panics if an invalid schema is passed.
#[must_use]
#[inline]
pub fn is_valid(schema: &Value, instance: &Value) -> bool {
    let compiled = JSONSchema::compile(schema).expect("Invalid schema");
    compiled.is_valid(instance)
}

#[cfg(test)]
pub(crate) mod tests_util {
    use super::JSONSchema;
    use crate::ValidationError;
    use serde_json::Value;

    pub(crate) fn is_not_valid(schema: &Value, instance: &Value) {
        let compiled = JSONSchema::compile(schema).unwrap();
        assert!(
            !compiled.is_valid(instance),
            "{} should not be valid (via is_valid)",
            instance
        );
        assert!(
            compiled.validate(instance).is_err(),
            "{} should not be valid (via validate)",
            instance
        );
        assert!(
            !compiled.apply(instance).basic().is_valid(),
            "{} should not be valid (via apply)",
            instance
        );
    }

    pub(crate) fn expect_errors(schema: &Value, instance: &Value, errors: &[&str]) {
        assert_eq!(
            JSONSchema::compile(schema)
                .expect("Should be a valid schema")
                .validate(instance)
                .expect_err(format!("{} should not be valid", instance).as_str())
                .into_iter()
                .map(|e| e.to_string())
                .collect::<Vec<String>>(),
            errors
        )
    }

    pub(crate) fn is_valid(schema: &Value, instance: &Value) {
        let compiled = JSONSchema::compile(schema).unwrap();
        assert!(
            compiled.is_valid(instance),
            "{} should be valid (via is_valid)",
            instance
        );
        assert!(
            compiled.validate(instance).is_ok(),
            "{} should be valid (via validate)",
            instance
        );
        assert!(
            compiled.apply(instance).basic().is_valid(),
            "{} should be valid (via apply)",
            instance
        );
    }

    pub(crate) fn validate(schema: &Value, instance: &Value) -> ValidationError<'static> {
        let compiled = JSONSchema::compile(schema).unwrap();
        let err = compiled
            .validate(instance)
            .expect_err("Should be an error")
            .next()
            .expect("Should be an error")
            .into_owned();
        err
    }

    pub(crate) fn assert_schema_path(schema: &Value, instance: &Value, expected: &str) {
        let error = validate(schema, instance);
        assert_eq!(error.schema_path.to_string(), expected)
    }

    pub(crate) fn assert_schema_paths(schema: &Value, instance: &Value, expected: &[&str]) {
        let compiled = JSONSchema::compile(schema).unwrap();
        let errors = compiled.validate(instance).expect_err("Should be an error");
        for (error, schema_path) in errors.zip(expected) {
            assert_eq!(error.schema_path.to_string(), *schema_path)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{is_valid, Draft, JSONSchema};
    use serde_json::json;
    use test_case::test_case;

    #[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));
    }

    #[test_case(Draft::Draft4)]
    #[test_case(Draft::Draft6)]
    #[test_case(Draft::Draft7)]
    fn meta_schemas(draft: Draft) {
        // See GH-258
        for schema in [json!({"enum": [0, 0.0]}), json!({"enum": []})] {
            assert!(JSONSchema::options()
                .with_draft(draft)
                .compile(&schema)
                .is_ok())
        }
    }

    #[test]
    fn incomplete_escape_in_pattern() {
        // See GH-253
        let schema = json!({"pattern": "\\u"});
        assert!(JSONSchema::compile(&schema).is_err())
    }
}