vld-http-common 0.6.1

Shared HTTP helpers for vld web-framework integrations (internal)
Documentation

vld-http-common

crates.io docs.rs

Shared HTTP helpers of the vld web adapters (vld-axum, vld-actix, vld-ntex, vld-poem, vld-rama, vld-rocket, vld-salvo, vld-tower, vld-warp, vld-tonic): value coercion, query / form / header / cookie parsing, content-type checks and the JSON error body. Framework-agnostic — no HTTP framework or http dependency.

Applications normally use it through an adapter; depend on it directly to write an adapter for another framework.

Install

[dependencies]
vld-http-common = "0.5"
Feature Description
conformance Test support: the shared adapter conformance suite

Example

use vld_http_common::{parse_json_body, require_json_content_type, VldHttpError};

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

fn extract(content_type: Option<&str>, body: &[u8]) -> Result<User, VldHttpError> {
    require_json_content_type(content_type)?; // 415
    let value = parse_json_body(body)?;       // 422 on malformed JSON
    Ok(User::parse_value(&value)?)            // 422 on validation errors
}

let err = extract(Some("application/json"), br#"{"name": "A"}"#).unwrap_err();
assert_eq!(err.status(), 422);
// {"error": "Validation failed", "issues": [{"path": ".name", "message": "...", "code": "too_small"}]}
println!("{}", err.body());

Documentation

MSRV: 1.85 (MSRV.md).

License

MIT