[][src]Struct tide_validator::ValidatorMiddleware

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

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

Methods

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

pub fn new() -> Self[src]

Create a new ValidatorMiddleware to put in your tide configuration.

Example

This example deliberately fails to compile
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(())
    })
}

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

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

Add new validator for your middleware

Example

This example deliberately fails to compile
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

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

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

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<State, F> Middleware<State> for F where
    F: Send + Sync + 'static + for<'a> Fn(Request<State>, Next<'a, State>) -> Pin<Box<dyn Future<Output = Result<Response, Error>> + 'a + Send>>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.