impl HasValidateArgs for all extractors

This commit is contained in:
gengteng
2023-09-27 10:31:45 +08:00
parent 323d16c142
commit 5b15f15ce3
14 changed files with 147 additions and 32 deletions

View File

@@ -236,9 +236,9 @@ pub mod query;
#[cfg(feature = "extra_typed_path")]
pub mod typed_path;
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum_extra::extract::{Cached, WithRejection};
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Cached<T> {
type Validate = T;
@@ -248,6 +248,13 @@ impl<T: Validate> HasValidate for Cached<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Cached<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
impl<T: Validate, R> HasValidate for WithRejection<T, R> {
type Validate = T;
fn get_validate(&self) -> &T {
@@ -255,6 +262,13 @@ impl<T: Validate, R> HasValidate for WithRejection<T, R> {
}
}
impl<'v, T: ValidateArgs<'v>, R> HasValidateArgs<'v> for WithRejection<T, R> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{Rejection, ValidTest};

View File

@@ -38,9 +38,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum_extra::extract::Form;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Form<T> {
type Validate = T;
@@ -49,6 +49,13 @@ impl<T: Validate> HasValidate for Form<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Form<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -40,9 +40,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum_extra::protobuf::Protobuf;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Protobuf<T> {
type Validate = T;
@@ -51,6 +51,13 @@ impl<T: Validate> HasValidate for Protobuf<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Protobuf<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -41,9 +41,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum_extra::extract::Query;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Query<T> {
type Validate = T;
@@ -52,6 +52,13 @@ impl<T: Validate> HasValidate for Query<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Query<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -49,16 +49,14 @@
//! }
//! ```
use crate::Valid;
use crate::{Valid, ValidArgs};
use axum_extra::routing::TypedPath;
use std::fmt::{Display, Formatter};
impl<T: Display> Display for Valid<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
use std::fmt::Display;
impl<T: TypedPath + Display> TypedPath for Valid<T> {
const PATH: &'static str = T::PATH;
}
impl<T: TypedPath + Display> TypedPath for ValidArgs<T> {
const PATH: &'static str = T::PATH;
}

View File

@@ -38,9 +38,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum::Form;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Form<T> {
type Validate = T;
@@ -49,6 +49,13 @@ impl<T: Validate> HasValidate for Form<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Form<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -38,9 +38,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum::Json;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Json<T> {
type Validate = T;
@@ -49,6 +49,13 @@ impl<T: Validate> HasValidate for Json<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Json<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -66,6 +66,12 @@ impl<E> DerefMut for Valid<E> {
}
}
impl<T: Display> Display for ValidArgs<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<E> Valid<E> {
/// Consume the `Valid` extractor and returns the inner type.
pub fn into_inner(self) -> E {
@@ -101,6 +107,12 @@ impl<E> DerefMut for ValidArgs<E> {
}
}
impl<T: Display> Display for Valid<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<E> ValidArgs<E> {
/// Consume the `ValidArgs` extractor and returns the inner type.
pub fn into_inner(self) -> E {

View File

@@ -47,9 +47,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum_msgpack::{MsgPack, MsgPackRaw};
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for MsgPack<T> {
type Validate = T;
@@ -58,6 +58,13 @@ impl<T: Validate> HasValidate for MsgPack<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPack<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
impl<T: Validate> HasValidate for MsgPackRaw<T> {
type Validate = T;
fn get_validate(&self) -> &T {
@@ -65,6 +72,13 @@ impl<T: Validate> HasValidate for MsgPackRaw<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPackRaw<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -37,9 +37,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum::extract::Path;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Path<T> {
type Validate = T;
@@ -47,3 +47,10 @@ impl<T: Validate> HasValidate for Path<T> {
&self.0
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Path<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}

View File

@@ -41,9 +41,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum::extract::Query;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Query<T> {
type Validate = T;
@@ -52,6 +52,13 @@ impl<T: Validate> HasValidate for Query<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Query<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -61,9 +61,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum::TypedHeader;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for TypedHeader<T> {
type Validate = T;
@@ -72,6 +72,13 @@ impl<T: Validate> HasValidate for TypedHeader<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for TypedHeader<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -50,9 +50,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum_typed_multipart::{BaseMultipart, TypedMultipart};
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate, R> HasValidate for BaseMultipart<T, R> {
type Validate = T;
@@ -61,6 +61,13 @@ impl<T: Validate, R> HasValidate for BaseMultipart<T, R> {
}
}
impl<'v, T: ValidateArgs<'v>, R> HasValidateArgs<'v> for BaseMultipart<T, R> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.data
}
}
impl<T: Validate> HasValidate for TypedMultipart<T> {
type Validate = T;
fn get_validate(&self) -> &T {
@@ -68,6 +75,13 @@ impl<T: Validate> HasValidate for TypedMultipart<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for TypedMultipart<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};

View File

@@ -42,9 +42,9 @@
//! }
//! ```
use crate::HasValidate;
use crate::{HasValidate, HasValidateArgs};
use axum_yaml::Yaml;
use validator::Validate;
use validator::{Validate, ValidateArgs};
impl<T: Validate> HasValidate for Yaml<T> {
type Validate = T;
@@ -53,6 +53,13 @@ impl<T: Validate> HasValidate for Yaml<T> {
}
}
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Yaml<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};