vld-web 0.8.0

Web framework integrations for the vld validation library: axum, actix-web, ntex, poem, Rocket, salvo, warp, rama, Tower, tonic
Documentation
use std::future::Future;

use crate::common::{parse_json_body, require_json_content_type};
use crate::rama::rejection::VldRejection;
use rama::http::service::web::extract::{FromRequest, FromRequestBody};
use rama::http::{Body, Request, header};
use vld::schema::VldParse;

/// Rama extractor that validates **JSON request bodies**.
///
/// Drop-in alternative to `rama::http::service::web::extract::Json<T>` that
/// uses `vld` instead of `serde::Deserialize`. Bodies larger than the
/// [`VldBodyLimit`](crate::rama::VldBodyLimit) extension (default 2 MiB) are
/// rejected with 413; a missing or non-JSON `Content-Type` (see
/// [`crate::common::is_json_content_type`]) with 415.
pub struct VldJson<T>(pub T);

impl<T> FromRequest for VldJson<T>
where
    T: VldParse + Send + Sync + 'static,
{
    type Rejection = VldRejection;

    async fn from_request(req: Request) -> Result<Self, Self::Rejection> {
        let (parts, body) = req.into_parts();
        Self::from_request_body(&parts, body).await
    }
}

impl<T> FromRequestBody for VldJson<T>
where
    T: VldParse + Send + Sync + 'static,
{
    // Must not capture `&Parts` — future is `'static` (Rama FromRequestBody contract).
    #[allow(clippy::manual_async_fn)]
    fn from_request_body(
        parts: &rama::http::request::Parts,
        body: Body,
    ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send + 'static {
        let content_type = require_json_content_type(
            parts
                .headers
                .get(header::CONTENT_TYPE)
                .and_then(|v| v.to_str().ok()),
        );
        let limit = super::body_limit(parts);
        async move {
            content_type?;
            let bytes = super::read_body(body, limit).await?;
            let value = parse_json_body(&bytes)?;
            let parsed = T::vld_parse_value(&value).map_err(VldRejection::from_vld)?;
            Ok(VldJson(parsed))
        }
    }
}