feat: Update validator to v0.17.0 and refactor argument-based validation using validator

This commit is contained in:
gengteng
2024-03-05 12:52:36 +08:00
parent 2b6641c558
commit 0ce56f3368
6 changed files with 223 additions and 245 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "axum-valid"
version = "0.16.0"
version = "0.17.0"
description = "Provides validation extractors for your Axum application, allowing you to validate data using validator, garde, validify or all of them."
authors = ["GengTeng <me@gteng.org>"]
license = "MIT"
@@ -27,7 +27,7 @@ features = ["full", "aide"]
[dependencies]
axum = { version = "0.7.3", default-features = false }
garde = { version = "0.18.0", optional = true }
validator = { version = "0.16.1", optional = true }
validator = { version = "0.17.0", optional = true }
validify = { version = "1.3.0", optional = true }
[dependencies.axum-extra]
@@ -57,7 +57,7 @@ axum = { version = "0.7.1", features = ["macros"] }
tokio = { version = "1.34.0", features = ["full"] }
reqwest = { version = "0.11.23", features = ["json", "multipart"] }
serde = { version = "1.0.195", features = ["derive"] }
validator = { version = "0.16.1", features = ["derive"] }
validator = { version = "0.17.0", features = ["derive"] }
garde = { version = "0.18.0", features = ["serde", "derive"] }
serde_json = "1.0.108"
serde_yaml = "0.9.27"

View File

