valico/json_schema/keywords/
maxmin_length.rs

1use serde_json::{Value};
2
3use super::super::schema;
4use super::super::validators;
5
6macro_rules! kw_minmax_integer{
7    ($name:ident, $keyword:expr) => {
8        #[allow(missing_copy_implementations)]
9        pub struct $name;
10        impl super::Keyword for $name {
11            fn compile(&self, def: &Value, ctx: &schema::WalkContext) -> super::KeywordResult {
12                let length = keyword_key_exists!(def, $keyword);
13
14                if length.is_number() {
15                    let length_val = length.as_f64().unwrap();
16                    if length_val >= 0f64 && length_val.fract() == 0f64 {
17                        Ok(Some(Box::new(validators::$name {
18                            length: length_val as u64
19                        })))
20                    } else {
21                        Err(schema::SchemaError::Malformed {
22                            path: ctx.fragment.join("/"),
23                            detail: "The value MUST be a positive integer or zero".to_string()
24                        })
25                    }
26                } else {
27                    Err(schema::SchemaError::Malformed {
28                        path: ctx.fragment.join("/"),
29                        detail: "The value MUST be a positive integer or zero".to_string()
30                    })
31                }
32            }
33        }
34    }
35}
36
37kw_minmax_integer!(MaxLength, "maxLength");
38kw_minmax_integer!(MinLength, "minLength");
39
40#[cfg(test)] use super::super::scope;
41#[cfg(test)] use jsonway;
42#[cfg(test)] use super::super::builder;
43#[cfg(test)] use serde_json::to_value;
44
45#[test]
46fn validate_max_length() {
47    let mut scope = scope::Scope::new();
48    let schema = scope.compile_and_return(builder::schema(|s| {
49        s.max_length(5u64);
50    }).into_json(), true).ok().unwrap();;
51
52    assert_eq!(schema.validate(&to_value(&"1234").unwrap()).is_valid(), true);
53    assert_eq!(schema.validate(&to_value(&"12345").unwrap()).is_valid(), true);
54    assert_eq!(schema.validate(&to_value(&"123456").unwrap()).is_valid(), false);
55}
56
57#[test]
58fn malformed_max_length() {
59    let mut scope = scope::Scope::new();
60
61    assert!(scope.compile_and_return(jsonway::object(|schema| {
62        schema.set("maxLength", to_value(&-1).unwrap());
63    }).unwrap(), true).is_err());
64
65    assert!(scope.compile_and_return(jsonway::object(|schema| {
66        schema.set("maxLength", to_value(&"").unwrap());
67    }).unwrap(), true).is_err());
68
69    assert!(scope.compile_and_return(jsonway::object(|schema| {
70        schema.set("maxLength", to_value(&1.1).unwrap());
71    }).unwrap(), true).is_err());
72}
73
74#[test]
75fn validate_min_length() {
76    let mut scope = scope::Scope::new();
77    let schema = scope.compile_and_return(builder::schema(|s| {
78        s.min_length(5u64);
79    }).into_json(), true).ok().unwrap();;
80
81    assert_eq!(schema.validate(&to_value(&"1234").unwrap()).is_valid(), false);
82    assert_eq!(schema.validate(&to_value(&"12345").unwrap()).is_valid(), true);
83    assert_eq!(schema.validate(&to_value(&"123456").unwrap()).is_valid(), true);
84}
85
86#[test]
87fn malformed_min_length() {
88    let mut scope = scope::Scope::new();
89
90    assert!(scope.compile_and_return(jsonway::object(|schema| {
91        schema.set("minLength", to_value(&-1).unwrap());
92    }).unwrap(), true).is_err());
93
94    assert!(scope.compile_and_return(jsonway::object(|schema| {
95        schema.set("minLength", to_value(&"").unwrap());
96    }).unwrap(), true).is_err());
97
98    assert!(scope.compile_and_return(jsonway::object(|schema| {
99        schema.set("minLength", to_value(&1.1).unwrap());
100    }).unwrap(), true).is_err());
101}