Validator
Showing how to use the validator crate to validate the values passed by the users.
Run
cargo run -p example-validator
Test
cargo test -p example-validator
Use
Field not supplied
$ curl -i http://localhost:3000/
HTTP/1.1 400 Bad Request
content-type: text/plain; charset=utf-8
content-length: 48
date: Wed, 19 Mar 2025 15:00:06 GMT
Failed to deserialize form: missing field `name`
Input too short
$ curl -i http://localhost:3000/?name=
HTTP/1.1 400 Bad Request
content-type: text/plain; charset=utf-8
content-length: 48
date: Wed, 19 Mar 2025 15:03:22 GMT
Input validation error: [name: Can not be empty]
Acceptable input
$ curl -i http://localhost:3000/?name=Jo
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 19
date: Wed, 19 Mar 2025 15:03:52 GMT
<h1>Hello, Jo!</h1>$
[package]
edition = "2021"
name = "example-validator"
publish = false
version = "0.1.0"
[dependencies]
axum = { path = "../../axum" }
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0.29"
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
validator = { version = "0.18.1", features = ["derive"] }
[dev-dependencies]
http-body-util = "0.1.0"
tower = { version = "0.5.2", features = ["util"] }
//! Run with //! //! ```not_rust //! 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' //! -> <h1>Hello, LT!</h1> //! ``` use axum::{ extract::{rejection::FormRejection, Form, FromRequest, Request}, http::StatusCode, response::{Html, IntoResponse, Response}, routing::get, Router, }; use serde::{de::DeserializeOwned, Deserialize}; use thiserror::Error; use tokio::net::TcpListener; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use validator::Validate; #[tokio::main] async fn main() { tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()), ) .with(tracing_subscriber::fmt::layer()) .init(); // build our application with a route let app = app(); // run it let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap(); tracing::debug!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } fn app() -> Router { Router::new().route("/", get(handler)) } #[derive(Debug, Deserialize, Validate)] pub struct NameInput { #[validate(length(min = 2, message = "Can not be empty"))] pub name: String, } async fn handler(ValidatedForm(input): ValidatedForm<NameInput>) -> Html<String> { Html(format!("<h1>Hello, {}!</h1>", input.name)) } #[derive(Debug, Clone, Copy, Default)] pub struct ValidatedForm<T>(pub T); impl<T, S> FromRequest<S> for ValidatedForm<T> where T: DeserializeOwned + Validate, S: Send + Sync, Form<T>: FromRequest<S, Rejection = FormRejection>, { type Rejection = ServerError; async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { let Form(value) = Form::<T>::from_request(req, state).await?; value.validate()?; Ok(ValidatedForm(value)) } } #[derive(Debug, Error)] pub enum ServerError { #[error(transparent)] ValidationError(#[from] validator::ValidationErrors), #[error(transparent)] AxumFormRejection(#[from] 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() } } #[cfg(test)] mod tests { use super::*; use axum::{ body::Body, http::{Request, StatusCode}, }; use http_body_util::BodyExt; use tower::ServiceExt; async fn get_html(response: Response<Body>) -> String { let body = response.into_body(); let bytes = body.collect().await.unwrap().to_bytes(); String::from_utf8(bytes.to_vec()).unwrap() } #[tokio::test] async fn test_no_param() { let response = app() .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap()) .await .unwrap(); assert_eq!(response.status(), StatusCode::BAD_REQUEST); let html = get_html(response).await; assert_eq!(html, "Failed to deserialize form: missing field `name`"); } #[tokio::test] async fn test_with_param_without_value() { let response = app() .oneshot( Request::builder() .uri("/?name=") .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(response.status(), StatusCode::BAD_REQUEST); let html = get_html(response).await; assert_eq!(html, "Input validation error: [name: Can not be empty]"); } #[tokio::test] async fn test_with_param_with_short_value() { let response = app() .oneshot( Request::builder() .uri("/?name=X") .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(response.status(), StatusCode::BAD_REQUEST); let html = get_html(response).await; assert_eq!(html, "Input validation error: [name: Can not be empty]"); } #[tokio::test] async fn test_with_param_and_value() { let response = app() .oneshot( Request::builder() .uri("/?name=LT") .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); let html = get_html(response).await; assert_eq!(html, "<h1>Hello, LT!</h1>"); } }