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
use crate::Error;
use jsonschema::{self, output::Output};
use serde_json::Value;
#[derive(Debug)]
/// Validator for cql2 json schema
pub struct Validator {
validator: jsonschema::Validator,
}
impl Validator {
/// Instantiate Validator instance loading schema.
pub fn new() -> Result<Self, Error> {
let schema_str = include_str!("cql2.json");
let schema_json: Value = serde_json::from_str(schema_str)?;
let validator =
jsonschema::validator_for(&schema_json).expect("Could not construct schema validator.");
Ok(Validator { validator })
}
/// Validate CQL2 Json
pub fn is_valid(&self, v: &Value) -> bool {
self.validator.is_valid(v)
}
/// Validates a [serde_json::Value].
///
/// # Examples
///
/// ```
/// use cql2::Validator;
/// use serde_json::json;
///
/// let validator = Validator::new().unwrap();
///
/// let valid = json!({
/// "op": "=",
/// "args": [
/// { "property": "landsat:scene_id" },
/// "LC82030282019133LGN00"
/// ]
/// });
/// validator.validate(&valid).unwrap();
///
/// let invalid = json!({
/// "op": "t_before",
/// "args": [{"property": "updated_at"}, {"timestamp": "invalid-timestamp"}],
/// });
/// validator.validate(&invalid).unwrap_err();
/// ```
pub fn validate<'i>(&self, v: &'i Value) -> Result<(), jsonschema::ValidationError<'i>> {
self.validator.validate(v)
}
/// Apply a validator
pub fn apply<'a, 'b>(&'a self, v: &'b Value) -> Output<'a, 'b> {
self.validator.apply(v)
}
/// Iterate through validation errors
pub fn iter_errors<'i>(&'i self, v: &'i Value) -> jsonschema::ErrorIterator<'i> {
self.validator.iter_errors(v)
}
}