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§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.