Skip to main content

valico/json_schema/keywords/
maxmin_items.rs

1use serde_json::Value;
2
3use super::super::schema;
4use super::super::validators;
5
6kw_minmax_integer!(MaxItems, "maxItems");
7kw_minmax_integer!(MinItems, "minItems");
8
9#[cfg(test)]
10use super::super::builder;
11#[cfg(test)]
12use super::super::scope;
13#[cfg(test)]
14use serde_json::to_value;
15
16#[test]
17fn validate_max_items() {
18    let mut scope = scope::Scope::new();
19    let schema = scope
20        .compile_and_return(
21            builder::schema(|s| {
22                s.max_items(5u64);
23            })
24            .into_json(),
25            true,
26        )
27        .ok()
28        .unwrap();
29
30    assert_eq!(
31        schema.validate(&to_value([1, 2, 3, 4]).unwrap()).is_valid(),
32        true
33    );
34    assert_eq!(
35        schema
36            .validate(&to_value([1, 2, 3, 4, 5]).unwrap())
37            .is_valid(),
38        true
39    );
40    assert_eq!(
41        schema
42            .validate(&to_value([1, 2, 3, 4, 5, 6]).unwrap())
43            .is_valid(),
44        false
45    );
46}
47
48#[test]
49fn malformed_max_items() {
50    let mut scope = scope::Scope::new();
51
52    assert!(scope
53        .compile_and_return(
54            jsonway::object(|schema| {
55                schema.set("maxItems", to_value(-1).unwrap());
56            })
57            .unwrap(),
58            true
59        )
60        .is_err());
61
62    assert!(scope
63        .compile_and_return(
64            jsonway::object(|schema| {
65                schema.set("maxItems", to_value("").unwrap());
66            })
67            .unwrap(),
68            true
69        )
70        .is_err());
71
72    assert!(scope
73        .compile_and_return(
74            jsonway::object(|schema| {
75                schema.set("maxItems", to_value(1.1).unwrap());
76            })
77            .unwrap(),
78            true
79        )
80        .is_err());
81}
82
83#[test]
84fn validate_min_items() {
85    let mut scope = scope::Scope::new();
86    let schema = scope
87        .compile_and_return(
88            builder::schema(|s| {
89                s.min_items(5u64);
90            })
91            .into_json(),
92            true,
93        )
94        .ok()
95        .unwrap();
96
97    assert_eq!(
98        schema.validate(&to_value([1, 2, 3, 4]).unwrap()).is_valid(),
99        false
100    );
101    assert_eq!(
102        schema
103            .validate(&to_value([1, 2, 3, 4, 5]).unwrap())
104            .is_valid(),
105        true
106    );
107    assert_eq!(
108        schema
109            .validate(&to_value([1, 2, 3, 4, 5, 6]).unwrap())
110            .is_valid(),
111        true
112    );
113}
114
115#[test]
116fn malformed_min_items() {
117    let mut scope = scope::Scope::new();
118
119    assert!(scope
120        .compile_and_return(
121            jsonway::object(|schema| {
122                schema.set("minItems", to_value(-1).unwrap());
123            })
124            .unwrap(),
125            true
126        )
127        .is_err());
128
129    assert!(scope
130        .compile_and_return(
131            jsonway::object(|schema| {
132                schema.set("minItems", to_value("").unwrap());
133            })
134            .unwrap(),
135            true
136        )
137        .is_err());
138
139    assert!(scope
140        .compile_and_return(
141            jsonway::object(|schema| {
142                schema.set("minItems", to_value(1.1).unwrap());
143            })
144            .unwrap(),
145            true
146        )
147        .is_err());
148}