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
//! # `vld_web::axum::ws` — validated axum WebSocket messages
//!
//! Decode, encode and answer [`axum::extract::ws::Message`]s whose payload
//! is a JSON message (feature `axum-ws` = `axum` + axum's `ws`). The same
//! rules as [`vld_web::tungstenite`](https://docs.rs/vld-web/latest/vld_web/tungstenite/):
//! malformed JSON is a single `parse_error` issue at path `""`, and the
//! error frame body is the [422 body](crate::common#error-bodies) of the
//! HTTP extractors.
//!
//! | Function | Use case |
//! |---|---|
//! | [`decode`] | Validate an incoming text / binary frame (`None` for ping, pong and close) |
//! | [`encode`] | Validate an outgoing value against its own schema and build a text frame |
//! | [`error_message`] | A text frame with the HTTP 422 body, to answer an invalid message |
//!
//! # Example
//!
//! ```
//! use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
//! use axum::response::Response;
//! use axum::{routing::any, Router};
//! use vld_web::axum::ws::{decode, error_message};
//!
//! vld::schema! {
//! #[derive(Debug)]
//! pub struct Chat {
//! pub room: String => vld::string().min(1),
//! pub text: String => vld::string().min(1).max(500),
//! }
//! }
//!
//! async fn chat(ws: WebSocketUpgrade) -> Response {
//! ws.on_upgrade(handle)
//! }
//!
//! async fn handle(mut socket: WebSocket) {
//! while let Some(Ok(msg)) = socket.recv().await {
//! let reply = match decode::<Chat>(&msg) {
//! Ok(Some(chat)) => Message::text(format!("{}: {}", chat.room, chat.text)),
//! Ok(None) => continue, // ping / pong / close
//! Err(err) => error_message(&err),
//! };
//! if socket.send(reply).await.is_err() {
//! break;
//! }
//! }
//! }
//!
//! let app: Router = Router::new().route("/ws", any(chat));
//! ```
//!
//! Guide: <https://s00d.github.io/vld/integrations/web/websocket>
use Message;
use VldError;
use VldParse;
/// Validate the JSON payload of a text or binary frame.
///
/// `Ok(None)` for ping, pong and close (axum answers pings itself); `Err`
/// when the payload is not JSON or fails `T`'s schema.
/// Serialize `value` to a JSON text frame after checking the JSON against
/// `T`'s own schema.
/// A text frame with the HTTP 422 body:
/// `{"error": "Validation failed", "issues": [{"path", "message", "code"}]}`.