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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! # `vld_web::warp` — Warp integration for `vld`
//!
//! Validation [filters](warp::Filter) for [Warp](https://docs.rs/warp) 0.3
//! and 0.4 (the dependency is `>=0.3, <0.5`; there are no version features).
//!
//! | Filter / Function | Source |
//! |-------------------|--------|
//! | [`vld_json::<T>()`] | JSON body |
//! | [`vld_query::<T>()`] | Query string |
//! | [`vld_form::<T>()`] | URL-encoded form body |
//! | [`vld_param::<T>(name)`](vld_param) | Single path segment |
//! | [`vld_path::<T>(names)`](vld_path) | All remaining path segments (tail) |
//! | [`validate_path_params::<T>(pairs)`](validate_path_params) | Pre-extracted path params |
//! | [`vld_headers::<T>()`] | HTTP headers |
//! | [`vld_cookie::<T>()`] | Cookie values |
//!
//! Every filter rejects with [`VldRejection`] (wrapping [`VldHttpError`]).
//! Add [`handle_rejection`] with `.recover()` to turn rejections into JSON
//! (see the [error body format](crate::common#error-bodies)):
//! validation / malformed body → 422, body over the limit → 413, `vld_json`
//! without a JSON `Content-Type` (`application/json` or `application/*+json`)
//! or `vld_form` without `application/x-www-form-urlencoded` → 415, plus
//! the usual warp rejections (404, 405, 400, 411, 415) and 500 for anything
//! unknown.
//!
//! # Example
//!
//! ```no_run
//! use vld_web::warp::prelude::*;
//! use warp::Filter;
//!
//! vld::schema! {
//!     #[derive(Debug, Clone)]
//!     pub struct CreateUser {
//!         pub name: String  => vld::string().min(2),
//!         pub email: String => vld::string().email(),
//!     }
//! }
//!
//! # #[tokio::main]
//! # async fn main() {
//! let create = warp::post()
//!     .and(warp::path("users"))
//!     .and(vld_json::<CreateUser>())
//!     .map(|u: CreateUser| warp::reply::json(&serde_json::json!({ "name": u.name })));
//!
//! warp::serve(create.recover(handle_rejection))
//!     .run(([127, 0, 0, 1], 3030))
//!     .await;
//! # }
//! ```
//!
//! # Body limit
//!
//! `vld_json` / `vld_form` stream the body and stop at
//! [`DEFAULT_BODY_LIMIT`](crate::common::DEFAULT_BODY_LIMIT) (2 MiB), also
//! without a `Content-Length` (chunked uploads);
//! [`vld_json_limit`] / [`vld_form_limit`] take an explicit limit.
//!
//! Guide: <https://s00d.github.io/vld/integrations/web/warp>
use crate::common::{
    VldHttpError, cookie_headers_to_json, headers_to_json, parse_coerced, parse_form_body,
    parse_json_body, path_params_to_json, percent_decode, query_string_to_json,
    require_form_content_type, require_json_content_type,
};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use futures_util::{Stream, StreamExt};
use std::convert::Infallible;
use vld::schema::VldParse;
use warp::http::{HeaderValue, StatusCode, header};
use warp::reject::Reject;
use warp::{Filter, Rejection, Reply};

// ---------------------------------------------------------------------------
// Rejection type
// ---------------------------------------------------------------------------

/// The rejection of every `vld_*` filter: validation / parse failure (422),
/// body too large (413) or wrong `Content-Type` (415). [`handle_rejection`]
/// answers it with [`status`](Self::status) and [`body`](Self::body).
#[derive(Debug)]
pub struct VldRejection(VldHttpError);

impl VldRejection {
    /// The underlying validation error.
    pub fn error(&self) -> &vld::error::VldError {
        self.0.error()
    }

    /// HTTP status (422, 413 or 415).
    pub fn status(&self) -> StatusCode {
        StatusCode::from_u16(self.0.status()).unwrap_or(StatusCode::UNPROCESSABLE_ENTITY)
    }

