This commit is contained in:
gengteng
2023-06-06 12:10:45 +08:00
parent 43611707e2
commit 0e5d7c6edb
3 changed files with 41 additions and 7 deletions

View File

@@ -1,2 +1,33 @@
# axum-valid
The Valid crate provides a `Valid` type that can be used in combination with `Json`, `Path`, `Query`, and `Form` types to validate the entities that implement the `Validate` trait.
## Usage
```rust
use validator::Validate;
use serde::Deserialize;
#[derive(Debug, Validate, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Pager {
#[validate(range(min = 1, max = 50))]
page_size: usize,
#[validate(range(min = 1))]
page_no: usize,
}
pub async fn get_page_by_query(
Valid(Query(pager)): Valid<Query<Pager>>,
) {
assert!((1..=50).contains(pager.page_size));
assert!((1..).contains(pager.page_no));
}
pub async fn get_page_by_json(
Valid(Json(pager)): Valid<Json<Pager>>,
) {
assert!((1..=50).contains(pager.page_size));
assert!((1..).contains(pager.page_no));
}
```