Module axum_valid::extra::query
source · Expand description
Support for Query<T> from axum-extra
Feature
Enable the extra_query feature to use Valid<Query<T>>.
Usage
- Implement
DeserializeandValidatefor your data typeT. - In your handler function, use
Valid<Query<T>>as some parameter’s type.
Example
use axum::routing::post;
use axum::Router;
use axum_extra::extract::Query;
use axum_valid::Valid;
use serde::Deserialize;
use validator::Validate;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let router = Router::new().route("/extra_query", post(handler));
axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
.serve(router.into_make_service())
.await?;
Ok(())
}
async fn handler(Valid(Query(parameter)): Valid<Query<Parameter>>) {
assert!(parameter.validate().is_ok());
}
#[derive(Validate, Deserialize)]
pub struct Parameter {
#[validate(range(min = 5, max = 10))]
pub v0: i32,
#[validate(length(min = 1, max = 10))]
pub v1: String,
}