    /// JSON response body.
    pub fn body(&self) -> serde_json::Value {
        self.0.body()
    }
}

impl From<VldHttpError> for VldRejection {
    fn from(e: VldHttpError) -> Self {
        Self(e)
    }
}

impl Reject for VldRejection {}

fn reject(e: impl Into<VldHttpError>) -> Rejection {
    warp::reject::custom(VldRejection(e.into()))
}

/// Validate a value coerced from text (query, path, form, headers, cookies).
fn validate<T: VldParse>(value: &serde_json::Value) -> Result<T, Rejection> {
    parse_coerced(value).map_err(reject)
}

/// The raw value of header `name`, if any. Unlike
/// `warp::header::optional::<String>`, a non-ASCII value is not a 400.
fn raw_header(
    name: &'static str,
) -> impl Filter<Extract = (Option<HeaderValue>,), Error = Infallible> + Clone {
    warp::header::value(name)
        .map(Some)
        .or(warp::any().map(|| None))
        .unify()
}

fn header_str(value: &Option<HeaderValue>) -> Option<&str> {
    value.as_ref().and_then(|v| v.to_str().ok())
}

/// Read the whole body, streaming, up to `limit` bytes. Works without a
/// `Content-Length` header (chunked uploads).
fn limited_body(limit: u64) -> impl Filter<Extract = (Bytes,), Error = Rejection> + Clone {
    warp::body::stream().and_then(move |stream| collect_limited(stream, limit))
}

async fn collect_limited<S, B>(stream: S, limit: u64) -> Result<Bytes, Rejection>
where
    S: Stream<Item = Result<B, warp::Error>>,
    B: Buf,
{
    let limit = usize::try_from(limit).unwrap_or(usize::MAX);
    let mut stream = std::pin::pin!(stream);
    let mut buf = BytesMut::new();
    while let Some(chunk) = stream.next().await {
        let chunk = chunk.map_err(|e| {
            reject(VldHttpError::parse(format!(
                "Failed to read request body: {e}"
            )))
        })?;
        if buf.len().saturating_add(chunk.remaining()) > limit {
            return Err(reject(VldHttpError::payload_too_large()));
        }
        buf.put(chunk);
    }
    Ok(buf.freeze())
}

// ---------------------------------------------------------------------------
// vld_json filter
// ---------------------------------------------------------------------------

/// Warp filter that extracts and validates a JSON body.
///
/// Returns the validated `T` or rejects with [`VldRejection`] (422; 413 for
/// a body over [`DEFAULT_BODY_LIMIT`](crate::common::DEFAULT_BODY_LIMIT);
/// 415 without a JSON `Content-Type`).
///
/// # Example
///
/// ```rust,ignore
/// use vld_web::warp::vld_json;
///
/// vld::schema! {
///     #[derive(Debug, Clone)]
///     pub struct CreateUser {
///         pub name: String  => vld::string().min(2),
///         pub email: String => vld::string().email(),
///     }
/// }
///
/// let route = warp::post()
///     .and(warp::path("users"))
///     .and(vld_json::<CreateUser>())
///     .map(|user: CreateUser| {
///         warp::reply::json(&serde_json::json!({"name": user.name}))
///     });
/// ```
pub fn vld_json<T: VldParse + Send + 'static>()
-> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    vld_json_limit(crate::common::DEFAULT_BODY_LIMIT as u64)
}

/// Like [`vld_json`], but with an explicit body size limit (bytes).
pub fn vld_json_limit<T: VldParse + Send + 'static>(
    limit: u64,
) -> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    raw_header("content-type")
        .and_then(|ct: Option<HeaderValue>| async move {
            require_json_content_type(header_str(&ct)).map_err(reject)
        })
        .untuple_one()
        .and(limited_body(limit))
        .and_then(|bytes: Bytes| async move {
            let value = parse_json_body(&bytes).map_err(reject)?;
            T::vld_parse_value(&value).map_err(reject)
        })
}

