Skip to content

Commit 21576b2

Browse files
committed
feat: add validator interface
1 parent 978e15b commit 21576b2

9 files changed

Lines changed: 656 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 548 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
@@ -9,4 +9,5 @@ publish = false
99

1010
[dependencies]
1111
serde = { version = "1", features = ["derive"] }
12-
serde_yaml = "0.9"
12+
serde_yaml = "0.9"
13+
axum = "0.7"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
*/
1717

1818
pub mod model;
19+
pub mod request;

src/model/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
* limitations under the License.
1616
*/
1717

18-
pub mod prase;
18+
pub mod parse;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
use crate::request::validator::ValidateRequest;
1819
use serde::{Deserialize, Serialize};
1920
use std::collections::HashMap;
2021
use std::hash::Hash;
@@ -37,6 +38,19 @@ impl OpenAPI {
3738
pub fn yaml(contents: &str) -> Result<Self, serde_yaml::Error> {
3839
serde_yaml::from_str(contents)
3940
}
41+
42+
pub fn validator(&self, _: impl ValidateRequest) -> Result<(), String> {
43+
if self.openapi.is_empty() {
44+
return Err("OpenAPI version is required".to_string());
45+
}
46+
if self.info.title.is_empty() {
47+
return Err("Title is required".to_string());
48+
}
49+
if self.info.version.is_empty() {
50+
return Err("Version is required".to_string());
51+
}
52+
Ok(())
53+
}
4054
}
4155

4256
#[derive(Debug, Serialize, Deserialize)]

src/request/axum.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use crate::request::validator::ValidateRequest;
19+
use axum::body::{Body, Bytes};
20+
use axum::http::Request;
21+
use std::io::Error;
22+
23+
#[allow(dead_code)]
24+
pub struct RequestData {
25+
inner: Request<Body>,
26+
body: Bytes,
27+
}
28+
29+
impl ValidateRequest for RequestData {
30+
fn header(&self) -> Result<(), Error> {
31+
Ok(())
32+
}
33+
34+
fn query(&self) -> Result<(), Error> {
35+
Ok(())
36+
}
37+
38+
fn path(&self) -> Result<(), Error> {
39+
Ok(())
40+
}
41+
42+
fn body(&self) -> Result<(), Error> {
43+
Ok(())
44+
}
45+
}

src/request/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
pub mod axum;
19+
pub mod validator;

src/request/validator.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use std::io::Error;
19+
20+
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>;
25+
}

tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#[cfg(test)]
1919
mod tests {
20-
use openapi_rs::model::prase::{Format, In, Method, OpenAPI, Type};
20+
use openapi_rs::model::parse::{Format, In, Method, OpenAPI, Type};
2121
use serde_yaml::Value;
2222
use serde_yaml::Value::Sequence;
2323
use std::env;

0 commit comments

Comments
 (0)