Skip to main content

Crate vld_tower

Crate vld_tower 

Source
Expand description

§vld-tower — Tower middleware for vld validation

A universal [tower::Layer] that validates incoming HTTP JSON request bodies against a vld schema. Works with any Tower-compatible framework: Axum, Hyper, Tonic, Warp, etc.

On success the validated struct is stored in http::Request::extensions so downstream handlers can retrieve it without re-parsing. The original body bytes are forwarded as-is.

On failure a 422 Unprocessable Entity JSON response is returned immediately — the inner service is never called.

§Quick Start (with Axum)

use vld::prelude::*;
use vld_tower::ValidateJsonLayer;

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

// Apply as a layer — works with any Tower-based router
// let app = Router::new()
//     .route("/users", post(handler))
//     .layer(ValidateJsonLayer::<CreateUser>::new());

Modules§

prelude
Prelude — import everything you need.

Structs§

ValidateJsonLayer
A tower_layer::Layer that validates JSON request bodies with vld.
ValidateJsonService
The middleware Service created by ValidateJsonLayer.

Functions§

try_validated
Try to extract the validated value from request extensions.
validated
Extract the validated value from request extensions.