Web frameworks
Rocket request guards that validate JSON bodies, query strings, path segments, forms, headers and cookies with vld schemas, plus JSON error catchers.

Data and request guards for Rocket 0.5, and catchers that turn their failures into the shared JSON error body. Shared behaviour (status codes, error body, coercion, header/cookie rules) is in the web adapters overview.

Install

Cargo.toml
[dependencies]
vld = "0.5"
vld-rocket = "0.5"
rocket = { version = "0.5", features = ["json"] }

No features. MSRV 1.85 (see MSRV).

Example

main.rs
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)
}

#[rocket::launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/api/v1", rocket::routes![create_user, get_user])
        .register("/", vld_catchers())
}

The full example is crates/vld-rocket/examples/rocket_basic.rs (cargo run -p vld-rocket --example rocket_basic).

API

ItemDescription
VldJson<T>Data guard: JSON body (requires a JSON Content-Type)
VldForm<T>Data guard: application/x-www-form-urlencoded body
VldQuery<T>Request guard: query string
VldPath<T>Request guard: the route's dynamic segments (<id>; a trailing <rest..> gets the remaining segments joined with /)
VldHeaders<T>Request guard: headers (X-Request-Id → x_request_id)
VldCookie<T>Request guard: cookies from all Cookie headers
vld_catchers()All four catchers below, for .register("/", ...)
vld_400_catcher, vld_413_catcher, vld_415_catcher, vld_422_catcherJSON catchers for the individual statuses
preludeEverything above plus vld::prelude::*

Error catchers

A failed guard only sets the status; Rocket renders the response through a catcher. Without vld_catchers() clients get Rocket's default HTML page. The vld catchers answer with the error body the guard stored for that status; when another guard failed with the same status (Rocket's own Json, for example) they answer {"error": "<reason>"}.

Body limits

VldJson reads up to Rocket's json limit and VldForm up to the form limit, configured in Rocket.toml or ROCKET_LIMITS:

Rocket.toml
[default.limits]
json = "8 MiB"
form = "256 KiB"

While a limit is left at Rocket's built-in default (1 MiB for json, 32 KiB for form), the shared 2 MiB applies instead, like in every other vld adapter.

Gotchas

  • Setting a limit to exactly Rocket's built-in value counts as "unset" and gives 2 MiB.
  • VldPath works under any mount point (/api/v1/users/<id>), and field names must match the <param> names.
  • VldJson / VldForm errors are 415, 413 or 422 like everywhere else, but only become JSON with the catchers registered.