pub trait Validator<T: ?Sized, C: ?Sized = ()> {
type Error: Error + 'static;
// Required method
fn validate(&self, value: &T, context: &C) -> Result<(), Self::Error>;
}Expand description
Validates a borrowed value using an immutable, typed context.
§Examples
use std::fmt;
use qubit_validator::Validator;
#[derive(Debug, Eq, PartialEq)]
struct EmptyText;
impl fmt::Display for EmptyText {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("text must not be empty")
}
}
impl std::error::Error for EmptyText {}
struct NonEmpty;
impl Validator<str> for NonEmpty {
type Error = EmptyText;
fn validate(&self, value: &str, _: &()) -> Result<(), Self::Error> {
if value.is_empty() { Err(EmptyText) } else { Ok(()) }
}
}
assert!(NonEmpty.validate("ready", &()).is_ok());
assert_eq!(NonEmpty.validate("", &()).unwrap_err(), EmptyText);§Type Parameters
T: Borrowed value type accepted by the validator.C: Immutable context supplied for each validation call.
Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".