valico/json_schema/validators/
multiple_of.rs

1use serde_json::{Value};
2
3use super::super::errors;
4use super::super::scope;
5use std::f64;
6
7#[allow(missing_copy_implementations)]
8pub struct MultipleOf {
9    pub number: f64
10}
11
12impl super::Validator for MultipleOf {
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 (number.fract() == 0f64) && (self.number.fract() == 0f64) {
17            (number % self.number) == 0f64
18        } else {
19            let remainder: f64 = (number/self.number) % 1f64;
20            !(remainder >= f64::EPSILON) && (remainder < (1f64 - f64::EPSILON))
21        };
22
23        if valid {
24            super::ValidationState::new()
25        } else {
26            val_error!(
27                errors::MultipleOf {
28                    path: path.to_string()
29                }
30            )
31        }
32    }
33}