valico/json_schema/validators/
maxmin.rs

1use serde_json::{Value};
2
3use super::super::errors;
4use super::super::scope;
5
6#[allow(missing_copy_implementations)]
7pub struct Maximum {
8    pub number: f64,
9    pub exclusive: bool
10}
11
12impl super::Validator for Maximum {
13    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
14        let number = nonstrict_process!(val.as_f64(), path);
15
16        let valid = if self.exclusive {
17            number < self.number
18        } else {
19            number <= self.number
20        };
21
22        if valid {
23            super::ValidationState::new()
24        } else {
25            val_error!(
26                errors::Maximum {
27                    path: path.to_string()
28                }
29            )
30        }
31    }
32}
33
34#[allow(missing_copy_implementations)]
35pub struct Minimum {
36    pub number: f64,
37    pub exclusive: bool
38}
39
40impl super::Validator for Minimum {
41    fn validate(&self, val: &Value, path: &str, _scope: &scope::Scope) -> super::ValidationState {
42        let number = nonstrict_process!(val.as_f64(), path);
43
44        let valid = if self.exclusive {
45            number > self.number
46        } else {
47            number >= self.number
48        };
49
50        if valid {
51            super::ValidationState::new()
52        } else {
53            val_error!(
54                errors::Minimum {
55                    path: path.to_string()
56                }
57            )
58        }
59    }
60}