1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
//! # Support for `T: TypedPath` from `axum-extra`
//!
//! ## Feature
//!
//! Enable the `extra_typed_path` feature to use `Valid<T: TypedPath>`.
//!
//! ## Usage
//!
//! 1. Implement `TypedPath`, `Deserialize`, `Validate` and `HasValidate` for your data type `T`.
//! 2. In your handler function, use `Valid<T>` as some parameter's type.
//!
//! ## Example
//!
//! ```no_run
//! use axum::Router;
//! use axum_extra::routing::{RouterExt, TypedPath};
//! use axum_valid::{HasValidate, Valid};
//! use serde::Deserialize;
//! use validator::Validate;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let router = Router::new().typed_get(handler);
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
//! .serve(router.into_make_service())
//! .await?;
//! Ok(())
//! }
//!
//! async fn handler(parameter: Valid<Parameter>) {
//! assert!(parameter.validate().is_ok());
//! }
//!
//! #[derive(TypedPath, Deserialize, Validate)]
//! #[typed_path("/extra_typed_path/:v0/:v1")]
//! struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! v1: String,
//! }
//!
//! impl HasValidate for Parameter {
//! type Validate = Self;
//!
//! fn get_validate(&self) -> &Self::Validate {
//! self
//! }
//! }
//! ```
use crate::{Valid, ValidEx};
use axum_extra::routing::TypedPath;
use std::fmt::Display;
impl<T: TypedPath + Display> TypedPath for Valid<T> {
const PATH: &'static str = T::PATH;
}
impl<T: TypedPath + Display, A> TypedPath for ValidEx<T, A> {
const PATH: &'static str = T::PATH;
}