Trait serde_valid::validation::ValidateMultipleOf
source · [−]pub trait ValidateMultipleOf<T>where
T: PartialEq + Rem<Output = T> + Zero,{
fn validate_multiple_of(&self, multiple_of: T) -> Result<(), MultipleOfError>;
}
Expand description
Multipl validation of the number.
See https://json-schema.org/understanding-json-schema/reference/numeric.html#multiples
use serde_json::json;
use serde_valid::{Validate, ValidateMultipleOf};
struct MyType(i32);
impl ValidateMultipleOf<i32> for MyType {
fn validate_multiple_of(
&self,
multiple_of: i32,
) -> Result<(), serde_valid::MultipleOfError> {
self.0.validate_multiple_of(multiple_of)
}
}
#[derive(Validate)]
struct TestStruct {
#[validate(multiple_of = 5)]
val: MyType,
}
let s = TestStruct { val: MyType(6) };
assert_eq!(
s.validate().unwrap_err().to_string(),
json!({
"errors": [],
"properties": {
"val": {
"errors": ["The value must be multiple of `5`."]
}
}
})
.to_string()
);