valico/json_schema/keywords/
maxmin_properties.rs1use serde_json::Value;
2
3use super::super::schema;
4use super::super::validators;
5
6kw_minmax_integer!(MaxProperties, "maxProperties");
7kw_minmax_integer!(MinProperties, "minProperties");
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_properties() {
18 let mut scope = scope::Scope::new();
19 let schema = scope
20 .compile_and_return(
21 builder::schema(|s| {
22 s.max_properties(2u64);
23 })
24 .into_json(),
25 true,
26 )
27 .ok()
28 .unwrap();
29
30 assert_eq!(
31 schema
32 .validate(
33 &jsonway::object(|obj| {
34 obj.set("p1", 0);
35 })
36 .unwrap()
37 )
38 .is_valid(),
39 true
40 );
41
42 assert_eq!(
43 schema
44 .validate(
45 &jsonway::object(|obj| {
46 obj.set("p1", 0);
47 obj.set("p2", 0);
48 })
49 .unwrap()
50 )
51 .is_valid(),
52 true
53 );
54
55 assert_eq!(
56 schema
57 .validate(
58 &jsonway::object(|obj| {
59 obj.set("p1", 0);
60 obj.set("p2", 0);
61 obj.set("p3", 0);
62 })
63 .unwrap()
64 )
65 .is_valid(),
66 false
67 );
68}
69
70#[test]
71fn malformed_max_properties() {
72 let mut scope = scope::Scope::new();
73
74 assert!(scope
75 .compile_and_return(
76 jsonway::object(|schema| {
77 schema.set("maxProperties", to_value(-1).unwrap());
78 })
79 .unwrap(),
80 true
81 )
82 .is_err());
83
84 assert!(scope
85 .compile_and_return(
86 jsonway::object(|schema| {
87 schema.set("maxProperties", to_value("").unwrap());
88 })
89 .unwrap(),
90 true
91 )
92 .is_err());
93
94 assert!(scope
95 .compile_and_return(
96 jsonway::object(|schema| {
97 schema.set("maxProperties", to_value(1.1).unwrap());
98 })
99 .unwrap(),
100 true
101 )
102 .is_err());
103}
104
105#[test]
106fn validate_min_properties() {
107 let mut scope = scope::Scope::new();
108 let schema = scope
109 .compile_and_return(
110 builder::schema(|s| {
111 s.min_properties(2u64);
112 })
113 .into_json(),
114 true,
115 )
116 .ok()
117 .unwrap();
118
119 assert_eq!(
120 schema
121 .validate(
122 &jsonway::object(|obj| {
123 obj.set("p1", 0);
124 })
125 .unwrap()
126 )
127 .is_valid(),
128 false
129 );
130
131 assert_eq!(
132 schema
133 .validate(
134 &jsonway::object(|obj| {
135 obj.set("p1", 0);
136 obj.set("p2", 0);
137 })
138 .unwrap()
139 )
140 .is_valid(),
141 true
142 );
143
144 assert_eq!(
145 schema
146 .validate(
147 &jsonway::object(|obj| {
148 obj.set("p1", 0);
149 obj.set("p2", 0);
150 obj.set("p3", 0);
151 })
152 .unwrap()
153 )
154 .is_valid(),
155 true
156 );
157}
158
159#[test]
160fn malformed_min_properties() {
161 let mut scope = scope::Scope::new();
162
163 assert!(scope
164 .compile_and_return(
165 jsonway::object(|schema| {
166 schema.set("minProperties", to_value(-1).unwrap());
167 })
168 .unwrap(),
169 true
170 )
171 .is_err());
172
173 assert!(scope
174 .compile_and_return(
175 jsonway::object(|schema| {
176 schema.set("minProperties", to_value("").unwrap());
177 })
178 .unwrap(),
179 true
180 )
181 .is_err());
182
183 assert!(scope
184 .compile_and_return(
185 jsonway::object(|schema| {
186 schema.set("minProperties", to_value(1.1).unwrap());
187 })
188 .unwrap(),
189 true
190 )
191 .is_err());
192}