pub trait ValidateFrom<Raw>: Sized {
type Context: ?Sized;
type Error;
// Required method
fn validate_from_in_context(
raw: Raw,
ctx: &Self::Context,
) -> Result<Self, Self::Error>;
// Provided method
fn validate_from(raw: Raw) -> Result<Self, Self::Error>
where Self::Context: From<()> { ... }
}Expand description
Target-side dual of Validate.
Unlike Validate, which forces a single validated output type, ValidateFrom allows the same type to be validated from multiple source types, since many domain types may arrive in various formats. This should not be abused to make a single raw value validate into multiple types, but I can’t force you.
§Examples
use peer_pressure::ValidateFrom;
struct Email(String);
impl ValidateFrom<&str> for Email {
type Context = ();
type Error = &'static str;
fn validate_from_in_context(raw: &str, _: &()) -> Result<Self, Self::Error> {
if raw.contains('@') { Ok(Email(raw.to_string())) } else { Err("missing @") }
}
}
assert!(Email::validate_from("alice@example.com").is_ok());
assert!(Email::validate_from("not-an-email").is_err());Required Associated Types§
Required Methods§
Sourcefn validate_from_in_context(
raw: Raw,
ctx: &Self::Context,
) -> Result<Self, Self::Error>
fn validate_from_in_context( raw: Raw, ctx: &Self::Context, ) -> Result<Self, Self::Error>
Validates raw in given context, returning either a valid Self or Error.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".