From 295ae17697f07dd83a80e0d1f9793bb2f3a96152 Mon Sep 17 00:00:00 2001 From: gengteng Date: Fri, 21 Jul 2023 21:01:10 +0800 Subject: [PATCH] Refactor: Split HasValidate implementations for four different extractors into multiple files and export them using feature flags --- Cargo.toml | 15 ++++++++-- examples/basic.rs | 2 ++ examples/custom.rs | 2 ++ src/form.rs | 21 ++++++++++++++ src/json.rs | 21 ++++++++++++++ src/lib.rs | 69 +++++++--------------------------------------- src/path.rs | 21 ++++++++++++++ src/query.rs | 21 ++++++++++++++ 8 files changed, 111 insertions(+), 61 deletions(-) create mode 100644 examples/basic.rs create mode 100644 examples/custom.rs create mode 100644 src/form.rs create mode 100644 src/json.rs create mode 100644 src/path.rs create mode 100644 src/query.rs diff --git a/Cargo.toml b/Cargo.toml index f7c8804..32863f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum-valid" -version = "0.2.2" +version = "0.2.3" description = "Validation tools for axum using the validator library." authors = ["GengTeng "] license = "MIT" @@ -20,5 +20,16 @@ categories = [ edition = "2021" [dependencies] -axum = "0.6.18" +axum = { version = "0.6.18", default-features = false } validator = "0.16.0" + +[dev-dependencies] +anyhow = "1.0.71" +tokio = { version = "1.28.2", features = ["full"] } +reqwest = { version = "0.11.18", features = ["json"] } + +[features] +default = ["json", "form", "query"] +json = ["axum/json"] +form = ["axum/form"] +query = ["axum/query"] diff --git a/examples/basic.rs b/examples/basic.rs new file mode 100644 index 0000000..7f755fb --- /dev/null +++ b/examples/basic.rs @@ -0,0 +1,2 @@ +#[tokio::main] +async fn main() {} diff --git a/examples/custom.rs b/examples/custom.rs new file mode 100644 index 0000000..7f755fb --- /dev/null +++ b/examples/custom.rs @@ -0,0 +1,2 @@ +#[tokio::main] +async fn main() {} diff --git a/src/form.rs b/src/form.rs new file mode 100644 index 0000000..c0a1f1b --- /dev/null +++ b/src/form.rs @@ -0,0 +1,21 @@ +//! # Implementation of the `HasValidate` trait for the `Form` extractor. +//! + +use crate::{HasValidate, ValidRejection}; +use axum::extract::rejection::FormRejection; +use axum::Form; +use validator::Validate; + +impl HasValidate for Form { + type Validate = T; + type Rejection = FormRejection; + fn get_validate(&self) -> &T { + &self.0 + } +} + +impl From for ValidRejection { + fn from(value: FormRejection) -> Self { + Self::Inner(value) + } +} diff --git a/src/json.rs b/src/json.rs new file mode 100644 index 0000000..f23f2fc --- /dev/null +++ b/src/json.rs @@ -0,0 +1,21 @@ +//! # Implementation of the `HasValidate` trait for the `Json` extractor. +//! + +use crate::{HasValidate, ValidRejection}; +use axum::extract::rejection::JsonRejection; +use axum::Json; +use validator::Validate; + +impl HasValidate for Json { + type Validate = T; + type Rejection = JsonRejection; + fn get_validate(&self) -> &T { + &self.0 + } +} + +impl From for ValidRejection { + fn from(value: JsonRejection) -> Self { + Self::Inner(value) + } +} diff --git a/src/lib.rs b/src/lib.rs index 68326f0..fd2cfc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,19 @@ #![doc = include_str!("../README.md")] #![deny(unsafe_code, missing_docs, clippy::unwrap_used)] -use axum::extract::rejection::{FormRejection, JsonRejection, PathRejection, QueryRejection}; -use axum::extract::{FromRequest, FromRequestParts, Path, Query}; +#[cfg(feature = "form")] +pub mod form; +#[cfg(feature = "json")] +pub mod json; +pub mod path; +#[cfg(feature = "query")] +pub mod query; + +use axum::async_trait; +use axum::extract::{FromRequest, FromRequestParts}; use axum::http::request::Parts; use axum::http::{Request, StatusCode}; use axum::response::{IntoResponse, Response}; -use axum::{async_trait, Form, Json}; use validator::{Validate, ValidationErrors}; /// Valid entity extractor @@ -39,30 +46,6 @@ impl IntoResponse for ValidRejection { } } -impl From for ValidRejection { - fn from(value: JsonRejection) -> Self { - Self::Inner(value) - } -} - -impl From for ValidRejection { - fn from(value: QueryRejection) -> Self { - Self::Inner(value) - } -} - -impl From for ValidRejection { - fn from(value: PathRejection) -> Self { - Self::Inner(value) - } -} - -impl From for ValidRejection { - fn from(value: FormRejection) -> Self { - Self::Inner(value) - } -} - /// Trait for types that can provide a reference that can be validated for correctness. pub trait HasValidate { /// Inner type that can be validated for correctness @@ -74,38 +57,6 @@ pub trait HasValidate { fn get_validate(&self) -> &Self::Validate; } -impl HasValidate for Json { - type Validate = T; - type Rejection = JsonRejection; - fn get_validate(&self) -> &T { - &self.0 - } -} - -impl HasValidate for Form { - type Validate = T; - type Rejection = FormRejection; - fn get_validate(&self) -> &T { - &self.0 - } -} - -impl HasValidate for Query { - type Validate = T; - type Rejection = QueryRejection; - fn get_validate(&self) -> &T { - &self.0 - } -} - -impl HasValidate for Path { - type Validate = T; - type Rejection = PathRejection; - fn get_validate(&self) -> &T { - &self.0 - } -} - #[async_trait] impl FromRequest for Valid where diff --git a/src/path.rs b/src/path.rs new file mode 100644 index 0000000..8ee3df4 --- /dev/null +++ b/src/path.rs @@ -0,0 +1,21 @@ +//! # Implementation of the `HasValidate` trait for the `Path` extractor. +//! + +use crate::{HasValidate, ValidRejection}; +use axum::extract::rejection::PathRejection; +use axum::extract::Path; +use validator::Validate; + +impl HasValidate for Path { + type Validate = T; + type Rejection = PathRejection; + fn get_validate(&self) -> &T { + &self.0 + } +} + +impl From for ValidRejection { + fn from(value: PathRejection) -> Self { + Self::Inner(value) + } +} diff --git a/src/query.rs b/src/query.rs new file mode 100644 index 0000000..87a79ed --- /dev/null +++ b/src/query.rs @@ -0,0 +1,21 @@ +//! # Implementation of the `HasValidate` trait for the `Query` extractor. +//! + +use crate::{HasValidate, ValidRejection}; +use axum::extract::rejection::QueryRejection; +use axum::extract::Query; +use validator::Validate; + +impl HasValidate for Query { + type Validate = T; + type Rejection = QueryRejection; + fn get_validate(&self) -> &T { + &self.0 + } +} + +impl From for ValidRejection { + fn from(value: QueryRejection) -> Self { + Self::Inner(value) + } +}