vld-tonic
tonic gRPC integration for the vld validation library.
Validate protobuf messages and gRPC metadata using vld schemas.
Installation
[]
= "0.1"
= "0.1"
= "0.12"
= { = "1", = ["derive"] }
Quick Start — Message Validation
Attach validation rules to protobuf message types with impl_validate!:
use Serialize;
use ;
// Protobuf message (generated by prost with serde support)
// Attach validation rules
impl_validate!;
// In your gRPC service handler:
async
impl_validate! generates:
msg.validate()→Result<(), VldError>msg.is_valid()→boolVldMessagetrait impl (used byvld_tonic::validate)
Schema-Based Validation
If you prefer a separate schema definition:
schema!
async
Metadata Validation
Validate gRPC request metadata (headers):
schema!
async
Metadata keys are converted from kebab-case to snake_case
(x-request-id → x_request_id). Values are coerced ("42" → number,
"true" → boolean).
Metadata Interceptor
Validate metadata automatically for all requests on a service:
use Server;
builder
.add_service
.serve
.await?;
Retrieve the validated metadata in handlers:
async
API Reference
| Function | Description |
|---|---|
validate(request) |
Validate message via VldMessage trait |
validate_ref(&msg) |
Validate reference without consuming request |
validate_with::<S, _>(request) |
Validate against a vld::schema! type |
validate_with_ref::<S, _>(&msg) |
Schema-based validation on reference |
validate_metadata(&request) |
Validate gRPC metadata |
validated_metadata(&request) |
Retrieve validated metadata from extensions |
metadata_interceptor::<T>(request) |
Interceptor for metadata validation |
vld_status(&error) |
Convert VldError → Status::InvalidArgument |
vld_status_with_code(&error, code) |
Convert with custom gRPC code |
impl_validate!(Type { ... }) |
Attach rules + implement VldMessage |
Nested Message Validation
For nested protobuf messages, use vld::object():
impl_validate!;
Running the Example
License
MIT