Trait serde_validate::Validate
source · pub trait Validate: Sized {
type Error;
// Required method
fn validate(&self) -> Result<(), Self::Error>;
// Provided method
fn validated(self) -> Result<Self, Self::Error> { ... }
}Expand description
The Validate trait defines the contract for validating deserialized structs.
Implementors of this trait are required to provide their own validation logic and an associated error type.
§Example
use serde_validate::Validate;
struct MyStruct {
value: i32,
}
impl Validate for MyStruct {
type Error = String;
fn validate(&self) -> Result<(), Self::Error> {
if self.value < 0 {
Err("Value must be non-negative".into())
} else {
Ok(())
}
}
}
let my_struct = MyStruct { value: 10 };
assert!(my_struct.validate().is_ok());Required Associated Types§
Required Methods§
Provided Methods§
Object Safety§
This trait is not object safe.