Skip to main content

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(
13        &self,
14        val: &Value,
15        path: &str,
16        _scope: &scope::Scope,
17        _: &super::ValidationState,
18    ) -> super::ValidationState {
19        let array = nonstrict_process!(val.as_array(), path);
20
21        if (array.len() as u64) <= self.length {
22            super::ValidationState::new()
23        } else {
24            val_error!(errors::MaxItems {
25                path: path.to_string()
26            })
27        }
28    }
29}
30
31#[allow(missing_copy_implementations)]
32pub struct MinItems {
33    pub length: u64,
34}
35
36impl super::Validator for MinItems {
37    fn validate(
38        &self,
39        val: &Value,
40        path: &str,
41        _scope: &scope::Scope,
42        _: &super::ValidationState,
43    ) -> super::ValidationState {
44        let array = nonstrict_process!(val.as_array(), path);
45
46        if (array.len() as u64) >= self.length {
47            super::ValidationState::new()
48        } else {
49            val_error!(errors::MinItems {
50                path: path.to_string()
51            })
52        }
53    }
54}