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
//! # `vld_web::rama` — Rama integration for `vld`
//!
//! Extractors and a [`ValidateJsonLayer`] that validate HTTP request data
//! with `vld` schemas (no `serde::Deserialize` needed on your types).
//!
//! | API | Source |
//! |---|---|
//! | [`VldJson<T>`] | JSON request body |
//! | [`VldQuery<T>`] | URL query parameters |
//! | [`VldPath<T>`] | URL path parameters |
//! | [`VldForm<T>`] | URL-encoded form body |
//! | [`VldHeaders<T>`] | HTTP headers |
//! | [`VldCookie<T>`] | Cookie values |
//! | [`ValidateJsonLayer<T>`] | Middleware: validates JSON bodies, stores [`Validated<T>`] |
//!
//! Failures are JSON responses (see the
//! [error body format](crate::common#error-bodies)): **422** for invalid or
//! malformed input, **413** for a body over the limit, **415** when
//! [`VldJson`] gets no JSON `Content-Type` (`application/json` or
//! `application/*+json`) or [`VldForm`] no
//! `application/x-www-form-urlencoded`. [`ValidateJsonLayer`] passes
//! non-JSON requests through, so [`validated`] returns `Option<T>`.
//!
//! # Example
//!
//! ```
//! use rama::http::service::web::WebService;
//! use rama::http::{Body, Request, StatusCode};
//! use rama::Service;
//! use vld_web::rama::VldJson;
//!
//! vld::schema! {
//! #[derive(Debug)]
//! pub struct CreateUser {
//! pub name: String => vld::string().min(2),
//! pub email: String => vld::string().email(),
//! }
//! }
//!
//! async fn create(VldJson(user): VldJson<CreateUser>) -> String {
//! format!("created {}", user.name)
//! }
//!
//! # #[tokio::main]
//! # async fn main() {
//! let svc = WebService::default().with_post("/users", create);
//!
//! let req = Request::builder()
//! .method("POST")
//! .uri("http://example.com/users")
//! .header("content-type", "application/json")
//! .body(Body::from(r#"{"name":"A","email":"bad"}"#))
//! .unwrap();
//! let resp = svc.serve(req).await.unwrap();
//! assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
//! # }
//! ```
//!
//! # Body limit
//!
//! [`DEFAULT_BODY_LIMIT`](crate::common::DEFAULT_BODY_LIMIT) (2 MiB).
//! The extractors read the [`VldBodyLimit`] request extension, e.g. added
//! with `rama::layer::AddInputExtensionLayer::new(VldBodyLimit(n))`; the
//! layer takes [`ValidateJsonLayer::with_limit`].
//!
//! Requires Rust **1.96+** (Rama's MSRV).
//!
//! Guide: <https://s00d.github.io/vld/integrations/web/rama>
pub use ;
pub use ;
pub use VldRejection;
/// Body size limit (bytes) for [`VldJson`] and [`VldForm`], read from the
/// request extensions; without it they use
/// [`DEFAULT_BODY_LIMIT`](crate::common::DEFAULT_BODY_LIMIT) (2 MiB).
;
/// Prelude — import everything you need.