Skip to main content

Crate vld_rocket

Crate vld_rocket 

Source
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

ExtractorSourceRocket equivalent
VldJson<T>JSON bodyrocket::serde::json::Json<T>
VldQuery<T>Query stringquery params
VldPath<T>Path segments<param> / <param..> segments
VldForm<T>Form bodyrocket::form::Form<T>
VldHeaders<T>HTTP headersmanual extraction
VldCookie<T>Cookie valuesCookieJar

§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 Request that returns JSON.
vld_413_catcher
Catcher for 413 Payload Too Large that returns {"error": "Payload too large"}.
vld_415_catcher
Catcher for 415 Unsupported Media Type that returns JSON.
vld_422_catcher
Catcher for 422 Unprocessable Entity that returns JSON from the validation error stored by vld extractors.
vld_catchers
All vld catchers: 400, 413, 415 and 422.