Skip to main content

Validator

Struct Validator 

Source
pub struct Validator { /* private fields */ }
Expand description

Fluent builder that collects field errors and converts to AppError via Validator::validate.

Implementations§

Source§

impl Validator

Source

pub fn new() -> Self

Create a new empty validator.

Source

pub fn required(self, field: &str, value: &str) -> Self

Fail if value is empty or whitespace-only.

Source

pub fn min_length(self, field: &str, value: &str, min: usize) -> Self

Fail if value has fewer than min characters.

Source

pub fn max_length(self, field: &str, value: &str, max: usize) -> Self

Fail if value exceeds max characters.

Source

pub fn email(self, field: &str, value: &str) -> Self

Fail if value is not a valid e-mail address.

Source

pub fn url(self, field: &str, value: &str) -> Self

Fail if value is not a valid HTTP/HTTPS URL.

Source

pub fn pattern(self, field: &str, value: &str, re: &str) -> Self

Fail if value does not match the regular expression re.

Source

pub fn required_uuid(self, field: &str, value: &str) -> Self

Fail if value is not a valid UUID string.

Source

pub fn optional_uuid(self, field: &str, value: Option<&str>) -> Self

Fail if value is Some but not a valid UUID string.

Source

pub fn in_range<T: PartialOrd + Display>( self, field: &str, value: T, min: T, max: T, ) -> Self

Fail if value is outside the inclusive range [min, max].

Source

pub fn min_value<T: PartialOrd + Display>( self, field: &str, value: T, min: T, ) -> Self

Fail if value is below min.

Source

pub fn max_value<T: PartialOrd + Display>( self, field: &str, value: T, max: T, ) -> Self

Fail if value is above max.

Source

pub fn before(self, field: &str, value: &str, deadline: &str) -> Self

Fail if value (ISO-8601 datetime string) is not before deadline.

Source

pub fn after(self, field: &str, value: &str, floor: &str) -> Self

Fail if value (ISO-8601 datetime string) is not after floor.

Source

pub fn one_of<T: PartialEq + Display>( self, field: &str, value: &T, allowed: &[T], ) -> Self

Fail if value is not in allowed.

Source

pub fn custom(self, condition: bool, field: &str, message: &str) -> Self

Add an error for field if condition is false.

Source

pub fn has_errors(&self) -> bool

Returns true if any validation errors have been accumulated.

Source

pub fn errors(&self) -> &[FieldError]

Returns a slice of all accumulated field errors.

Source

pub fn validate(self) -> AppResult<()>

Consume the validator and return Ok(()) if no errors, or an AppError::invalid_input containing all field errors.

Trait Implementations§

Source§

impl Debug for Validator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Validator

Source§

fn default() -> Validator

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.