Skip to main content

Module actix

Module actix 

Source
Expand description

§vld_web::actix — Actix-web integration for the vld validation library

Provides extractors that validate request data using vld schemas:

ExtractorReplacesSource
VldJson<T>actix_web::web::Json<T>JSON request body
VldQuery<T>actix_web::web::Query<T>URL query parameters
VldPath<T>actix_web::web::Path<T>URL path parameters
VldForm<T>actix_web::web::Form<T>URL-encoded form body
VldHeaders<T>manual header extractionHTTP headers
VldCookie<T>manual cookie parsingCookie values

All extractors return 422 Unprocessable Entity on validation failure or malformed input, and 413 Payload Too Large when the 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/actix (shared rules for all adapters: https://s00d.github.io/vld/integrations/web/overview).

§Body limits

VldJson and VldForm read the body themselves (compressed bodies are decoded; the limit applies to the decoded size) up to DEFAULT_BODY_LIMIT (2 MiB). Register VldBodyLimit with app_data to change it — actix’s JsonConfig / PayloadConfig do not apply:

use actix_web::App;

let app = App::new().app_data(vld_web::actix::VldBodyLimit(8 * 1024 * 1024));

§Example

use actix_web::{web, App, HttpResponse};
use vld_web::actix::{VldHeaders, VldJson, VldPath};

vld::schema! {
    #[derive(Debug)]
    pub struct PathParams {
        pub id: i64 => vld::number().int().min(1),
    }
}

vld::schema! {
    #[derive(Debug)]
    pub struct Auth {
        pub authorization: String => vld::string().min(1),
    }
}

vld::schema! {
    #[derive(Debug)]
    pub struct Body {
        pub name: String => vld::string().min(2),
    }
}

async fn handler(
    path: VldPath<PathParams>,
    headers: VldHeaders<Auth>,
    body: VldJson<Body>,
) -> HttpResponse {
    HttpResponse::Ok().body(format!(
        "id={} auth={} name={}",
        path.id, headers.authorization, body.name,
    ))
}

let app = App::new().route("/users/{id}", web::post().to(handler));

Re-exports§

pub use crate::common::VldBodyLimit;

Modules§

prelude
Prelude — import everything you need.

Structs§

VldActixError
Error type returned when validation or body reading fails.
VldCookie
Actix-web extractor that validates cookie values from the Cookie headers (all of them; the first occurrence of a name wins).
VldForm
Actix-web extractor that validates URL-encoded form bodies (application/x-www-form-urlencoded).
VldHeaders
Actix-web extractor that validates HTTP headers.
VldJson
Actix-web extractor that validates JSON request bodies.
VldPath
Actix-web extractor that validates URL path parameters.
VldQuery
Actix-web extractor that validates URL query parameters.