[](https://crates.io/crates/vld-rocket)
[](https://docs.rs/vld-rocket)
[](https://github.com/s00d/vld/blob/main/LICENSE)
[](https://github.com/s00d/vld)
[](https://github.com/s00d/vld/issues)
[](https://github.com/s00d/vld/stargazers)
# vld-rocket
[Rocket](https://rocket.rs/) integration for the **vld** validation library.
## Features
| `VldJson<T>` | JSON body | Validates JSON request body |
| `VldQuery<T>` | Query string | Validates URL query parameters |
| `VldForm<T>` | Form body | Validates `application/x-www-form-urlencoded` |
All extractors return `422 Unprocessable Entity` with a JSON error body on validation failure.
## Installation
```toml
[dependencies]
vld-rocket = "0.1"
vld = "0.1"
rocket = { version = "0.5", features = ["json"] }
serde_json = "1"
```
## Quick Start
```rust
use vld_rocket::prelude::*;
vld::schema! {
#[derive(Debug, Clone)]
pub struct CreateUser {
pub name: String => vld::string().min(2).max(50),
pub email: String => vld::string().email(),
}
}
#[rocket::post("/users", data = "<user>")]
fn create_user(user: VldJson<CreateUser>) -> rocket::serde::json::Json<serde_json::Value> {
rocket::serde::json::Json(serde_json::json!({
"name": user.name,
"email": user.email,
}))
}
#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
rocket::build()
.mount("/", rocket::routes![create_user])
.register("/", rocket::catchers![
vld_rocket::vld_422_catcher,
vld_rocket::vld_400_catcher,
])
.launch()
.await?;
Ok(())
}
```
## Error Catchers
Register the built-in catchers to get JSON error responses:
```rust,ignore
.register("/", rocket::catchers![
vld_rocket::vld_422_catcher, // validation errors
vld_rocket::vld_400_catcher, // JSON parse errors
])
```
## Running Examples
```bash
cargo run -p vld-rocket --example rocket_basic
```
### Example Requests
```bash
# Create user (valid)
curl -X POST http://localhost:8000/users \
-H 'Content-Type: application/json' \
-d '{"name":"Alice","email":"alice@example.com","age":30}'
# Create user (invalid — triggers 422)
curl -X POST http://localhost:8000/users \
-H 'Content-Type: application/json' \
-d '{"name":"A","email":"bad","age":-1}'
# Search (query params)
curl "http://localhost:8000/search?q=hello&page=1&limit=10"
```
## License
MIT