vld-http-common 0.7.1

DEPRECATED: moved to vld-web 0.8 (vld_web::common). Shared HTTP helpers for vld web-framework integrations (internal)
Documentation

vld-http-common

Deprecated. vld-http-common is not developed any more. Since vld 0.8 it is part of vld-web: vld_web::common.

vld-web = "0.8"

Migration guide: https://s00d.github.io/vld/guides/migration#from-07-to-08. This version (0.7.1) is 0.7.0 unchanged, with this notice.

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