diff --git a/Cargo.toml b/Cargo.toml index 2fb3011..5dd61c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,7 @@ optional = true [dev-dependencies] anyhow = "1.0.72" -axum = { version = "0.6.20" } +axum = { version = "0.6.20", features = ["macros"] } tokio = { version = "1.29.1", features = ["full"] } hyper = { version = "0.14.27", features = ["full"] } reqwest = { version = "0.11.18", features = ["json", "multipart"] } diff --git a/src/lib.rs b/src/lib.rs index 5823acd..34acf93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -213,7 +213,7 @@ impl IntoResponse for ValidRejection { } } -/// Trait for types that can provide a reference that can be validated for correctness. +/// Trait for types that can supply a reference that can be validated. /// /// Extractor types `T` that implement this trait can be used with `Valid`. /// @@ -224,9 +224,12 @@ pub trait HasValidate { fn get_validate(&self) -> &Self::Validate; } +/// Trait for types that can supply a reference that can be validated using arguments. +/// +/// Extractor types `T` that implement this trait can be used with `ValidEx`. /// pub trait HasValidateArgs<'v> { - /// Inner type that can be validated for correctness + /// Inner type that can be validated using arguments type ValidateArgs: ValidateArgs<'v>; /// Get the inner value fn get_validate_args(&self) -> &Self::ValidateArgs; diff --git a/src/test.rs b/src/test.rs index 9213f92..d80ce46 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,6 +1,6 @@ use crate::tests::{ValidTest, ValidTestParameter}; use crate::{Arguments, HasValidate, Valid, ValidEx, ValidationContext, VALIDATION_ERROR_STATUS}; -use axum::extract::{Path, Query}; +use axum::extract::{FromRef, Path, Query}; use axum::routing::{get, post}; use axum::{Form, Json, Router}; use hyper::Method; @@ -107,6 +107,11 @@ impl HasValidate for Parameters { } } +#[derive(Debug, Clone, FromRef)] +struct MyState { + validation_ctx: ValidationContext, +} + #[tokio::test] async fn test_main() -> anyhow::Result<()> { let router = Router::new() @@ -117,8 +122,11 @@ async fn test_main() -> anyhow::Result<()> { let router_ex = Router::new() .route(route::QUERY_EX, get(extract_query_ex)) + .route(route::FORM_EX, post(extract_form_ex)) .route(route::JSON_EX, post(extract_json_ex)) - .with_state(ValidationContext::::default()); + .with_state(MyState { + validation_ctx: ValidationContext::::default(), + }); let router = router.merge(router_ex); @@ -257,10 +265,16 @@ async fn test_main() -> anyhow::Result<()> { .execute::>(Method::GET, route::QUERY_EX) .await?; + // Valid test_executor .execute::>(Method::POST, route::FORM) .await?; + // ValidEx + test_executor + .execute::>(Method::POST, route::FORM_EX) + .await?; + // Valid test_executor .execute::>(Method::POST, route::JSON) @@ -506,6 +520,7 @@ mod route { pub const QUERY: &str = "/query"; pub const QUERY_EX: &str = "/query_ex"; pub const FORM: &str = "/form"; + pub const FORM_EX: &str = "/form_ex"; pub const JSON: &str = "/json"; pub const JSON_EX: &str = "/json_ex"; } @@ -528,6 +543,12 @@ async fn extract_form(Valid(Form(parameters)): Valid>) -> Statu validate_again(parameters) } +async fn extract_form_ex( + ValidEx(Form(parameters), args): ValidEx, ParametersExValidationArguments>, +) -> StatusCode { + validate_again_ex(parameters, args.get()) +} + async fn extract_json(Valid(Json(parameters)): Valid>) -> StatusCode { validate_again(parameters) }