Skip to main content

valico/json_schema/keywords/
maxmin.rs

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