mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-26 20:40:29 +00:00
94 lines
2.6 KiB
Rust
94 lines
2.6 KiB
Rust
//! 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 = Router::new().route("/", get(handler));
|
|
|
|
// 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();
|
|
}
|
|
|
|
#[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<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()
|
|
}
|
|
}
|