Skip to content

Commit fc289ff

Browse files
committed
feat: axum method add valid
1 parent 21576b2 commit fc289ff

5 files changed

Lines changed: 63 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ publish = false
1010
[dependencies]
1111
serde = { version = "1", features = ["derive"] }
1212
serde_yaml = "0.9"
13-
axum = "0.7"
13+
axum = "0.7"
14+
anyhow = "1.0"

src/model/parse.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::request::validator::ValidateRequest;
1919
use serde::{Deserialize, Serialize};
2020
use std::collections::HashMap;
2121
use std::hash::Hash;
22+
use std::str::FromStr;
2223

2324
#[derive(Debug, Serialize, Deserialize)]
2425
pub struct OpenAPI {
@@ -39,7 +40,7 @@ impl OpenAPI {
3940
serde_yaml::from_str(contents)
4041
}
4142

42-
pub fn validator(&self, _: impl ValidateRequest) -> Result<(), String> {
43+
pub fn validator(&self, valid: impl ValidateRequest) -> Result<(), String> {
4344
if self.openapi.is_empty() {
4445
return Err("OpenAPI version is required".to_string());
4546
}
@@ -49,6 +50,10 @@ impl OpenAPI {
4950
if self.info.version.is_empty() {
5051
return Err("Version is required".to_string());
5152
}
53+
if self.paths.is_empty() {
54+
return Err("Paths are required".to_string());
55+
}
56+
valid.path(self).unwrap();
5257
Ok(())
5358
}
5459
}
@@ -214,6 +219,25 @@ pub enum Method {
214219
Trace,
215220
}
216221

222+
impl FromStr for Method {
223+
type Err = String;
224+
225+
fn from_str(s: &str) -> Result<Self, Self::Err> {
226+
match s.to_uppercase().as_str() {
227+
"GET" => Ok(Method::Get),
228+
"HEAD" => Ok(Method::Head),
229+
"POST" => Ok(Method::Post),
230+
"PUT" => Ok(Method::Put),
231+
"DELETE" => Ok(Method::Delete),
232+
"CONNECT" => Ok(Method::Connect),
233+
"PATCH" => Ok(Method::Patch),
234+
"OPTIONS" => Ok(Method::Options),
235+
"TRACE" => Ok(Method::Trace),
236+
_ => Err(format!("Invalid method: {}", s)),
237+
}
238+
}
239+
}
240+
217241
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
218242
#[serde(rename_all(deserialize = "lowercase"))]
219243
pub enum In {

src/request/axum.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
17+
use crate::model::parse::{Method, OpenAPI};
1818
use crate::request::validator::ValidateRequest;
19+
use anyhow::{Context, Result};
1920
use axum::body::{Body, Bytes};
2021
use axum::http::Request;
21-
use std::io::Error;
22+
use std::str::FromStr;
2223

2324
#[allow(dead_code)]
2425
pub struct RequestData {
@@ -27,19 +28,34 @@ pub struct RequestData {
2728
}
2829

2930
impl ValidateRequest for RequestData {
30-
fn header(&self) -> Result<(), Error> {
31+
fn header(&self, _: &OpenAPI) -> Result<()> {
32+
Ok(())
33+
}
34+
35+
fn method(&self, open_api: &OpenAPI) -> Result<()> {
36+
let path = open_api
37+
.paths
38+
.get(self.inner.uri().path())
39+
.context("Path not found")?;
40+
41+
let method =
42+
Method::from_str(self.inner.method().as_str()).map_err(|e| anyhow::anyhow!(e))?;
43+
44+
if path.get(&method).is_none() {
45+
return Err(anyhow::anyhow!("Path is empty"));
46+
}
3147
Ok(())
3248
}
3349

34-
fn query(&self) -> Result<(), Error> {
50+
fn query(&self, _: &OpenAPI) -> Result<()> {
3551
Ok(())
3652
}
3753

38-
fn path(&self) -> Result<(), Error> {
54+
fn path(&self, _: &OpenAPI) -> Result<()> {
3955
Ok(())
4056
}
4157

42-
fn body(&self) -> Result<(), Error> {
58+
fn body(&self, _: &OpenAPI) -> Result<()> {
4359
Ok(())
4460
}
4561
}

src/request/validator.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
18-
use std::io::Error;
17+
use crate::model::parse::OpenAPI;
18+
use anyhow::Result;
1919

2020
pub trait ValidateRequest {
21-
fn header(&self) -> Result<(), Error>;
22-
fn query(&self) -> Result<(), Error>;
23-
fn path(&self) -> Result<(), Error>;
24-
fn body(&self) -> Result<(), Error>;
21+
fn header(&self, _: &OpenAPI) -> Result<()>;
22+
fn method(&self, _: &OpenAPI) -> Result<()>;
23+
fn query(&self, _: &OpenAPI) -> Result<()>;
24+
fn path(&self, _: &OpenAPI) -> Result<()>;
25+
fn body(&self, _: &OpenAPI) -> Result<()>;
2526
}

0 commit comments

Comments
 (0)