// ---------------------------------------------------------------------------
// vld_query filter
// ---------------------------------------------------------------------------

/// Warp filter that extracts and validates query parameters.
///
/// See [`crate::common::parse_query_string`] for coercion and array rules.
///
/// # Example
///
/// ```rust,ignore
/// use vld_web::warp::vld_query;
///
/// vld::schema! {
///     #[derive(Debug, Clone)]
///     pub struct Pagination {
///         pub page: i64  => vld::number().int().min(1),
///         pub limit: i64 => vld::number().int().min(1).max(100),
///     }
/// }
///
/// let route = warp::get()
///     .and(warp::path("items"))
///     .and(vld_query::<Pagination>())
///     .map(|p: Pagination| {
///         warp::reply::json(&serde_json::json!({"page": p.page, "limit": p.limit}))
///     });
/// ```
pub fn vld_query<T: VldParse + Send + 'static>()
-> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    warp::query::raw()
        .or(warp::any().map(String::new))
        .unify()
        .and_then(|qs: String| async move { validate(&query_string_to_json(&qs)) })
}

// ---------------------------------------------------------------------------
// Recovery handler
// ---------------------------------------------------------------------------

/// Recovery handler that converts rejections into JSON responses.
///
/// | Rejection | Status |
/// |---|---|
/// | [`VldRejection`] | its [`status`](VldRejection::status) (422 / 413 / 415) and [`body`](VldRejection::body) |
/// | `PayloadTooLarge` | 413 |
/// | `MethodNotAllowed` | 405 |
/// | `UnsupportedMediaType` | 415 |
/// | `LengthRequired` | 411 |
/// | `MissingHeader`, `InvalidHeader`, `MissingCookie`, `InvalidQuery` | 400 |
/// | not found | 404 |
/// | anything else | 500 |
///
/// Use with `warp::Filter::recover()`:
///
/// ```rust,ignore
/// use vld_web::warp::handle_rejection;
///
/// let routes = warp::any()
///     // ...your routes...
///     .recover(handle_rejection);
/// ```
pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
    use warp::reject as r;

    let (status, body) = if let Some(e) = err.find::<VldRejection>() {
        (e.status(), e.body())
    } else if err.find::<r::PayloadTooLarge>().is_some() {
        (
            StatusCode::PAYLOAD_TOO_LARGE,
            crate::common::format_payload_too_large(),
        )
    } else if err.is_not_found() {
        (
            StatusCode::NOT_FOUND,
            crate::common::format_generic_error("Not Found"),
        )
    } else {
        let known = |status: StatusCode, msg: String| (status, msg);
        // Same priority as warp: any specific error beats 405, 405 beats 404.
        let (status, msg) = if let Some(e) = err.find::<r::UnsupportedMediaType>() {
            known(StatusCode::UNSUPPORTED_MEDIA_TYPE, e.to_string())
        } else if let Some(e) = err.find::<r::LengthRequired>() {
            known(StatusCode::LENGTH_REQUIRED, e.to_string())
        } else if let Some(e) = err.find::<r::MissingHeader>() {
            known(StatusCode::BAD_REQUEST, e.to_string())
        } else if let Some(e) = err.find::<r::InvalidHeader>() {
            known(StatusCode::BAD_REQUEST, e.to_string())
        } else if let Some(e) = err.find::<r::MissingCookie>() {
            known(StatusCode::BAD_REQUEST, e.to_string())
        } else if let Some(e) = err.find::<r::InvalidQuery>() {
            known(StatusCode::BAD_REQUEST, e.to_string())
        } else if let Some(e) = err.find::<r::MethodNotAllowed>() {
            known(StatusCode::METHOD_NOT_ALLOWED, e.to_string())
        } else {
            known(
                StatusCode::INTERNAL_SERVER_ERROR,
                "Internal Server Error".to_string(),
            )
        };
        (status, crate::common::format_generic_error(&msg))
    };

    Ok(warp::reply::with_status(warp::reply::json(&body), status))
}

