Web frameworks
vld extractors in loco-rs controllers - loco handlers are axum handlers, so vld_web::axum works there unchanged.

loco controllers are axum handlers on a Router<AppContext>, so the vld_web::axum extractors work in them as they are. There is no loco feature: loco's Validatable trait is built on the validator crate, and loco-rs itself (tera, sea-orm, Rust 1.94) is too heavy to pull into vld-web for a few lines of glue.

Install

Cargo.toml
[dependencies]
vld = "0.8"
vld-web = { version = "0.8", features = ["axum"] }
loco-rs = "1"

loco 1.x builds on axum 0.8, the version vld_web::axum targets.

Example

src/controllers/articles.rs
use loco_rs::prelude::*;
use vld_web::axum::{VldJson, VldPath};

vld::schema! {
    #[derive(Debug)]
    pub struct Params {
        pub title: String => vld::string().min(3).max(120),
        pub content: String => vld::string().max(10_000),
    }
}

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

/// Invalid body: vld's 422 with every issue, before the handler runs.
pub async fn add(State(ctx): State<AppContext>, VldJson(params): VldJson<Params>) -> Result<Response> {
    // ... insert with ctx.db
    format::json(serde_json::json!({ "title": params.title }))
}

pub async fn update(
    State(ctx): State<AppContext>,
    VldPath(path): VldPath<Id>,
    VldJson(params): VldJson<Params>,
) -> Result<Response> {
    // ... update article path.id
    format::empty()
}

pub fn routes() -> Routes {
    Routes::new()
        .prefix("api/articles/")
        .add("/", post(add))
        .add("{id}", put(update))
}

loco's error format

VldJson rejects with the vld error body (422). To answer in loco's ErrorDetail shape instead, take Result<VldJson<T>, VldJsonRejection> and convert the rejection to a loco_rs::Error:

use loco_rs::controller::ErrorDetail;
use vld_web::axum::VldJsonRejection;

fn bad_request(r: VldJsonRejection) -> loco_rs::Error {
    let mut detail = ErrorDetail::new("Bad Request", "invalid request");
    detail.errors = Some(r.body()["issues"].clone()); // [{path, message, code}]
    loco_rs::Error::CustomError(axum::http::StatusCode::BAD_REQUEST, detail)
}

pub async fn add(
    State(ctx): State<AppContext>,
    body: std::result::Result<VldJson<Params>, VldJsonRejection>,
) -> Result<Response> {
    let VldJson(params) = body.map_err(bad_request)?;
    format::json(serde_json::json!({ "title": params.title }))
}

Gotchas

  • Body extractors (VldJson, VldForm) go last in the handler's arguments, as in any axum handler.
  • loco's JsonValidate / JsonValidateWithMessage need validator::Validate; VldJson replaces them, it does not plug into them.
  • crates/vld-web/tests/it/axum/story_loco.rs runs this controller shape (state, prefix, both error formats) through a real axum router.