Skip to main content

Crate vld_tonic

Crate vld_tonic 

Source
Expand description

§vld-tonic — tonic gRPC integration for the vld validation library

Validate protobuf messages and gRPC metadata using vld schemas.

§Approaches

FunctionUse case
validate / validate_refValidate a message that implements VldMessage (via impl_validate!)
validate_with / validate_with_refValidate against a separate vld::schema! struct
validate_metadataValidate gRPC request metadata
metadata_interceptorTonic interceptor for automatic metadata validation

§Quick example

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

// Protobuf message (generated by prost with serde support)
#[derive(Clone, Serialize)]
pub struct CreateUserRequest {
    pub name: String,
    pub email: String,
}

// Attach validation rules
vld_tonic::impl_validate!(CreateUserRequest {
    name  => vld::string().min(2).max(100),
    email => vld::string().email(),
});

// In your service handler:
async fn create_user(request: Request<CreateUserRequest>) -> Result<Response<()>, Status> {
    let msg = vld_tonic::validate(request)?;
    // msg is validated
    Ok(Response::new(()))
}

Modules§

prelude
Prelude — import everything you need.

Macros§

impl_validate
Attach vld validation rules to a protobuf message type.

Traits§

VldMessage
Trait for message types that can be validated with vld.

Functions§

metadata_interceptor
A tonic interceptor function that validates request metadata.
validate
Validate a gRPC request message that implements VldMessage.
validate_metadata
Validate gRPC request metadata against a vld schema.
validate_ref
Validate a message reference without consuming the request.
validate_with
Validate a gRPC request message against a separate vld schema type.
validate_with_ref
Validate a message reference against a separate vld schema type.
validated_metadata
Retrieve a validated metadata struct from request extensions.
vld_status
Convert a VldError into a tonic::Status with INVALID_ARGUMENT code.
vld_status_with_code
Convert a VldError into a tonic::Status with a custom gRPC code.