valico/json_schema/keywords/
maxmin_properties.rs
1use 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)] use super::super::scope;
10#[cfg(test)] use jsonway;
11#[cfg(test)] use super::super::builder;
12#[cfg(test)] use serde_json::to_value;
13
14#[test]
15fn validate_max_properties() {
16 let mut scope = scope::Scope::new();
17 let schema = scope.compile_and_return(builder::schema(|s| {
18 s.max_properties(2u64);
19 }).into_json(), true).ok().unwrap();
20
21 assert_eq!(schema.validate(&jsonway::object(|obj| {
22 obj.set("p1", 0);
23 }).unwrap()).is_valid(), true);
24
25 assert_eq!(schema.validate(&jsonway::object(|obj| {
26 obj.set("p1", 0);
27 obj.set("p2", 0);
28 }).unwrap()).is_valid(), true);
29
30 assert_eq!(schema.validate(&jsonway::object(|obj| {
31 obj.set("p1", 0);
32 obj.set("p2", 0);
33 obj.set("p3", 0);
34 }).unwrap()).is_valid(), false);
35}
36
37#[test]
38fn malformed_max_properties() {
39 let mut scope = scope::Scope::new();
40
41 assert!(scope.compile_and_return(jsonway::object(|schema| {
42 schema.set("maxProperties", to_value(&-1).unwrap());
43 }).unwrap(), true).is_err());
44
45 assert!(scope.compile_and_return(jsonway::object(|schema| {
46 schema.set("maxProperties", to_value(&"").unwrap());
47 }).unwrap(), true).is_err());
48
49 assert!(scope.compile_and_return(jsonway::object(|schema| {
50 schema.set("maxProperties", to_value(&1.1).unwrap());
51 }).unwrap(), true).is_err());
52}
53
54#[test]
55fn validate_min_properties() {
56 let mut scope = scope::Scope::new();
57 let schema = scope.compile_and_return(builder::schema(|s| {
58 s.min_properties(2u64);
59 }).into_json(), true).ok().unwrap();;
60
61 assert_eq!(schema.validate(&jsonway::object(|obj| {
62 obj.set("p1", 0);
63 }).unwrap()).is_valid(), false);
64
65 assert_eq!(schema.validate(&jsonway::object(|obj| {
66 obj.set("p1", 0);
67 obj.set("p2", 0);
68 }).unwrap()).is_valid(), true);
69
70 assert_eq!(schema.validate(&jsonway::object(|obj| {
71 obj.set("p1", 0);
72 obj.set("p2", 0);
73 obj.set("p3", 0);
74 }).unwrap()).is_valid(), true);
75}
76
77#[test]
78fn malformed_min_properties() {
79 let mut scope = scope::Scope::new();
80
81 assert!(scope.compile_and_return(jsonway::object(|schema| {
82 schema.set("minProperties", to_value(&-1).unwrap());
83 }).unwrap(), true).is_err());
84
85 assert!(scope.compile_and_return(jsonway::object(|schema| {
86 schema.set("minProperties", to_value(&"").unwrap());
87 }).unwrap(), true).is_err());
88
89 assert!(scope.compile_and_return(jsonway::object(|schema| {
90 schema.set("minProperties", to_value(&1.1).unwrap());
91 }).unwrap(), true).is_err());
92}