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}
10
11impl super::Validator for Maximum {
12    fn validate(
13        &self,
14        val: &Value,
15        path: &str,
16        _scope: &scope::Scope,
17        _: &super::ValidationState,
18    ) -> super::ValidationState {
19        let number = nonstrict_process!(val.as_f64(), path);
20
21        if number <= self.number {
22            super::ValidationState::new()
23        } else {
24            val_error!(errors::Maximum {
25                path: path.to_string()
26            })
27        }
28    }
29}
30
31#[allow(missing_copy_implementations)]
32pub struct ExclusiveMaximum {
33    pub number: f64,
34}
35
36impl super::Validator for ExclusiveMaximum {
37    fn validate(
38        &self,
39        val: &Value,
40        path: &str,
41        _scope: &scope::Scope,
42        _: &super::ValidationState,
43    ) -> super::ValidationState {
44        let number = nonstrict_process!(val.as_f64(), path);
45
46        if number < self.number {
47            super::ValidationState::new()
48        } else {
49            val_error!(errors::Maximum {
50                path: path.to_string()
51            })
52        }
53    }
54}
55
56#[allow(missing_copy_implementations)]
57pub struct Minimum {
58    pub number: f64,
59}
60
61impl super::Validator for Minimum {
62    fn validate(
63        &self,
64        val: &Value,
65        path: &str,
66        _scope: &scope::Scope,
67        _: &super::ValidationState,
68    ) -> super::ValidationState {
69        let number = nonstrict_process!(val.as_f64(), path);
70
71        if number >= self.number {
72            super::ValidationState::new()
73        } else {
74            val_error!(errors::Minimum {
75                path: path.to_string()
76            })
77        }
78    }
79}
80
81#[allow(missing_copy_implementations)]
82pub struct ExclusiveMinimum {
83    pub number: f64,
84}
85
86impl super::Validator for ExclusiveMinimum {
87    fn validate(
88        &self,
89        val: &Value,
90        path: &str,
91        _scope: &scope::Scope,
92        _: &super::ValidationState,
93    ) -> super::ValidationState {
94        let number = nonstrict_process!(val.as_f64(), path);
95
96        if number > self.number {
97            super::ValidationState::new()
98        } else {
99            val_error!(errors::Minimum {
100                path: path.to_string()
101            })
102        }
103    }
104}