valico/json_schema/keywords/
maxmin.rs1use serde_json::{Value};
2
3use super::super::schema;
4use super::super::validators;
5
6macro_rules! kw_minmax{
7 ($name:ident, $keyword:expr, $exclusive: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 maybe_value = def.get($keyword);
13 let exclusive = def.get($exclusive);
14
15 if exclusive.is_some() {
16 if !maybe_value.is_some() {
17 return Err(schema::SchemaError::Malformed {
18 path: ctx.fragment.join("/"),
19 detail: "`exclusiveMinimum/exclusiveMaximum` can't go without minimum/maximum".to_string()
20 })
21 }
22 }
23
24 if maybe_value.is_some() {
25 let value = maybe_value.unwrap();
26 if value.is_number() {
27 let value = value.as_f64().unwrap();
28 Ok(Some(Box::new(validators::$name {
29 number: value,
30 exclusive: exclusive.is_some() &&
31 try!(exclusive.unwrap()
32 .as_bool()
33 .ok_or_else(||
34 schema::SchemaError::Malformed {
35 path: ctx.fragment.join("/"),
36 detail: "`exclusiveMaximum/exclusiveMaximum` must be boolean".to_string()
37 }
38 ))
39 })))
40 } else {
41 Err(schema::SchemaError::Malformed {
42 path: ctx.fragment.join("/"),
43 detail: "the `minimum/maximum` value must be a number".to_string()
44 })
45 }
46 } else {
47 Ok(None)
48 }
49 }
50 }
51 }
52}
53
54kw_minmax!(Minimum, "minimum", "exclusiveMinimum");
55kw_minmax!(Maximum, "maximum", "exclusiveMaximum");
56
57#[cfg(test)] use super::super::scope;
58#[cfg(test)] use jsonway;
59#[cfg(test)] use super::super::builder;
60#[cfg(test)] use serde_json::to_value;
61
62#[test]
63fn validate_maximum() {
64 let mut scope = scope::Scope::new();
65 let schema = scope.compile_and_return(builder::schema(|s| {
66 s.maximum(10f64, false);
67 }).into_json(), true).ok().unwrap();
68
69 assert_eq!(schema.validate(&to_value(&9).unwrap()).is_valid(), true);
70 assert_eq!(schema.validate(&to_value(&10).unwrap()).is_valid(), true);
71 assert_eq!(schema.validate(&to_value(&11).unwrap()).is_valid(), false);
72}
73
74#[test]
75fn validate_exclusive_maximum() {
76 let mut scope = scope::Scope::new();
77 let schema = scope.compile_and_return(builder::schema(|s| {
78 s.maximum(10f64, true);
79 }).into_json(), true).ok().unwrap();
80
81 assert_eq!(schema.validate(&to_value(&9).unwrap()).is_valid(), true);
82 assert_eq!(schema.validate(&to_value(&10).unwrap()).is_valid(), false);
83 assert_eq!(schema.validate(&to_value(&11).unwrap()).is_valid(), false);
84}
85
86#[test]
87fn mailformed_maximum() {
88 let mut scope = scope::Scope::new();
89
90 assert!(scope.compile_and_return(jsonway::object(|schema| {
91 schema.set("maximum", true);
92 }).unwrap(), true).is_err());
93}
94
95#[test]
96fn mailformed_exclusive_maximum() {
97 let mut scope = scope::Scope::new();
98
99 assert!(scope.compile_and_return(jsonway::object(|schema| {
100 schema.set("exclusiveMaximum", true);
101 }).unwrap(), true).is_err());
102
103 assert!(scope.compile_and_return(jsonway::object(|schema| {
104 schema.set("maximum", 10);
105 schema.set("exclusiveMaximum", "".to_string());
106 }).unwrap(), true).is_err());
107}
108
109#[test]
110fn validate_minumum() {
111 let mut scope = scope::Scope::new();
112 let schema = scope.compile_and_return(builder::schema(|s| {
113 s.minimum(10f64, false);
114 }).into_json(), true).ok().unwrap();
115
116 assert_eq!(schema.validate(&to_value(&9).unwrap()).is_valid(), false);
117 assert_eq!(schema.validate(&to_value(&10).unwrap()).is_valid(), true);
118 assert_eq!(schema.validate(&to_value(&11).unwrap()).is_valid(), true);
119}
120
121#[test]
122fn validate_exclusive_minimum() {
123 let mut scope = scope::Scope::new();
124 let schema = scope.compile_and_return(builder::schema(|s| {
125 s.minimum(10f64, true);
126 }).into_json(), true).ok().unwrap();
127
128 assert_eq!(schema.validate(&to_value(&9).unwrap()).is_valid(), false);
129 assert_eq!(schema.validate(&to_value(&10).unwrap()).is_valid(), false);
130 assert_eq!(schema.validate(&to_value(&11).unwrap()).is_valid(), true);
131}
132
133#[test]
134fn mailformed_minumum() {
135 let mut scope = scope::Scope::new();
136
137 assert!(scope.compile_and_return(jsonway::object(|schema| {
138 schema.set("minimum", true);
139 }).unwrap(), true).is_err());
140}
141
142#[test]
143fn mailformed_exclusive_minumum() {
144 let mut scope = scope::Scope::new();
145
146 assert!(scope.compile_and_return(jsonway::object(|schema| {
147 schema.set("exclusiveMinimum", true);
148 }).unwrap(), true).is_err());
149
150 assert!(scope.compile_and_return(jsonway::object(|schema| {
151 schema.set("minimum", 10);
152 schema.set("exclusiveMinimum", "".to_string());
153 }).unwrap(), true).is_err());
154}