Web frameworks
Validate tonic gRPC messages and metadata with vld schemas.

Validation helpers for tonic gRPC services: attach rules to prost messages, validate request metadata, and turn failures into Status::invalid_argument. Only tonic::{Request, Status} are used, so no tonic transport features are needed.

Install

Cargo.toml
[dependencies]
vld = "0.8"
vld-web = { version = "0.8", features = ["tonic"] }

Module vld_web::tonic. The dependency is tonic >=0.12, <0.15: one feature, Cargo picks the tonic you use.

Example

use tonic::{Request, Response, Status};

// Generated by prost; vld needs no extra derives.
#[derive(Clone, PartialEq, prost::Message)]
pub struct CreateUserRequest {
    #[prost(string, tag = "1")]
    pub name: String,
    #[prost(string, tag = "2")]
    pub email: String,
}

vld_web::tonic::impl_validate!(CreateUserRequest {
    name  => vld::string().min(2).max(100),
    email => vld::string().email(),
});

vld::schema! {
    #[derive(Debug, Clone)]
    pub struct AuthMeta {
        pub authorization: String => vld::string().starts_with("Bearer "),
    }
}

async fn create_user(request: Request<CreateUserRequest>) -> Result<Response<()>, Status> {
    let auth: AuthMeta = vld_web::tonic::validate_metadata(&request)?;
    let msg = vld_web::tonic::validate(request)?; // Status::invalid_argument on failure
    println!("{} by {}", msg.name, auth.authorization);
    Ok(Response::new(()))
}

impl_validate! builds on vld::impl_rules!: it adds inherent validate() / is_valid() methods and implements VldMessage. Each listed field is serialized with serde and checked by its schema.

Validate against a separate schema

When a vld::schema! struct mirrors the message, validate a serde::Serialize message against it:

let msg = vld_web::tonic::validate_with::<UserSchema, _>(request)?;

Metadata interceptor

Server::builder()
    .add_service(MyServiceServer::with_interceptor(
        my_service,
        vld_web::tonic::metadata_interceptor::<AuthMeta>,
    ))
    .serve(addr)
    .await?;

// In the handler:
let auth = vld_web::tonic::validated_metadata::<AuthMeta, _>(&request)
    .ok_or_else(|| Status::unauthenticated("missing auth"))?;

API

ItemPurpose
impl_validate!(Type { field => schema, .. })Attach rules to a message, implement VldMessage
VldMessageTrait with vld_validate(&self) -> Result<(), VldError>
validate(request) / validate_ref(&msg)Validate a VldMessage
validate_with::<S, _>(request) / validate_with_ref::<S, _>(&msg)Validate a Serialize message against schema S
validate_metadata::<T, _>(&request)Parse metadata into T
metadata_interceptor::<T>Interceptor that validates metadata and stores Validated<T>
validated_metadata::<T, _>(&request)Option<T> stored by the interceptor
Validated<T>Extension wrapper (derefs to T)
vld_status(&err) / vld_status_with_code(&err, code)VldError to Status
preludeAll of the above plus vld::prelude::*

Status format

vld_status returns Code::InvalidArgument with a JSON message:

{
  "error": "VALIDATION_ERROR",
  "message": "Request validation failed",
  "details": [
    { "path": ".name", "message": "String must be at least 2 characters", "code": "too_small" }
  ]
}

path uses the same format as the HTTP adapters (.user.items[0].name); code is IssueCode::key().

Gotchas

  • Metadata keys and values follow the header rules (x-request-id is the field x_request_id); binary (-bin) keys are skipped.
  • No body limit of its own: tonic's max_decoding_message_size applies.
  • validate_with serializes the message first; a serialization failure is Status::internal.