// ---------------------------------------------------------------------------
// vld_form filter
// ---------------------------------------------------------------------------

/// Warp filter that extracts and validates a URL-encoded form body
/// (`application/x-www-form-urlencoded`, 415 otherwise).
///
/// Values are parsed the same way as query parameters.
pub fn vld_form<T: VldParse + Send + 'static>()
-> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    vld_form_limit(crate::common::DEFAULT_BODY_LIMIT as u64)
}

/// Like [`vld_form`], but with an explicit body size limit (bytes).
pub fn vld_form_limit<T: VldParse + Send + 'static>(
    limit: u64,
) -> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    raw_header("content-type")
        .and_then(|ct: Option<HeaderValue>| async move {
            require_form_content_type(header_str(&ct)).map_err(reject)
        })
        .untuple_one()
        .and(limited_body(limit))
        .and_then(|bytes: Bytes| async move { validate(&parse_form_body(&bytes).map_err(reject)?) })
}

// ---------------------------------------------------------------------------
// vld_param — single path parameter filter
// ---------------------------------------------------------------------------

/// Warp filter that extracts and validates **a single path segment**.
///
/// Works like `warp::path::param::<String>()` but percent-decodes and
/// coerces the raw value and validates via `T::vld_parse_value()`.
///
/// The extracted segment is wrapped into a JSON object `{ "<name>": <coerced> }`
/// so that the vld schema field name matches.
///
/// # Example
///
/// ```rust,ignore
/// use vld_web::warp::vld_param;
///
/// vld::schema! {
///     #[derive(Debug, Clone)]
///     pub struct UserId {
///         pub id: i64 => vld::number().int().min(1),
///     }
/// }
///
/// // GET /users/<id>
/// let route = warp::path("users")
///     .and(vld_param::<UserId>("id"))
///     .and(warp::path::end())
///     .map(|p: UserId| {
///         warp::reply::json(&serde_json::json!({"id": p.id}))
///     });
/// ```
pub fn vld_param<T: VldParse + Send + 'static>(
    name: &'static str,
) -> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    warp::path::param::<String>().and_then(move |raw: String| async move {
        validate(&path_params_to_json([(
            name,
            percent_decode(&raw).as_str(),
        )]))
    })
}

// ---------------------------------------------------------------------------
// vld_path — multi-param tail filter
// ---------------------------------------------------------------------------

/// Warp filter that extracts and validates **all remaining path segments**.
///
/// Uses `warp::path::tail()` internally — all segments after the current
/// position are consumed, percent-decoded and mapped to `param_names` in
/// order. The number of remaining segments **must equal** the number of
/// names; otherwise the request is rejected with 404 (not found).
///
/// Best suited for routes where all remaining segments are parameters
/// (no static segments left).
///
/// # Example
///
/// ```rust,ignore
/// use vld_web::warp::vld_path;
///
/// vld::schema! {
///     #[derive(Debug, Clone)]
///     pub struct PostPath {
///         pub user_id: i64 => vld::number().int().min(1),
///         pub post_id: i64 => vld::number().int().min(1),
///     }
/// }
///
/// // GET /posts/<user_id>/<post_id>
/// let route = warp::path("posts")
///     .and(vld_path::<PostPath>(&["user_id", "post_id"]))
///     .map(|p: PostPath| {
///         warp::reply::json(&serde_json::json!({
///             "user_id": p.user_id,
///             "post_id": p.post_id
///         }))
///     });
/// ```
pub fn vld_path<T: VldParse + Send + 'static>(
    param_names: &'static [&'static str],
) -> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    warp::path::tail().and_then(move |tail: warp::path::Tail| async move {
        let segments: Vec<String> = tail
            .as_str()
            .split('/')
            .filter(|s| !s.is_empty())
            .map(percent_decode)
            .collect();

        if segments.len() != param_names.len() {
            return Err(warp::reject::not_found());
        }

        validate(&path_params_to_json(
            param_names
                .iter()
                .copied()
                .zip(segments.iter().map(String::as_str)),
        ))
    })
}

