pub trait Validator<T: ?Sized> {
// Required method
fn validate(value: &T) -> Result<(), Violation>;
}Expand description
Validates values of type T.
Validators are types (usually without fields) so that they can be named
in the types of fields, for instance Validated<String, Email>. They
are parameterized with const generics (Len<1, 64>) and combined with
tuples: (NonEmpty, MaxLen<64>) requires all of them. Validators that
need data that cannot be a const generic (like a pattern) are types of
their own. Implementing them for all types that are strings covers
String, &str, Cow<str> and Box<str>:
use deser_validate::{Validator, Violation};
/// A lowercase identifier like `my-service`.
pub struct Slug;
impl<T: AsRef<str> + ?Sized> Validator<T> for Slug {
fn validate(value: &T) -> Result<(), Violation> {
let value = value.as_ref();
if !value.is_empty()
&& value.bytes().all(|b| {
b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'
})
{
Ok(())
} else {
Err(Violation::new("slug", "must be a lowercase identifier"))
}
}
}
assert!(Slug::validate("my-service").is_ok());
assert!(Slug::validate(&"My Service".to_string()).is_err());Most validators are easier to write with the
validator! macro, which turns a condition or
a function into a validator type.
Implementing the trait is needed for types with generics or lifetimes,
which the macro does not support:
use deser_validate::{Validator, Violation};
pub struct Name<'a>(&'a str);
pub struct NonEmptyName;
impl<'a> Validator<Name<'a>> for NonEmptyName {
fn validate(value: &Name<'a>) -> Result<(), Violation> {
if value.0.is_empty() {
return Err(Violation::new("not_empty", "must not be empty"));
}
Ok(())
}
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>, D: Validator<T>, E: Validator<T>, F: Validator<T>> Validator<T> for (A, B, C, D, E, F)
Requires all validators, the first violation is reported.
impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>, D: Validator<T>, E: Validator<T>, F: Validator<T>> Validator<T> for (A, B, C, D, E, F)
Requires all validators, the first violation is reported.
Source§impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>, D: Validator<T>, E: Validator<T>> Validator<T> for (A, B, C, D, E)
Requires all validators, the first violation is reported.
impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>, D: Validator<T>, E: Validator<T>> Validator<T> for (A, B, C, D, E)
Requires all validators, the first violation is reported.
Source§impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>, D: Validator<T>> Validator<T> for (A, B, C, D)
Requires all validators, the first violation is reported.
impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>, D: Validator<T>> Validator<T> for (A, B, C, D)
Requires all validators, the first violation is reported.
Source§impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>> Validator<T> for (A, B, C)
Requires all validators, the first violation is reported.
impl<T: ?Sized, A: Validator<T>, B: Validator<T>, C: Validator<T>> Validator<T> for (A, B, C)
Requires all validators, the first violation is reported.
Source§impl<T: ?Sized, A: Validator<T>, B: Validator<T>> Validator<T> for (A, B)
Requires all validators, the first violation is reported.
impl<T: ?Sized, A: Validator<T>, B: Validator<T>> Validator<T> for (A, B)
Requires all validators, the first violation is reported.
Source§impl<T: ?Sized, A: Validator<T>> Validator<T> for (A,)
Requires all validators, the first violation is reported.
impl<T: ?Sized, A: Validator<T>> Validator<T> for (A,)
Requires all validators, the first violation is reported.