valico/json_schema/validators/
maxmin_length.rs

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