// ---------------------------------------------------------------------------
// validate_path_params — standalone validator for pre-extracted params
// ---------------------------------------------------------------------------

/// Validate pre-extracted path parameter pairs against a vld schema.
///
/// Values are percent-decoded (warp's `path::param::<String>()` yields the
/// raw, still-encoded segment) and coerced.
///
/// Useful for complex routes where static and dynamic segments are
/// interleaved and you extract `String` params with
/// `warp::path::param::<String>()`, then validate them all at once.
///
/// # Example
///
/// ```rust,ignore
/// use vld_web::warp::validate_path_params;
///
/// vld::schema! {
///     #[derive(Debug, Clone)]
///     pub struct CommentPath {
///         pub user_id: i64 => vld::number().int().min(1),
///         pub post_id: i64 => vld::number().int().min(1),
///         pub comment_id: i64 => vld::number().int().min(1),
///     }
/// }
///
/// // GET /users/<id>/posts/<pid>/comments/<cid>
/// let route = warp::path("users")
///     .and(warp::path::param::<String>())
///     .and(warp::path("posts"))
///     .and(warp::path::param::<String>())
///     .and(warp::path("comments"))
///     .and(warp::path::param::<String>())
///     .and(warp::path::end())
///     .and_then(|uid: String, pid: String, cid: String| async move {
///         validate_path_params::<CommentPath>(&[
///             ("user_id", &uid),
///             ("post_id", &pid),
///             ("comment_id", &cid),
///         ])
///     });
/// ```
pub fn validate_path_params<T: VldParse>(params: &[(&str, &str)]) -> Result<T, Rejection> {
    let decoded: Vec<(&str, String)> = params
        .iter()
        .map(|(k, v)| (*k, percent_decode(v)))
        .collect();
    validate(&path_params_to_json(
        decoded.iter().map(|(k, v)| (*k, v.as_str())),
    ))
}

// ---------------------------------------------------------------------------
// vld_headers filter
// ---------------------------------------------------------------------------

/// Warp filter that extracts and validates HTTP headers.
///
/// See [`crate::common::headers_to_json`]: names become snake_case
/// (`Content-Type` → `content_type`), names containing `_` are ignored,
/// repeated headers are joined with `", "`.
pub fn vld_headers<T: VldParse + Send + 'static>()
-> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    warp::header::headers_cloned().and_then(|headers: warp::http::HeaderMap| async move {
        validate(&headers_to_json(
            headers.iter().map(|(n, v)| (n.as_str(), v.as_bytes())),
        ))
    })
}

// ---------------------------------------------------------------------------
// vld_cookie filter
// ---------------------------------------------------------------------------

/// Warp filter that extracts and validates cookies from the `Cookie`
/// headers (all of them; the first occurrence of a name wins; a non-ASCII
/// value is not a 400).
///
/// Cookie names are used as-is for schema field matching.
/// Values are unquoted, percent-decoded and coerced.
pub fn vld_cookie<T: VldParse + Send + 'static>()
-> impl Filter<Extract = (T,), Error = Rejection> + Clone {
    warp::header::headers_cloned().and_then(|headers: warp::http::HeaderMap| async move {
        validate(&cookie_headers_to_json(
            headers.get_all(header::COOKIE).iter().map(|v| v.as_bytes()),
        ))
    })
}

/// Prelude — import everything you need.
pub mod prelude {
    pub use super::{
        VldRejection, handle_rejection, validate_path_params, vld_cookie, vld_form, vld_form_limit,
        vld_headers, vld_json, vld_json_limit, vld_param, vld_path, vld_query,
    };
    pub use vld::prelude::*;
}