Skip to main content

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