
vld-warp
Warp integration for the vld validation library.
Features
| Filter |
Source |
Description |
vld_json::<T>() |
JSON body |
Validates JSON request body |
vld_query::<T>() |
Query string |
Validates URL query parameters |
handle_rejection |
— |
Converts vld rejections into JSON responses |
Validation failures are returned as 422 Unprocessable Entity with a JSON error body.
Installation
[dependencies]
vld-warp = "0.1"
vld = "0.1"
warp = "0.3"
serde_json = "1"
Quick Start
use vld_warp::prelude::*;
use warp::Filter;
vld::schema! {
#[derive(Debug, Clone)]
pub struct CreateUser {
pub name: String => vld::string().min(2).max(50),
pub email: String => vld::string().email(),
}
}
#[tokio::main]
async fn main() {
let route = warp::post()
.and(warp::path("users"))
.and(vld_json::<CreateUser>())
.map(|u: CreateUser| {
warp::reply::json(&serde_json::json!({"name": u.name}))
})
.recover(handle_rejection);
warp::serve(route).run(([0, 0, 0, 0], 3030)).await;
}
Recovery Handler
Always add .recover(handle_rejection) to convert vld rejections into structured JSON:
let routes = create.or(search).recover(handle_rejection);
Error response format:
{
"error": "Validation failed",
"issues": [
{ "path": ".name", "message": "String must be at least 2 characters" }
]
}
Running Examples
cargo run -p vld-warp --example warp_basic
Example Requests
curl -X POST http://localhost:3030/users \
-H 'Content-Type: application/json' \
-d '{"name":"Alice","email":"alice@example.com","age":30}'
curl -X POST http://localhost:3030/users \
-H 'Content-Type: application/json' \
-d '{"name":"A","email":"bad","age":-1}'
curl "http://localhost:3030/search?q=hello&page=1&limit=10"
License
MIT