Messaging
vld_data::centrifuge validates payloads of the centrifuge-client crate as JSON: values before they are published or sent as an RPC, publications and RPC results after they arrive.
centrifuge-client is at 0.1: its API may change in a minor release, and this module follows it.Install
Cargo.toml
[dependencies]
vld = "0.8"
vld-data = { version = "0.8", features = ["centrifuge"] }
centrifuge = { package = "centrifuge-client", version = "0.1" }
| Feature | Module | Notes |
|---|---|---|
centrifuge | vld_data::centrifuge | centrifuge-client 0.1 (library name centrifuge); default features off, ring on (the crate needs a rustls crypto provider to compile) |
Example
use vld_data::centrifuge::prelude::*;
vld::schema! {
#[derive(Debug, serde::Serialize)]
pub struct ChatMessage {
pub text: String => vld::string().min(1).max(500),
}
}
vld::schema! {
#[derive(Debug, serde::Serialize)]
pub struct Echo {
pub text: String => vld::string(),
}
}
async fn run(
client: ¢rifuge::Client,
sub: ¢rifuge::Subscription,
event: centrifuge::PublicationEvent,
) -> Result<(), VldCentrifugeError> {
publish(sub, &ChatMessage { text: "hi".into() }).await?;
client_publish(client, "chat:lobby", &ChatMessage { text: "hello".into() }).await?;
let echo: Echo = rpc(client, "echo", &ChatMessage { text: "ping".into() }).await?;
let msg: ChatMessage = decode_publication(&event.publication)?;
Ok(())
}
API
publish(&subscription, &value)— validate, encode as JSON,Subscription::publish.client_publish(&client, channel, &value)— the same withClient::publish.rpc::<Req, Resp>(&client, method, &req)— validatereq,Client::rpc, then parse and validate the result asResp.decode_publication::<T>(&publication),decode_rpc::<T>(&result),decode::<T>(&bytes),encode(&value).VldCentrifugeError—Validation,Serialization,Deserialization,Centrifuge(centrifuge::Error).
Gotchas
- The value type is the schema: published values must be
Serialize + VldParse. - Payloads are JSON bytes; on a Protobuf connection they travel as opaque bytes and are still JSON inside.
- An invalid value never reaches the client; a valid one can still fail there (
Centrifuge(Error::Timeout),ClientClosed, …).