Web frameworks
ntex 3 and ntex 4 extractors that validate JSON bodies, query strings, path parameters, forms, headers and cookies with vld schemas.

Drop-in replacements for ntex::web::types::{Json, Query, Path, Form}, plus header and cookie extractors. Shared behaviour (status codes, error body, coercion, header/cookie rules) is in the web adapters overview.

Install

Cargo.toml
[dependencies]
vld = "0.8"
vld-web = { version = "0.8", features = ["ntex-4"] }   # module vld_web::ntex4
ntex = { version = "4", features = ["tokio"] }
FeatureModuleNotes
ntex-4vld_web::ntex4ntex 4, needs Rust 1.97
ntex-3vld_web::ntex3ntex 3, needs Rust 1.88
ntex-compress—decode Content-Encoding: gzip / deflate / br request bodies (turns on ntex's compress for every enabled major)

The two modules have the same API and can be enabled together. Each re-exports its ntex crate as ntex (vld_web::ntex4::ntex); use the module that matches your own ntex dependency, or the extractors will not implement your version's FromRequest. The examples below use ntex4; for ntex 3 write vld_web::ntex3.

Example

main.rs
use ntex::web::{self, App, HttpResponse, HttpServer};
use vld_web::ntex4::{VldJson, VldPath, VldQuery};

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

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

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

async fn create_user(user: VldJson<CreateUser>) -> HttpResponse {
    HttpResponse::Created().body(format!("created {}", user.name))
}

async fn get_user(path: VldPath<UserPath>) -> HttpResponse {
    HttpResponse::Ok().body(format!("user #{}", path.id))
}

async fn search(q: VldQuery<Search>) -> HttpResponse {
    HttpResponse::Ok().body(format!("search: {}", q.q))
}

#[ntex::main]
async fn main() -> std::io::Result<()> {
    let app = || {
        App::new()
            .route("/users", web::post().to(create_user))
            .route("/users/{id}", web::get().to(get_user))
            .route("/search", web::get().to(search))
    };

    // ntex 4
    HttpServer::new(async move |_| app())
        .bind("127.0.0.1:8080", ntex::SharedCfg::default())?
        .run()
        .await
    // ntex 3: HttpServer::new(async move || app()).bind("127.0.0.1:8080")?.run().await
}

The full example is crates/vld-web/examples/ntex_basic.rs (cargo run -p vld-web --features ntex-4 --example ntex_basic).

API

ItemDescription
VldJson<T>JSON body (requires a JSON Content-Type)
VldQuery<T>Query string
VldPath<T>Path parameters (decoded by ntex's router)
VldForm<T>application/x-www-form-urlencoded body
VldHeaders<T>Request headers (X-Request-Id → x_request_id)
VldCookie<T>Cookies from all Cookie headers
VldNtexErrorError of every extractor (WebResponseError): status(), body(), error()
VldBodyLimitBody size limit for VldJson / VldForm, registered as app state
ntexRe-export of the module's ntex crate
preludeEverything above plus vld::prelude::*

Body limit

VldJson and VldForm read the payload themselves up to 2 MiB; ntex's JsonConfig / PayloadConfig do not apply. Register VldBodyLimit as app state:

use vld_web::ntex4::VldBodyLimit;

// ntex 4 (4.7+)
App::new().with_config(web::WebAppConfig::new().set_value(VldBodyLimit(8 * 1024 * 1024)));
// ntex 3
App::new().state(VldBodyLimit(8 * 1024 * 1024));

Gotchas

  • Without the ntex-compress feature compressed request bodies are not decoded, so a gzip body fails as malformed JSON (422). With it, the limit applies to the decoded size.