Struct ValidatorMiddleware

Source
pub struct ValidatorMiddleware<T>
where T: Serialize + Send + Sync + 'static,
{ /* private fields */ }
Expand description

Used as a middleware in your tide framework and add your custom validators

Implementations§

Source§

impl<T> ValidatorMiddleware<T>
where T: Serialize + Send + Sync + 'static,

Source

pub fn new() -> Self

Create a new ValidatorMiddleware to put in your tide configuration.

§Example
fn main() -> io::Result<()> {
    task::block_on(async {
        let mut app = tide::new();

        let mut validator_middleware = ValidatorMiddleware::new();
        validator_middleware.add_validator(HttpField::Header("X-Custom-Header"), is_number);

        app.at("/test/:n").middleware(validator_middleware).get(
            |_: tide::Request<()>| async move { Ok(tide::Response::new(StatusCode::Ok).body_json("test").unwrap()) },
        );

        app.listen("127.0.0.1:8080").await?;
        Ok(())
    })
}
Source

pub fn with_validators<F>( self, validators: HashMap<HttpField<'static>, F>, ) -> Self
where F: Fn(&str, Option<&str>) -> Result<(), T> + Send + Sync + 'static,

Source

pub fn add_validator<F>(&mut self, param_name: HttpField<'static>, validator: F)
where F: Fn(&str, Option<&str>) -> Result<(), T> + Send + Sync + 'static,

Add new validator for your middleware

§Example
fn main() -> io::Result<()> {
    task::block_on(async {
        let mut app = tide::new();

        let mut validator_middleware = ValidatorMiddleware::new();
        validator_middleware.add_validator(HttpField::Header("X-Custom-Header"), is_number);
        validator_middleware.add_validator(HttpField::QueryParam("myqueryparam"), is_required);

        app.at("/test/:n").middleware(validator_middleware).get(
            |_: tide::Request<()>| async move { Ok(tide::Response::new(StatusCode::Ok).body_json("test").unwrap()) },
        );

        app.listen("127.0.0.1:8080").await?;
        Ok(())
    })
}

Trait Implementations§

Source§

impl<T> Debug for ValidatorMiddleware<T>
where T: Serialize + Send + Sync + 'static,

Source§

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

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

impl<State, T> Middleware<State> for ValidatorMiddleware<T>
where State: Send + Sync + 'static, T: Serialize + Send + Sync + 'static,

Source§

fn handle<'a>( &'a self, ctx: Request<State>, next: Next<'a, State>, ) -> BoxFuture<'a, Result>

Asynchronously handle the request, and return a response.

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.