@@ -9,7 +9,9 @@
## 📑 Overview
**axum-valid** is a library that provides data validation extractors for the Axum web framework. It integrates **validator**, **garde** and **validify**, three popular validation crates in the Rust ecosystem, to offer convenient validation and data handling extractors for Axum applications.
**axum-valid** is a library that provides data validation extractors for the Axum web framework. It integrates *
*validator**, **garde** and **validify**, three popular validation crates in the Rust ecosystem, to offer convenient
validation and data handling extractors for Axum applications.
## 🚀 Basic usage
@@ -66,7 +68,8 @@ async fn main() -> anyhow::Result<()> {
}
```
In case of inner extractor errors, it will first return the Rejection from the inner extractor. When validation errors occur, the outer extractor will automatically return 400 with validation errors as the HTTP message body.
In case of inner extractor errors, it will first return the Rejection from the inner extractor. When validation errors
occur, the outer extractor will automatically return 400 with validation errors as the HTTP message body.
### 📦 `Garde<E>`
@@ -239,11 +242,12 @@ async fn main() -> anyhow::Result<()> {
}
```
To see how each inner extractor can be used with validation extractors, please refer to the example in the [documentation](https://docs.rs/axum-valid) of the corresponding module.
To see how each inner extractor can be used with validation extractors, please refer to the example in
the [documentation](https://docs.rs/axum-valid) of the corresponding module.
## 🚀 Argument-Based Validation
### 📦 `ValidEx<E, A>`
### 📦 `ValidEx<E>`
* Install
@@ -258,35 +262,34 @@ cargo add axum-valid
```rust,ignore
use axum::routing::post;
use axum::{Form, Router};
use axum_valid::{Arguments, ValidEx};
use axum_valid::ValidEx;
use serde::Deserialize;
use std::net::SocketAddr;
use std::ops::{RangeFrom, RangeInclusive};
use tokio::net::TcpListener;
use validator::{Validate, ValidateArgs, ValidationError};
use validator::{Validate, ValidationError};
// NOTE: When some fields use custom validation functions with arguments,
// `#[derive(Validate)]` will implement `ValidateArgs` instead of `Validate` for the type.
// The validation arguments will be a tuple of all the field validation args.
// In this example it is (&RangeInclusive<usize>, &RangeFrom<usize>).
// For more detailed information and understanding of `ValidateArgs` and their argument types,
// please refer to the `validator` crate documentation.
#[derive(Debug, Validate, Deserialize)]
#[validate(context = PagerValidArgs)] // context is required
pub struct Pager {
#[validate(custom(function = "validate_page_size", arg = "&'v_a RangeInclusive<usize>"))]
#[validate(custom(function = "validate_page_size", use_context))]
pub page_size: usize,
#[validate(custom(function = "validate_page_no", arg = "&'v_a RangeFrom<usize>"))]
#[validate(custom(function = "validate_page_no", use_context))]
pub page_no: usize,
}
fn validate_page_size(v: usize, args: &RangeInclusive<usize>) -> Result<(), ValidationError> {
args.contains(&v)
fn validate_page_size(v: &usize, args: &PagerValidArgs) -> Result<(), ValidationError> {
args.page_size_range
.contains(&v)
.then_some(())
.ok_or_else(|| ValidationError::new("page_size is out of range"))
}
fn validate_page_no(v: usize, args: &RangeFrom<usize>) -> Result<(), ValidationError> {
args.contains(&v)
fn validate_page_no(v: &usize, args: &PagerValidArgs) -> Result<(), ValidationError> {
args.page_no_range
.contains(&v)
.then_some(())
.ok_or_else(|| ValidationError::new("page_no is out of range"))
}
@@ -298,18 +301,7 @@ pub struct PagerValidArgs {
page_no_range: RangeFrom<usize>,
}
// NOTE: This implementation allows PagerValidArgs to be the second member of ValidEx, and provides arguments for actual validation.
// get() method returns the actual validation arguments to be used during validation.
impl<'a> Arguments<'a> for PagerValidArgs {
type T = Pager;
// NOTE: <Pager as ValidateArgs<'a>>::Args == (&RangeInclusive<usize>, &RangeFrom<usize>)
fn get(&'a self) -> <Pager as ValidateArgs<'a>>::Args {
(&self.page_size_range, &self.page_no_range)
}
}
pub async fn pager_from_form_ex(ValidEx(Form(pager), _): ValidEx<Form<Pager>, PagerValidArgs>) {
pub async fn pager_from_form_ex(ValidEx(Form(pager)): ValidEx<Form<Pager>>) {
assert!((1..=50).contains(&pager.page_size));
assert!((1..).contains(&pager.page_no));
}
@@ -408,11 +400,10 @@ Current module documentation predominantly showcases `Valid` examples, the usage
## 🗂️ Extractors List
| Extractor | Backend / Feature | Data's trait bound | Functionality | Benefits | Drawbacks |
|-----------------------|-------------------|---------------------------------------------------------------------------------|----------------------------------------|--------------------------------------------|--------------------------------------------------|
| `Valid<E>` | validator | `validator::Validate` | Validation | | |
| `ValidEx<E, A>` | validator | `validator::ValidateArgs` | Validation with arguments | | More complex arguments coding |
| `ValidEx<E>` | validator | `validator::ValidateArgs` | Validation with arguments | | |
| `Garde<E>` | garde | `garde::Validate` | Validation with or without arguments | | Require empty tuple as the argument if use state | |
| `Validated<E>` | validify | `validify::Validate` | Validation | | |
| `Modified<E>` | validify | `validify::Modify` | Modification / Conversion to response | | |
@@ -437,6 +428,7 @@ Current module documentation predominantly showcases `Valid` examples, the usage
| yaml | Enables support for `Yaml` from `axum-serde` | [`yaml`] | ❌ | ✅ | ✅ |
| xml | Enables support for `Xml` from `axum-serde` | [`xml`] | ❌ | ✅ | ✅ |
| toml | Enables support for `Toml` from `axum-serde` | [`toml`] | ❌ | ✅ | ✅ |
| sonic | Enables support for `Sonic` from `axum-serde` | [`sonic`] | ❌ | ✅ | ✅ |
| extra | Enables support for `Cached`, `WithRejection` from `axum-extra` | [`extra`] | ❌ | ✅ | ✅ |
| extra_typed_path | Enables support for `T: TypedPath` from `axum-extra` | [`extra::typed_path`] | ❌ | ✅ | ✅ |
| extra_query | Enables support for `Query` from `axum-extra` | [`extra::query`] | ❌ | ✅ | ✅ |
@@ -454,9 +446,11 @@ Current module documentation predominantly showcases `Valid` examples, the usage
## 🔌 Compatibility
To determine the compatible versions of dependencies that work together, please refer to the dependencies listed in the `Cargo.toml` file. The version numbers listed there will indicate the compatible versions.
To determine the compatible versions of dependencies that work together, please refer to the dependencies listed in
the `Cargo.toml` file. The version numbers listed there will indicate the compatible versions.
If you encounter code compilation problems, it could be attributed to either **missing trait bounds**, **unmet feature requirements**, or **incorrect dependency version selections**.
If you encounter code compilation problems, it could be attributed to either **missing trait bounds**, **unmet feature
requirements**, or **incorrect dependency version selections**.
## 📜 License

View File

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

View File

@@ -56,7 +56,7 @@ pub trait HasValidate {
}
#[cfg(feature = "validator")]
pub use crate::validator::{Arguments, HasValidateArgs, Valid, ValidEx, ValidRejection};
pub use crate::validator::{HasValidateArgs, Valid, ValidEx, ValidRejection};
#[cfg(feature = "garde")]
pub use crate::garde::{Garde, GardeRejection};

View File

@@ -81,9 +81,9 @@ where
/// Although current module documentation predominantly showcases `Valid` examples, the usage of `ValidEx` is analogous.
///
#[derive(Debug, Clone, Copy, Default)]
pub struct ValidEx<E, A>(pub E, pub A);
pub struct ValidEx<E>(pub E);
impl<E, A> Deref for ValidEx<E, A> {
impl<E> Deref for ValidEx<E> {
type Target = E;
fn deref(&self) -> &Self::Target {
@@ -91,19 +91,19 @@ impl<E, A> Deref for ValidEx<E, A> {
}
}
impl<E, A> DerefMut for ValidEx<E, A> {
impl<E> DerefMut for ValidEx<E> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T: Display, A> Display for ValidEx<T, A> {
impl<T: Display> Display for ValidEx<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<E, A> ValidEx<E, A> {
impl<E> ValidEx<E> {
/// Consumes the `ValidEx` and returns the validated data within.
///
/// This returns the `E` type which represents the data that has been
@@ -111,22 +111,10 @@ impl<E, A> ValidEx<E, A> {
pub fn into_inner(self) -> E {
self.0
}
/// Returns a reference to the validation arguments.
///
/// This provides access to the `A` type which contains the arguments used
/// to validate the data. These arguments were passed to the validation
/// function.
pub fn arguments<'a>(&'a self) -> <<A as Arguments<'a>>::T as ValidateArgs<'a>>::Args
where
A: Arguments<'a>,
{
self.1.get()
}
}
#[cfg(feature = "aide")]
impl<T, A> aide::OperationInput for ValidEx<T, A>
impl<T> aide::OperationInput for ValidEx<T>
where
T: aide::OperationInput,
{
@@ -135,23 +123,6 @@ where
}
}
/// `Arguments` provides the validation arguments for the data type `T`.
///
/// This trait has an associated type `T` which represents the data type to
/// validate. `T` must implement the `ValidateArgs` trait which defines the
/// validation logic.
///
/// It's important to mention that types implementing `Arguments` should be a part of the router's state
/// (either through implementing `FromRef<StateType>` or by directly becoming the state)
/// to enable automatic arguments retrieval during validation.
///
pub trait Arguments<'a> {
/// The data type to validate using this arguments
type T: ValidateArgs<'a>;
/// This method gets the arguments required by `ValidateArgs::validate_args`
fn get(&'a self) -> <<Self as Arguments<'a>>::T as ValidateArgs<'a>>::Args;
}
/// `ValidRejection` is returned when the `Valid` or `ValidEx` extractor fails.
///
pub type ValidRejection<E> = ValidationRejection<ValidationErrors, E>;
@@ -210,14 +181,12 @@ where
}
#[async_trait]
impl<State, Extractor, Args> FromRequest<State> for ValidEx<Extractor, Args>
impl<State, Extractor, Args> FromRequest<State> for ValidEx<Extractor>
where
State: Send + Sync,
Args: Send
+ Sync
+ FromRef<State>
+ for<'a> Arguments<'a, T = <Extractor as HasValidateArgs<'a>>::ValidateArgs>,
Args: Send + Sync + FromRef<State>,
Extractor: for<'v> HasValidateArgs<'v> + FromRequest<State>,
for<'v> <Extractor as HasValidateArgs<'v>>::ValidateArgs: ValidateArgs<'v, Args = &'v Args>,
{
type Rejection = ValidRejection<<Extractor as FromRequest<State>>::Rejection>;
@@ -227,20 +196,18 @@ where
.await
.map_err(ValidRejection::Inner)?;
inner.get_validate_args().validate_args(arguments.get())?;
Ok(ValidEx(inner, arguments))
inner.get_validate_args().validate_with_args(&arguments)?;
Ok(ValidEx(inner))
}
}
#[async_trait]
impl<State, Extractor, Args> FromRequestParts<State> for ValidEx<Extractor, Args>
impl<State, Extractor, Args> FromRequestParts<State> for ValidEx<Extractor>
where
State: Send + Sync,
Args: Send
+ Sync
+ FromRef<State>
+ for<'a> Arguments<'a, T = <Extractor as HasValidateArgs<'a>>::ValidateArgs>,
Args: Send + Sync + FromRef<State>,
Extractor: for<'v> HasValidateArgs<'v> + FromRequestParts<State>,
for<'v> <Extractor as HasValidateArgs<'v>>::ValidateArgs: ValidateArgs<'v, Args = &'v Args>,
{
type Rejection = ValidRejection<<Extractor as FromRequestParts<State>>::Rejection>;
@@ -249,8 +216,8 @@ where
let inner = Extractor::from_request_parts(parts, state)
.await
.map_err(ValidRejection::Inner)?;
inner.get_validate_args().validate_args(arguments.get())?;
Ok(ValidEx(inner, arguments))
inner.get_validate_args().validate_with_args(&arguments)?;
Ok(ValidEx(inner))
}
}
@@ -278,20 +245,22 @@ pub mod tests {
#[test]
fn valid_ex_deref_deref_mut_into_inner_arguments() {
let mut inner = String::from(TEST);
let mut v = ValidEx(inner.clone(), ());
let mut v = ValidEx(inner.clone());
assert_eq!(&inner, v.deref());
inner.push_str(TEST);
v.deref_mut().push_str(TEST);
assert_eq!(&inner, v.deref());
assert_eq!(inner, v.into_inner());
fn validate(_v: i32, _args: i32) -> Result<(), ValidationError> {
fn validate(v: &i32, args: &DataVA) -> Result<(), ValidationError> {
assert!(*v < args.a);
Ok(())
}
#[derive(Debug, Validate)]
#[validate(context = DataVA)]
struct Data {
#[validate(custom(function = "validate", arg = "i32"))]
#[validate(custom(function = "validate", use_context))]
v: i32,
}
@@ -305,21 +274,13 @@ pub mod tests {
a: i32,
}
impl<'a> Arguments<'a> for DataVA {
type T = Data;
fn get(&'a self) -> <<Self as Arguments<'a>>::T as ValidateArgs<'a>>::Args {
self.a
}
}
let data = Data { v: 12 };
let args = DataVA { a: 123 };
let ve = ValidEx(data, args);
let v = 12;
let data = Data { v };
let args = DataVA { a: v + 1 };
let ve = ValidEx(data);
ve.validate_with_args(&args).expect("invalid");
println!("{}", ve);
assert_eq!(ve.v, 12);
let a = ve.arguments();
assert_eq!(a, 123);
assert_eq!(ve.v, v);
}
#[test]

View File

@@ -1,8 +1,8 @@
#![cfg(feature = "validator")]
use crate::tests::{ValidTest, ValidTestParameter};
use crate::{Arguments, HasValidate, HasValidateArgs, Valid, ValidEx, VALIDATION_ERROR_STATUS};
use axum::extract::{FromRef, Path, Query};
use crate::{HasValidate, HasValidateArgs, Valid, ValidEx, VALIDATION_ERROR_STATUS};
use axum::extract::{FromRef, Path, Query, State};
use axum::http::StatusCode;
use axum::routing::{get, post};
use axum::{Form, Json, Router};
@@ -37,23 +37,28 @@ pub struct Parameters {
feature = "typed_multipart",
derive(axum_typed_multipart::TryFromMultipart)
)]
#[validate(context = ParametersExValidationArguments)]
pub struct ParametersEx {
#[validate(custom(function = "validate_v0", arg = "&'v_a RangeInclusive<i32>"))]
#[validate(custom(function = "validate_v0", use_context))]
#[cfg_attr(feature = "extra_protobuf", prost(int32, tag = "1"))]
v0: i32,
#[validate(custom(function = "validate_v1", arg = "&'v_a RangeInclusive<usize>"))]
#[validate(custom(function = "validate_v1", use_context))]
#[cfg_attr(feature = "extra_protobuf", prost(string, tag = "2"))]
v1: String,
}
fn validate_v0(v: i32, args: &RangeInclusive<i32>) -> Result<(), ValidationError> {
args.contains(&v)
fn validate_v0(v: &i32, args: &ParametersExValidationArguments) -> Result<(), ValidationError> {
args.inner
.v0_range
.contains(v)
.then_some(())
.ok_or_else(|| ValidationError::new("v0 is out of range"))
}
fn validate_v1(v: &str, args: &RangeInclusive<usize>) -> Result<(), ValidationError> {
args.contains(&v.len())
fn validate_v1(v: &str, args: &ParametersExValidationArguments) -> Result<(), ValidationError> {
args.inner
.v1_length_range
.contains(&v.len())
.then_some(())
.ok_or_else(|| ValidationError::new("v1 is invalid"))
}
@@ -69,13 +74,6 @@ pub struct ParametersExValidationArguments {
inner: Arc<ParametersExValidationArgumentsInner>,
}
impl<'a> Arguments<'a> for ParametersExValidationArguments {
type T = ParametersEx;
fn get(&'a self) -> <ParametersEx as ValidateArgs<'a>>::Args {
(&self.inner.v0_range, &self.inner.v1_length_range)
}
}
impl Default for ParametersExValidationArgumentsInner {
fn default() -> Self {
Self {
@@ -146,12 +144,18 @@ async fn test_main() -> anyhow::Result<()> {
let router = Router::new()
.route(route::PATH, get(extract_path))
.route(route::PATH_EX, get(extract_path_ex));
#[cfg(feature = "query")]
let router = router
.route(route::QUERY, get(extract_query))
.route(route::QUERY_EX, get(extract_query_ex));
#[cfg(feature = "form")]
let router = router
.route(route::FORM, post(extract_form))
.route(route::FORM_EX, post(extract_form_ex));
#[cfg(feature = "json")]
let router = router
.route(route::JSON, post(extract_json))
.route(route::PATH_EX, get(extract_path_ex))
.route(route::QUERY_EX, get(extract_query_ex))
.route(route::FORM_EX, post(extract_form_ex))
.route(route::JSON_EX, post(extract_json_ex));
#[cfg(feature = "typed_header")]
@@ -355,6 +359,8 @@ async fn test_main() -> anyhow::Result<()> {
test_extra_path(&test_executor, "path", &server_url).await?;
test_extra_path(&test_executor, "path_ex", &server_url).await?;
#[cfg(feature = "query")]
{
// Valid
test_executor
.execute::<Query<Parameters>>(Method::GET, route::QUERY)
@@ -364,7 +370,10 @@ async fn test_main() -> anyhow::Result<()> {
test_executor
.execute::<Query<Parameters>>(Method::GET, route::QUERY_EX)
.await?;
}
#[cfg(feature = "form")]
{
// Valid
test_executor
.execute::<Form<Parameters>>(Method::POST, route::FORM)
@@ -374,7 +383,10 @@ async fn test_main() -> anyhow::Result<()> {
test_executor
.execute::<Form<Parameters>>(Method::POST, route::FORM_EX)
.await?;
}
#[cfg(feature = "json")]
{
// Valid
test_executor
.execute::<Json<Parameters>>(Method::POST, route::JSON)
@@ -384,6 +396,7 @@ async fn test_main() -> anyhow::Result<()> {
test_executor
.execute::<Json<Parameters>>(Method::POST, route::JSON_EX)
.await?;
}
#[cfg(feature = "typed_header")]
{
@@ -729,9 +742,10 @@ async fn extract_path(Valid(Path(parameters)): Valid<Path<Parameters>>) -> Statu
}
async fn extract_path_ex(
ValidEx(Path(parameters), args): ValidEx<Path<ParametersEx>, ParametersExValidationArguments>,
ValidEx(Path(parameters)): ValidEx<Path<ParametersEx>>,
State(arguments): State<ParametersExValidationArguments>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
async fn extract_query(Valid(Query(parameters)): Valid<Query<Parameters>>) -> StatusCode {
@@ -739,9 +753,10 @@ async fn extract_query(Valid(Query(parameters)): Valid<Query<Parameters>>) -> St
}
async fn extract_query_ex(
ValidEx(Query(parameters), args): ValidEx<Query<ParametersEx>, ParametersExValidationArguments>,
ValidEx(Query(parameters)): ValidEx<Query<ParametersEx>>,
State(arguments): State<ParametersExValidationArguments>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
async fn extract_form(Valid(Form(parameters)): Valid<Form<Parameters>>) -> StatusCode {
@@ -749,9 +764,10 @@ async fn extract_form(Valid(Form(parameters)): Valid<Form<Parameters>>) -> Statu
}
async fn extract_form_ex(
ValidEx(Form(parameters), args): ValidEx<Form<ParametersEx>, ParametersExValidationArguments>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Form(parameters)): ValidEx<Form<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
async fn extract_json(Valid(Json(parameters)): Valid<Json<Parameters>>) -> StatusCode {
@@ -759,9 +775,10 @@ async fn extract_json(Valid(Json(parameters)): Valid<Json<Parameters>>) -> Statu
}
async fn extract_json_ex(
ValidEx(Json(parameters), args): ValidEx<Json<ParametersEx>, ParametersExValidationArguments>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Json(parameters)): ValidEx<Json<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
fn validate_again<V: Validate>(validate: V) -> StatusCode {
@@ -783,7 +800,7 @@ fn validate_again_ex<'v, V: ValidateArgs<'v>>(
// it should have returned `400 BAD REQUEST` if the `parameters` were invalid,
// Let's validate them again to check if the `ValidEx` extractor works well.
// If it works properly, this function will never return `500 INTERNAL SERVER ERROR`
match validate.validate_args(args) {
match validate.validate_with_args(args) {
Ok(_) => StatusCode::OK,
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
@@ -798,7 +815,8 @@ mod typed_header {
use super::{validate_again, Parameters};
use super::{validate_again_ex, ParametersEx, ParametersExValidationArguments};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_extra::headers::{Error, Header, HeaderName, HeaderValue};
use axum_extra::typed_header::TypedHeader;
@@ -812,12 +830,10 @@ mod typed_header {
}
pub(super) async fn extract_typed_header_ex(
ValidEx(TypedHeader(parameters), args): ValidEx<
TypedHeader<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(TypedHeader(parameters)): ValidEx<TypedHeader<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
impl Header for Parameters {
@@ -906,7 +922,8 @@ mod typed_multipart {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_typed_multipart::{BaseMultipart, TypedMultipart, TypedMultipartError};
@@ -932,12 +949,10 @@ mod typed_multipart {
}
pub(super) async fn extract_typed_multipart_ex(
ValidEx(TypedMultipart(parameters), args): ValidEx<
TypedMultipart<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(TypedMultipart(parameters)): ValidEx<TypedMultipart<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
pub(super) async fn extract_base_multipart(
@@ -947,12 +962,12 @@ mod typed_multipart {
}
pub(super) async fn extract_base_multipart_ex(
ValidEx(BaseMultipart { data, .. }, args): ValidEx<
State(arguments): State<ParametersExValidationArguments>,
ValidEx(BaseMultipart { data, .. }): ValidEx<
BaseMultipart<ParametersEx, TypedMultipartError>,
ParametersExValidationArguments,
>,
) -> StatusCode {
validate_again_ex(data, args.get())
validate_again_ex(data, &arguments)
}
}
@@ -963,8 +978,8 @@ mod extra {
ParametersExValidationArguments,
};
use crate::tests::{Rejection, ValidTest, ValidTestParameter};
use crate::{Arguments, Valid, ValidEx, ValidRejection};
use axum::extract::FromRequestParts;
use crate::{Valid, ValidEx, ValidRejection};
use axum::extract::{FromRequestParts, State};
use axum::http::request::Parts;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
@@ -1094,12 +1109,10 @@ mod extra {
}
pub async fn extract_cached_ex(
ValidEx(Cached(parameters), args): ValidEx<
Cached<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Cached(parameters)): ValidEx<Cached<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
pub async fn extract_with_rejection(
@@ -1111,12 +1124,12 @@ mod extra {
}
pub async fn extract_with_rejection_ex(
ValidEx(WithRejection(parameters, _), args): ValidEx<
State(arguments): State<ParametersExValidationArguments>,
ValidEx(WithRejection(parameters, _)): ValidEx<
WithRejection<ParametersEx, ValidWithRejectionRejection>,
ParametersExValidationArguments,
>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
pub struct WithRejectionValidRejection<E> {
@@ -1147,24 +1160,26 @@ mod extra {
}
pub async fn extract_with_rejection_valid_ex(
WithRejection(ValidEx(parameters, args), _): WithRejection<
ValidEx<ParametersEx, ParametersExValidationArguments>,
State(arguments): State<ParametersExValidationArguments>,
WithRejection(ValidEx(parameters), _): WithRejection<
ValidEx<ParametersEx>,
WithRejectionValidRejection<ParametersRejection>,
>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
#[cfg(feature = "extra_typed_path")]
mod extra_typed_path {
use super::{validate_again, validate_again_ex};
use crate::{Arguments, HasValidate, HasValidateArgs, Valid, ValidEx};
use crate::{HasValidate, HasValidateArgs, Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_extra::routing::TypedPath;
use serde::Deserialize;
use std::ops::RangeInclusive;
use validator::{Validate, ValidateArgs};
use validator::{Validate, ValidationError};
pub mod route {
pub const EXTRA_TYPED_PATH: &str = "/extra_typed_path/:v0/:v1";
@@ -1192,12 +1207,33 @@ mod extra_typed_path {
validate_again(param)
}
fn validate_v0(
v: &i32,
args: &TypedPathParamExValidationArguments,
) -> Result<(), ValidationError> {
args.v0_range
.contains(v)
.then_some(())
.ok_or_else(|| ValidationError::new("v0 is out of range"))
}
fn validate_v1(
v: &str,
args: &TypedPathParamExValidationArguments,
) -> Result<(), ValidationError> {
args.v1_length_range
.contains(&v.len())
.then_some(())
.ok_or_else(|| ValidationError::new("v1 is invalid"))
}
#[derive(Validate, TypedPath, Deserialize)]
#[typed_path("/extra_typed_path_ex/:v0/:v1")]
#[validate(context = TypedPathParamExValidationArguments)]
pub struct TypedPathParamEx {
#[validate(custom(function = "super::validate_v0", arg = "&'v_a RangeInclusive<i32>"))]
#[validate(custom(function = "validate_v0", use_context))]
v0: i32,
#[validate(custom(function = "super::validate_v1", arg = "&'v_a RangeInclusive<usize>"))]
#[validate(custom(function = "validate_v1", use_context))]
v1: String,
}
@@ -1224,17 +1260,11 @@ mod extra_typed_path {
}
}
impl<'a> Arguments<'a> for TypedPathParamExValidationArguments {
type T = TypedPathParamEx;
fn get(&'a self) -> <TypedPathParamEx as ValidateArgs<'a>>::Args {
(&self.v0_range, &self.v1_length_range)
}
}
pub async fn extract_extra_typed_path_ex(
ValidEx(param, args): ValidEx<TypedPathParamEx, TypedPathParamExValidationArguments>,
State(arguments): State<TypedPathParamExValidationArguments>,
ValidEx(param): ValidEx<TypedPathParamEx>,
) -> StatusCode {
validate_again_ex(param, args.get())
validate_again_ex(param, &arguments)
}
}
@@ -1244,7 +1274,8 @@ mod extra_query {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_extra::extract::Query;
@@ -1260,12 +1291,10 @@ mod extra_query {
}
pub async fn extract_extra_query_ex(
ValidEx(Query(parameters), args): ValidEx<
Query<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Query(parameters)): ValidEx<Query<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
@@ -1275,7 +1304,8 @@ mod extra_form {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_extra::extract::Form;
@@ -1291,12 +1321,10 @@ mod extra_form {
}
pub async fn extract_extra_form_ex(
ValidEx(Form(parameters), args): ValidEx<
Form<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Form(parameters)): ValidEx<Form<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
@@ -1306,7 +1334,8 @@ mod extra_protobuf {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_extra::protobuf::Protobuf;
@@ -1322,12 +1351,10 @@ mod extra_protobuf {
}
pub async fn extract_extra_protobuf_ex(
ValidEx(Protobuf(parameters), args): ValidEx<
Protobuf<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Protobuf(parameters)): ValidEx<Protobuf<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
@@ -1337,7 +1364,8 @@ mod yaml {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_serde::Yaml;
@@ -1351,12 +1379,10 @@ mod yaml {
}
pub async fn extract_yaml_ex(
ValidEx(Yaml(parameters), args): ValidEx<
Yaml<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Yaml(parameters)): ValidEx<Yaml<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
@@ -1366,7 +1392,8 @@ mod msgpack {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_serde::{MsgPack, MsgPackRaw};
@@ -1384,12 +1411,10 @@ mod msgpack {
}
pub async fn extract_msgpack_ex(
ValidEx(MsgPack(parameters), args): ValidEx<
MsgPack<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(MsgPack(parameters)): ValidEx<MsgPack<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
pub async fn extract_msgpack_raw(
@@ -1399,12 +1424,10 @@ mod msgpack {
}
pub async fn extract_msgpack_raw_ex(
ValidEx(MsgPackRaw(parameters), args): ValidEx<
MsgPackRaw<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(MsgPackRaw(parameters)): ValidEx<MsgPackRaw<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
@@ -1414,7 +1437,8 @@ mod xml {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_serde::Xml;
@@ -1428,9 +1452,10 @@ mod xml {
}
pub async fn extract_xml_ex(
ValidEx(Xml(parameters), args): ValidEx<Xml<ParametersEx>, ParametersExValidationArguments>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Xml(parameters)): ValidEx<Xml<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
@@ -1440,7 +1465,8 @@ mod toml {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_serde::Toml;
@@ -1454,12 +1480,10 @@ mod toml {
}
pub async fn extract_toml_ex(
ValidEx(Toml(parameters), args): ValidEx<
Toml<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Toml(parameters)): ValidEx<Toml<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}
@@ -1469,7 +1493,8 @@ mod sonic {
validate_again, validate_again_ex, Parameters, ParametersEx,
ParametersExValidationArguments,
};
use crate::{Arguments, Valid, ValidEx};
use crate::{Valid, ValidEx};
use axum::extract::State;
use axum::http::StatusCode;
use axum_serde::Sonic;
@@ -1483,11 +1508,9 @@ mod sonic {
}
pub async fn extract_sonic_ex(
ValidEx(Sonic(parameters), args): ValidEx<
Sonic<ParametersEx>,
ParametersExValidationArguments,
>,
State(arguments): State<ParametersExValidationArguments>,
ValidEx(Sonic(parameters)): ValidEx<Sonic<ParametersEx>>,
) -> StatusCode {
validate_again_ex(parameters, args.get())
validate_again_ex(parameters, &arguments)
}
}