Expand description
Deprecated: moved to vld-web 0.8 (vld_web::rocket); see the
migration guide.
§vld-rocket — Rocket integration for vld
Validation extractors for Rocket. Validates request
data against vld schemas and returns 422 Unprocessable Entity with
structured JSON errors on failure (malformed JSON/form bodies included),
and 413 Payload Too Large when a body exceeds the limit.
VldJson requires a JSON Content-Type (application/json or
application/*+json) and VldForm application/x-www-form-urlencoded;
anything else is 415 Unsupported Media Type. See the
error body format.
Guide: https://s00d.github.io/vld/integrations/web/rocket (shared rules for all adapters: https://s00d.github.io/vld/integrations/web/overview).
§Example
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(),
}
}
vld::schema! {
#[derive(Debug, Clone)]
pub struct UserPath {
pub id: i64 => vld::number().int().min(1),
}
}
#[rocket::post("/users", data = "<user>")]
fn create_user(user: VldJson<CreateUser>) -> String {
format!("created {}", user.name)
}
// Rocket needs every `<param>` as an argument too; `VldPath` validates them.
#[rocket::get("/users/<id>")]
fn get_user(id: &str, p: VldPath<UserPath>) -> String {
let _ = id;
format!("user #{}", p.id)
}
fn app() -> rocket::Rocket<rocket::Build> {
rocket::build()
.mount("/api", rocket::routes![create_user, get_user])
.register("/", vld_catchers())
}§Extractors
| Extractor | Source | Rocket equivalent |
|---|---|---|
VldJson<T> | JSON body | rocket::serde::json::Json<T> |
VldQuery<T> | Query string | query params |
VldPath<T> | Path segments | <param> / <param..> segments |
VldForm<T> | Form body | rocket::form::Form<T> |
VldHeaders<T> | HTTP headers | manual extraction |
VldCookie<T> | Cookie values | CookieJar |
§Body limits
VldJson reads up to Rocket’s json limit and VldForm up to the
form limit, configured via limits in Rocket.toml (or
ROCKET_LIMITS). While a limit is left at Rocket’s built-in default
(1 MiB / 32 KiB) DEFAULT_BODY_LIMIT (2 MiB) applies instead, like in
every other vld adapter — so setting exactly the built-in value counts
as unset.
§Error catchers
Register vld_catchers() (as in the example above) to get JSON error
responses instead of Rocket’s default HTML pages. A failed guard only sets
the status; the catcher answers with the error body the guard stored.
Modules§
- prelude
- Prelude — import everything you need.
Structs§
- VldCookie
- Validated cookie extractor for Rocket.
- VldForm
- Validated form body extractor.
- VldHeaders
- Validated HTTP headers extractor for Rocket.
- VldJson
- Validated JSON body extractor.
- VldPath
- Validated path parameter extractor for Rocket.
- VldQuery
- Validated query string extractor.
Functions§
- vld_
400_ catcher - Catcher for
400 Bad Requestthat returns JSON. - vld_
413_ catcher - Catcher for
413 Payload Too Largethat returns{"error": "Payload too large"}. - vld_
415_ catcher - Catcher for
415 Unsupported Media Typethat returns JSON. - vld_
422_ catcher - Catcher for
422 Unprocessable Entitythat returns JSON from the validation error stored by vld extractors. - vld_
catchers - All vld catchers:
400,413,415and422.