valico/json_schema/validators/
maxmin_items.rs

1use serde_json::{Value};
2
3use super::super::errors;
4use super::super::scope;
5
6#[allow(missing_copy_implementations)]
7pub struct MaxItems {
8    pub length: u64
9}
10
11impl super::Validator for MaxItems {
12    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
13        let array = nonstrict_process!(val.as_array(), path);
14
15        if (array.len() as u64) <= self.length {
16            super::ValidationState::new()
17        } else {
18            val_error!(
19                errors::MaxItems {
20                    path: path.to_string()
21                }
22            )
23        }
24    }
25}
26
27#[allow(missing_copy_implementations)]
28pub struct MinItems {
29    pub length: u64
30}
31
32impl super::Validator for MinItems {
33    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
34        let array = nonstrict_process!(val.as_array(), path);
35
36        if (array.len() as u64) >= self.length {
37            super::ValidationState::new()
38        } else {
39            val_error!(
40                errors::MinItems {
41                    path: path.to_string()
42                }
43            )
44        }
45    }
46}