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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! # 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
//! #[cfg(feature = "validator")]
//! mod validator_example {
//!     use axum::Router;
//!     use axum_extra::routing::{RouterExt, TypedPath};
//!     use axum_valid::{HasValidate, Valid};
//!     use serde::Deserialize;
//!     use validator::Validate;
//!
//!     pub fn router() -> Router {
//!         Router::new().typed_get(handler)
//!     }
//!
//!     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
//!         }
//!     }
//! }
//!
//! #[cfg(feature = "garde")]
//! mod garde_example {
//!     use axum::Router;
//!     use axum_extra::routing::{RouterExt, TypedPath};
//!     use axum_valid::{HasValidate, Garde};
//!     use serde::Deserialize;
//!     use garde::Validate;
//!
//!     pub fn router() -> Router {
//!         Router::new().typed_get(handler)
//!     }
//!
//!     async fn handler(parameter: Garde<Parameter>) {
//!         assert!(parameter.validate_with(&()).is_ok());
//!     }
//!
//!     #[derive(TypedPath, Deserialize, Validate)]
//!     #[typed_path("/extra_typed_path/:v0/:v1")]
//!     struct Parameter {
//!         #[garde(range(min = 5, max = 10))]
//!         v0: i32,
//!         #[garde(length(min = 1, max = 10))]
//!         v1: String,
//!     }
//!
//!     impl HasValidate for Parameter {
//!         type Validate = Self;
//!
//!         fn get_validate(&self) -> &Self::Validate {
//!             self
//!         }
//!     }
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! #     use std::net::SocketAddr;
//! #     use axum::Router;
//! #     use tokio::net::TcpListener;
//! #     let router = Router::new();
//! #     #[cfg(feature = "validator")]
//! #     let router = router.nest("/validator", validator_example::router());
//! #     #[cfg(feature = "garde")]
//! #     let router = router.nest("/garde", garde_example::router());
//! #     let listener = TcpListener::bind(&SocketAddr::from(([0u8, 0, 0, 0], 0u16))).await?;
//! #     axum::serve(listener, router.into_make_service())
//! #         .await?;
//! #     Ok(())
//! # }
//! ```

#[cfg(feature = "garde")]
use crate::Garde;
#[cfg(feature = "validify")]
use crate::{Modified, Validated, ValidifiedByRef};
#[cfg(feature = "validator")]
use crate::{Valid, ValidEx};
#[cfg(any(feature = "validator", feature = "garde", feature = "validify"))]
use axum_extra::routing::TypedPath;
#[cfg(any(feature = "validator", feature = "garde", feature = "validify"))]
use std::fmt::Display;

#[cfg(feature = "validator")]
impl<T: TypedPath + Display> TypedPath for Valid<T> {
    const PATH: &'static str = T::PATH;
}

#[cfg(feature = "validator")]
impl<T: TypedPath + Display> TypedPath for ValidEx<T> {
    const PATH: &'static str = T::PATH;
}

#[cfg(feature = "garde")]
impl<T: TypedPath + Display> TypedPath for Garde<T> {
    const PATH: &'static str = T::PATH;
}

#[cfg(feature = "validify")]
impl<T: TypedPath + Display> TypedPath for Validated<T> {
    const PATH: &'static str = T::PATH;
}

#[cfg(feature = "validify")]
impl<T: TypedPath + Display> TypedPath for Modified<T> {
    const PATH: &'static str = T::PATH;
}

#[cfg(feature = "validify")]
impl<T: TypedPath + Display> TypedPath for ValidifiedByRef<T> {
    const PATH: &'static str = T::PATH;
}