add into_json feature

This commit is contained in:
gengteng
2023-08-01 12:38:14 +08:00
parent b7fdcb15ff
commit 91ce221dd3
6 changed files with 52 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "axum-valid" name = "axum-valid"
version = "0.3.0" version = "0.4.0"
description = "Validation tools for axum using the validator library." description = "Validation tools for axum using the validator library."
authors = ["GengTeng <me@gteng.org>"] authors = ["GengTeng <me@gteng.org>"]
license = "MIT" license = "MIT"
@@ -23,6 +23,11 @@ edition = "2021"
axum = { version = "0.6.18", default-features = false } axum = { version = "0.6.18", default-features = false }
validator = "0.16.0" validator = "0.16.0"
[dependencies.serde_json]
package = "serde_json"
version = "1.0.103"
optional = true
[dev-dependencies] [dev-dependencies]
anyhow = "1.0.71" anyhow = "1.0.71"
axum = { version = "0.6.18" } axum = { version = "0.6.18" }
@@ -38,3 +43,4 @@ default = ["json", "form", "query"]
json = ["axum/json"] json = ["axum/json"]
form = ["axum/form"] form = ["axum/form"]
query = ["axum/query"] query = ["axum/query"]
into_json = ["serde_json"]

View File

@@ -44,4 +44,8 @@ pub async fn get_page_by_json(
} }
``` ```
For more usage examples, please refer to the `basic.rs` and `custom.rs` files in the `tests` directory. For more usage examples, please refer to the `basic.rs` and `custom.rs` files in the `tests` directory.
## Features
`into_json`: When this feature is enabled, validation errors will be serialized into JSON format and returned as the HTTP body.

View File

@@ -39,8 +39,17 @@ impl<E: IntoResponse> IntoResponse for ValidRejection<E> {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match self { match self {
ValidRejection::Valid(validate_error) => { ValidRejection::Valid(validate_error) => {
(StatusCode::BAD_REQUEST, validate_error.to_string()).into_response() #[cfg(feature = "into_json")]
} match serde_json::to_string(&validate_error) {
Ok(json) => (StatusCode::BAD_REQUEST, json),
Err(error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to serialize validation error into JSON ({validate_error}): {error}"),
),
}
#[cfg(not(feature = "into_json"))]
(StatusCode::BAD_REQUEST, validate_error.to_string())
}.into_response(),
ValidRejection::Inner(json_error) => json_error.into_response(), ValidRejection::Inner(json_error) => json_error.into_response(),
} }
} }

4
tarpaulin.toml Normal file
View File

@@ -0,0 +1,4 @@
[feature_default]
[feature_into_json]
features = "into_json"

View File

@@ -77,6 +77,11 @@ async fn main() -> anyhow::Result<()> {
.send() .send()
.await?; .await?;
assert_eq!(invalid_path_response.status(), StatusCode::BAD_REQUEST); assert_eq!(invalid_path_response.status(), StatusCode::BAD_REQUEST);
#[cfg(feature = "into_json")]
assert!(invalid_path_response
.json::<serde_json::Value>()
.await
.is_ok());
println!("Valid<Path<...>> works."); println!("Valid<Path<...>> works.");
// Valid<Query<...>> // Valid<Query<...>>
@@ -101,6 +106,11 @@ async fn main() -> anyhow::Result<()> {
.send() .send()
.await?; .await?;
assert_eq!(invalid_query_response.status(), StatusCode::BAD_REQUEST); assert_eq!(invalid_query_response.status(), StatusCode::BAD_REQUEST);
#[cfg(feature = "into_json")]
assert!(invalid_query_response
.json::<serde_json::Value>()
.await
.is_ok());
println!("Valid<Query<...>> works."); println!("Valid<Query<...>> works.");
// Valid<Form<...>> // Valid<Form<...>>
@@ -128,6 +138,11 @@ async fn main() -> anyhow::Result<()> {
.send() .send()
.await?; .await?;
assert_eq!(invalid_form_response.status(), StatusCode::BAD_REQUEST); assert_eq!(invalid_form_response.status(), StatusCode::BAD_REQUEST);
#[cfg(feature = "into_json")]
assert!(invalid_form_response
.json::<serde_json::Value>()
.await
.is_ok());
println!("Valid<Form<...>> works."); println!("Valid<Form<...>> works.");
// Valid<Json<...>> // Valid<Json<...>>
@@ -155,6 +170,11 @@ async fn main() -> anyhow::Result<()> {
.send() .send()
.await?; .await?;
assert_eq!(invalid_json_response.status(), StatusCode::BAD_REQUEST); assert_eq!(invalid_json_response.status(), StatusCode::BAD_REQUEST);
#[cfg(feature = "into_json")]
assert!(invalid_json_response
.json::<serde_json::Value>()
.await
.is_ok());
println!("Valid<Json<...>> works."); println!("Valid<Json<...>> works.");
drop(server_guard); drop(server_guard);

View File

@@ -123,6 +123,11 @@ async fn main() -> anyhow::Result<()> {
.send() .send()
.await?; .await?;
assert_eq!(invalid_my_data_response.status(), StatusCode::BAD_REQUEST); assert_eq!(invalid_my_data_response.status(), StatusCode::BAD_REQUEST);
#[cfg(feature = "into_json")]
assert!(invalid_my_data_response
.json::<serde_json::Value>()
.await
.is_ok());
println!("Valid<MyData> works."); println!("Valid<MyData> works.");
drop(server_guard); drop(server_guard);