Expand description
§vld-tonic — tonic gRPC integration for the vld validation library
Validate protobuf messages and gRPC metadata using vld schemas.
§Approaches
| Function | Use case |
|---|---|
validate / validate_ref | Validate a message that implements VldMessage (via impl_validate!) |
validate_with / validate_with_ref | Validate against a separate vld::schema! struct |
validate_metadata | Validate gRPC request metadata |
metadata_interceptor | Tonic 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
VldErrorinto atonic::StatuswithINVALID_ARGUMENTcode. - vld_
status_ with_ code - Convert a
VldErrorinto atonic::Statuswith a custom gRPC code.