• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gengteng / axum-valid / 6456231901

09 Oct 2023 11:53AM UTC coverage: 89.07% (+17.9%) from 71.154%
6456231901

push

github

gengteng
refactor Rejections

25 of 25 new or added lines in 3 files covered. (100.0%)

766 of 860 relevant lines covered (89.07%)

13.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/query.rs
1
//! # Support for `Query<T>`
2
//!
3
//! ## Feature
4
//!
5
//! Enable the `query` feature (enabled by default) to use `Valid<Query<T>>`.
6
//!
7
//! ## Usage
8
//!
9
//! 1. Implement `Deserialize` and `Validate` for your data type `T`.
10
//! 2. In your handler function, use `Valid<Query<T>>` as some parameter's type.
11
//!
12
//! ## Example
13
//!
14
//! ```no_run
15
//! #[cfg(feature = "validator")]
16
//! mod validator_example {
17
//!     use axum::extract::Query;
18
//!     use axum::routing::post;
19
//!     use axum::Router;
20
//!     use axum_valid::Valid;
21
//!     use serde::Deserialize;
22
//!     use validator::Validate;
23
//!
24
//!     pub fn router() -> Router {
25
//!         Router::new().route("/query", post(handler))
26
//!     }
27
//!
28
//!     async fn handler(Valid(Query(parameter)): Valid<Query<Parameter>>) {
29
//!         assert!(parameter.validate().is_ok());
30
//!         // Support automatic dereferencing
31
//!         println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
32
//!     }
33
//!
34
//!     #[derive(Validate, Deserialize)]
35
//!     pub struct Parameter {
36
//!         #[validate(range(min = 5, max = 10))]
37
//!         pub v0: i32,
38
//!         #[validate(length(min = 1, max = 10))]
39
//!         pub v1: String,
40
//!     }
41
//! }
42
//!
43
//! #[cfg(feature = "garde")]
44
//! mod garde_example {
45
//!     use axum::routing::post;
46
//!     use axum::extract::Query;
47
//!     use axum::Router;
48
//!     use axum_valid::Garde;
49
//!     use serde::Deserialize;
50
//!     use garde::Validate;
51
//!
52
//!     pub fn router() -> Router {
53
//!         Router::new().route("/query", post(handler))
54
//!     }
55
//!
56
//!     async fn handler(Garde(Query(parameter)): Garde<Query<Parameter>>) {
57
//!         assert!(parameter.validate(&()).is_ok());
58
//!         // Support automatic dereferencing
59
//!         println!("v0 = {}, v1 = {}", parameter.v0, parameter.v1);
60
//!     }
61
//!
62
//!     #[derive(Validate, Deserialize)]
63
//!     pub struct Parameter {
64
//!         #[garde(range(min = 5, max = 10))]
65
//!         pub v0: i32,
66
//!         #[garde(length(min = 1, max = 10))]
67
//!         pub v1: String,
68
//!     }
69
//! }
70
//!
71
//! # #[tokio::main]
72
//! # async fn main() -> anyhow::Result<()> {
73
//! #     use axum::Router;
74
//! #     let router = Router::new();
75
//! #     #[cfg(feature = "validator")]
76
//! #     let router = router.nest("/validator", validator_example::router());
77
//! #     #[cfg(feature = "garde")]
78
//! #     let router = router.nest("/garde", garde_example::router());
79
//! #     axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
80
//! #         .serve(router.into_make_service())
81
//! #         .await?;
82
//! #     Ok(())
83
//! # }
84
//! ```
85

86
use crate::HasValidate;
87
#[cfg(feature = "validator")]
88
use crate::HasValidateArgs;
89
use axum::extract::Query;
90
#[cfg(feature = "validator")]
91
use validator::ValidateArgs;
92

93
impl<T> HasValidate for Query<T> {
94
    type Validate = T;
95
    fn get_validate(&self) -> &T {
7✔
96
        &self.0
×
97
    }
98
}
99

100
#[cfg(feature = "validator")]
101
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for Query<T> {
102
    type ValidateArgs = T;
103
    fn get_validate_args(&self) -> &Self::ValidateArgs {
4✔
104
        &self.0
×
105
    }
106
}
107

108
#[cfg(test)]
109
mod tests {
110
    use crate::tests::{ValidTest, ValidTestParameter};
111
    use axum::extract::Query;
112
    use axum::http::StatusCode;
113
    use reqwest::RequestBuilder;
114
    use serde::Serialize;
115

116
    impl<T: ValidTestParameter + Serialize> ValidTest for Query<T> {
117
        const ERROR_STATUS_CODE: StatusCode = StatusCode::BAD_REQUEST;
118

119
        fn set_valid_request(builder: RequestBuilder) -> RequestBuilder {
120
            builder.query(&T::valid())
121
        }
122

123
        fn set_error_request(builder: RequestBuilder) -> RequestBuilder {
124
            builder.query(T::error())
125
        }
126

127
        fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {
128
            builder.query(&T::invalid())
129
        }
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc