poem_openapi/validation/
minimum.rs

1use derive_more::Display;
2use num_traits::AsPrimitive;
3
4use crate::{
5    registry::MetaSchema,
6    validation::{Validator, ValidatorMeta},
7};
8
9#[derive(Display)]
10#[display("minimum({n}, exclusive: {exclusive})")]
11pub struct Minimum {
12    n: f64,
13    exclusive: bool,
14}
15
16impl Minimum {
17    #[inline]
18    pub fn new(n: f64, exclusive: bool) -> Self {
19        Self { n, exclusive }
20    }
21}
22
23impl<T: AsPrimitive<f64>> Validator<T> for Minimum {
24    #[inline]
25    fn check(&self, value: &T) -> bool {
26        if self.exclusive {
27            value.as_() > self.n
28        } else {
29            value.as_() >= self.n
30        }
31    }
32}
33
34impl ValidatorMeta for Minimum {
35    fn update_meta(&self, meta: &mut MetaSchema) {
36        meta.minimum = Some(self.n);
37        if self.exclusive {
38            meta.exclusive_minimum = Some(true);
39        }
40    }
41}