rename ValidArgs to ValidEx

This commit is contained in:
gengteng
2023-09-27 12:10:06 +08:00
parent 5b15f15ce3
commit a6da64d6c9
3 changed files with 19 additions and 15 deletions

View File

@@ -8,6 +8,8 @@
This crate provides a `Valid` type that can be used in combination with `Json`, `Path`, `Query`, and `Form` extractors to validate the entities that implement the `Validate` trait from the `validator` crate. This crate provides a `Valid` type that can be used in combination with `Json`, `Path`, `Query`, and `Form` extractors to validate the entities that implement the `Validate` trait from the `validator` crate.
It also provides a `ValidEx` type that works similarly to `Valid`, but can perform validation requiring additional arguments by using types that implement the `ValidateArgs` trait.
Additional extractors like `TypedHeader`, `MsgPack`, `Yaml` etc. are supported through optional features. The full list of supported extractors is in the Features section below. Additional extractors like `TypedHeader`, `MsgPack`, `Yaml` etc. are supported through optional features. The full list of supported extractors is in the Features section below.
## Basic usage ## Basic usage

View File

@@ -49,7 +49,7 @@
//! } //! }
//! ``` //! ```
use crate::{Valid, ValidArgs}; use crate::{Valid, ValidEx};
use axum_extra::routing::TypedPath; use axum_extra::routing::TypedPath;
use std::fmt::Display; use std::fmt::Display;
@@ -57,6 +57,6 @@ impl<T: TypedPath + Display> TypedPath for Valid<T> {
const PATH: &'static str = T::PATH; const PATH: &'static str = T::PATH;
} }
impl<T: TypedPath + Display> TypedPath for ValidArgs<T> { impl<T: TypedPath + Display> TypedPath for ValidEx<T> {
const PATH: &'static str = T::PATH; const PATH: &'static str = T::PATH;
} }

View File

@@ -66,7 +66,7 @@ impl<E> DerefMut for Valid<E> {
} }
} }
impl<T: Display> Display for ValidArgs<T> { impl<T: Display> Display for ValidEx<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f) self.0.fmt(f)
} }
@@ -79,9 +79,11 @@ impl<E> Valid<E> {
} }
} }
/// `ValidArgs` can be used with extractors from the various modules. /// # `ValidEx` data extractor
///
/// `ValidEx` can be used with extractors from the various modules.
/// Refer to the examples for `Valid` in each module - the usage of /// Refer to the examples for `Valid` in each module - the usage of
/// `ValidArgs` is similar, except the inner data type implements /// `ValidEx` is similar, except the inner data type implements
/// `ValidateArgs` instead of `Validate`. /// `ValidateArgs` instead of `Validate`.
/// ///
/// `ValidateArgs` is usually automatically implemented by validator's /// `ValidateArgs` is usually automatically implemented by validator's
@@ -89,11 +91,11 @@ impl<E> Valid<E> {
/// ///
/// Note that the documentation for each module currently only shows /// Note that the documentation for each module currently only shows
/// examples of `Valid`, and does not demonstrate concrete usage of /// examples of `Valid`, and does not demonstrate concrete usage of
/// `ValidArgs`, but the usage is analogous. /// `ValidEx`, but the usage is analogous.
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub struct ValidArgs<E>(pub E); pub struct ValidEx<E>(pub E);
impl<E> Deref for ValidArgs<E> { impl<E> Deref for ValidEx<E> {
type Target = E; type Target = E;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@@ -101,7 +103,7 @@ impl<E> Deref for ValidArgs<E> {
} }
} }
impl<E> DerefMut for ValidArgs<E> { impl<E> DerefMut for ValidEx<E> {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0 &mut self.0
} }
@@ -113,8 +115,8 @@ impl<T: Display> Display for Valid<T> {
} }
} }
impl<E> ValidArgs<E> { impl<E> ValidEx<E> {
/// Consume the `ValidArgs` extractor and returns the inner type. /// Consume the `ValidEx` extractor and returns the inner type.
pub fn into_inner(self) -> E { pub fn into_inner(self) -> E {
self.0 self.0
} }
@@ -375,7 +377,7 @@ where
} }
#[async_trait] #[async_trait]
impl<'v, S, B, E> FromRequest<S, B> for ValidArgs<E> impl<'v, S, B, E> FromRequest<S, B> for ValidEx<E>
where where
S: Send + Sync, S: Send + Sync,
B: Send + Sync + 'static, B: Send + Sync + 'static,
@@ -402,12 +404,12 @@ where
error: ValidError::Valid(e), error: ValidError::Valid(e),
response_builder: context.response_builder, response_builder: context.response_builder,
})?; })?;
Ok(ValidArgs(inner)) Ok(ValidEx(inner))
} }
} }
#[async_trait] #[async_trait]
impl<'v, S, E> FromRequestParts<S> for ValidArgs<E> impl<'v, S, E> FromRequestParts<S> for ValidEx<E>
where where
S: Send + Sync, S: Send + Sync,
E: HasValidateArgs<'v> + FromRequestParts<S>, E: HasValidateArgs<'v> + FromRequestParts<S>,
@@ -433,7 +435,7 @@ where
error: ValidError::Valid(e), error: ValidError::Valid(e),
response_builder: context.response_builder, response_builder: context.response_builder,
})?; })?;
Ok(ValidArgs(inner)) Ok(ValidEx(inner))
} }
} }