//! Run with //! //! ```not_rust //! cd examples && cargo run -p example-validator //! //! curl '127.0.0.1:3000?name=' //! -> Input validation error: [name: Can not be empty] //! //! curl '127.0.0.1:3000?name=LT' //! ->

Hello, LT!

//! ``` use async_trait::async_trait; use axum::{ extract::{Form, FromRequest, RequestParts}, http::StatusCode, response::{Html, IntoResponse, Response}, routing::get, BoxError, Router, }; use serde::{de::DeserializeOwned, Deserialize}; use std::net::SocketAddr; use thiserror::Error; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use validator::Validate; #[tokio::main] async fn main() { tracing_subscriber::registry() .with(tracing_subscriber::EnvFilter::new( std::env::var("RUST_LOG").unwrap_or_else(|_| "example_validator=debug".into()), )) .with(tracing_subscriber::fmt::layer()) .init(); // build our application with a route let app = Router::new().route("/", get(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); tracing::debug!("listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } #[derive(Debug, Deserialize, Validate)] pub struct NameInput { #[validate(length(min = 1, message = "Can not be empty"))] pub name: String, } async fn handler(ValidatedForm(input): ValidatedForm) -> Html { Html(format!("

Hello, {}!

", input.name)) } #[derive(Debug, Clone, Copy, Default)] pub struct ValidatedForm(pub T); #[async_trait] impl FromRequest for ValidatedForm where T: DeserializeOwned + Validate, B: http_body::Body + Send, B::Data: Send, B::Error: Into, { type Rejection = ServerError; async fn from_request(req: &mut RequestParts) -> Result { let Form(value) = Form::::from_request(req).await?; value.validate()?; Ok(ValidatedForm(value)) } } #[derive(Debug, Error)] pub enum ServerError { #[error(transparent)] ValidationError(#[from] validator::ValidationErrors), #[error(transparent)] AxumFormRejection(#[from] axum::extract::rejection::FormRejection), } impl IntoResponse for ServerError { fn into_response(self) -> Response { match self { ServerError::ValidationError(_) => { let message = format!("Input validation error: [{}]", self).replace('\n', ", "); (StatusCode::BAD_REQUEST, message) } ServerError::AxumFormRejection(_) => (StatusCode::BAD_REQUEST, self.to_string()), } .into_response() } }