|
| 1 | +# axum-validated-extractors |
| 2 | + |
| 3 | +[](https://crates.io/crates/axum-validated-extractors) |
| 4 | +[](https://docs.rs/axum-validated-extractors) |
| 5 | +[](https://opensource.org/licenses/MIT) |
| 6 | +[](https://github.com/truehazker/axum-validated-extractors/actions/workflows/ci.yml) |
| 7 | +[](https://www.rust-lang.org) |
| 8 | +[](https://deps.rs/repo/github/truehazker/axum-validated-extractors) |
| 9 | + |
| 10 | +A collection of validated extractors for Axum that automatically validate the extracted data using the `validator` crate. This library provides type-safe, automatic validation for your Axum web applications, making it easier to handle and validate incoming requests. |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- `ValidatedForm`: Validates form data (URL-encoded or multipart) |
| 15 | +- `ValidatedJson`: Validates JSON data |
| 16 | +- `ValidatedQuery`: Validates query parameters |
| 17 | +- Automatic validation using the `validator` crate |
| 18 | +- Type-safe error handling |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +Add this to your `Cargo.toml`: |
| 23 | + |
| 24 | +```toml |
| 25 | +[dependencies] |
| 26 | +axum-validated-extractors = "0.1.0" |
| 27 | +validator = { version = "0.16", features = ["derive"] } |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Basic Example |
| 33 | + |
| 34 | +```rust |
| 35 | +use axum::{ |
| 36 | + routing::post, |
| 37 | + Router, |
| 38 | +}; |
| 39 | +use axum_validated_extractors::{ValidatedJson, ValidatedForm, ValidatedQuery}; |
| 40 | +use serde::Deserialize; |
| 41 | +use validator::Validate; |
| 42 | + |
| 43 | +#[derive(Debug, Deserialize, Validate)] |
| 44 | +struct CreateUser { |
| 45 | + #[validate(length(min = 3))] |
| 46 | + username: String, |
| 47 | + #[validate(email)] |
| 48 | + email: String, |
| 49 | +} |
| 50 | + |
| 51 | +async fn create_user( |
| 52 | + ValidatedJson(user): ValidatedJson<CreateUser>, |
| 53 | +) { |
| 54 | + // user is guaranteed to be valid |
| 55 | + println!("Creating user: {:?}", user); |
| 56 | +} |
| 57 | + |
| 58 | +let app: Router<()> = Router::new() |
| 59 | + .route("/users", post(create_user)); |
| 60 | +``` |
| 61 | + |
| 62 | +### Form Data |
| 63 | + |
| 64 | +```rust |
| 65 | +use axum::{ |
| 66 | + routing::post, |
| 67 | + Router, |
| 68 | +}; |
| 69 | +use axum_validated_extractors::ValidatedForm; |
| 70 | +use serde::Deserialize; |
| 71 | +use validator::Validate; |
| 72 | + |
| 73 | +#[derive(Debug, Deserialize, Validate)] |
| 74 | +struct LoginForm { |
| 75 | + #[validate(length(min = 3))] |
| 76 | + username: String, |
| 77 | + #[validate(length(min = 8))] |
| 78 | + password: String, |
| 79 | +} |
| 80 | + |
| 81 | +async fn login( |
| 82 | + ValidatedForm(form): ValidatedForm<LoginForm>, |
| 83 | +) { |
| 84 | + // form is guaranteed to be valid |
| 85 | + println!("Logging in user: {:?}", form); |
| 86 | +} |
| 87 | + |
| 88 | +let app: Router<()> = Router::new() |
| 89 | + .route("/login", post(login)); |
| 90 | +``` |
| 91 | + |
| 92 | +### Query Parameters |
| 93 | + |
| 94 | +```rust |
| 95 | +use axum::{ |
| 96 | + routing::get, |
| 97 | + Router, |
| 98 | +}; |
| 99 | +use axum_validated_extractors::ValidatedQuery; |
| 100 | +use serde::Deserialize; |
| 101 | +use validator::Validate; |
| 102 | + |
| 103 | +#[derive(Debug, Deserialize, Validate)] |
| 104 | +struct SearchParams { |
| 105 | + #[validate(length(min = 2))] |
| 106 | + query: String, |
| 107 | + #[validate(range(min = 1, max = 100))] |
| 108 | + page: Option<u32>, |
| 109 | +} |
| 110 | + |
| 111 | +async fn search( |
| 112 | + ValidatedQuery(params): ValidatedQuery<SearchParams>, |
| 113 | +) { |
| 114 | + // params is guaranteed to be valid |
| 115 | + println!("Searching with params: {:?}", params); |
| 116 | +} |
| 117 | + |
| 118 | +let app: Router<()> = Router::new() |
| 119 | + .route("/search", get(search)); |
| 120 | +``` |
| 121 | + |
| 122 | +## Validation Rules |
| 123 | + |
| 124 | +The validation rules are provided by the `validator` crate. Here are some common validation rules: |
| 125 | + |
| 126 | +```rust |
| 127 | +#[derive(Debug, Deserialize, Validate)] |
| 128 | +struct User { |
| 129 | + #[validate(length(min = 3, max = 20))] |
| 130 | + username: String, |
| 131 | + |
| 132 | + #[validate(email)] |
| 133 | + email: String, |
| 134 | + |
| 135 | + #[validate(range(min = 18))] |
| 136 | + age: u32, |
| 137 | + |
| 138 | + #[validate(url)] |
| 139 | + website: Option<String>, |
| 140 | + |
| 141 | + #[validate(regex = "^[a-zA-Z0-9_]+$")] |
| 142 | + nickname: String, |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +See the [validator documentation](https://docs.rs/validator) for more validation rules. |
| 147 | + |
| 148 | +## Error Handling |
| 149 | + |
| 150 | +The extractors return a `ValidationError` when validation fails. This error can be converted into a response: |
| 151 | + |
| 152 | +```rust |
| 153 | +use axum::{ |
| 154 | + routing::post, |
| 155 | + Router, |
| 156 | + response::IntoResponse, |
| 157 | +}; |
| 158 | +use axum_validated_extractors::{ValidatedJson, ValidationError}; |
| 159 | +use serde::Deserialize; |
| 160 | +use validator::Validate; |
| 161 | + |
| 162 | +#[derive(Debug, Deserialize, Validate)] |
| 163 | +struct CreateUser { |
| 164 | + #[validate(length(min = 3))] |
| 165 | + username: String, |
| 166 | + #[validate(email)] |
| 167 | + email: String, |
| 168 | +} |
| 169 | + |
| 170 | +async fn create_user( |
| 171 | + ValidatedJson(user): ValidatedJson<CreateUser>, |
| 172 | +) -> impl IntoResponse { |
| 173 | + // If validation fails, a 400 Bad Request response is returned |
| 174 | + // with a detailed error message |
| 175 | + user |
| 176 | +} |
| 177 | + |
| 178 | +let app: Router<()> = Router::new() |
| 179 | + .route("/users", post(create_user)); |
| 180 | +``` |
| 181 | + |
| 182 | +## Contributing |
| 183 | + |
| 184 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 185 | + |
| 186 | +## License |
| 187 | + |
| 188 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments