Expand description
Runtime validation for Protocol Buffer messages using
buf.validate rules.
This crate dynamically inspects prost-reflect message descriptors at runtime,
compiles buf.validate constraint annotations (including CEL expressions),
and evaluates them against concrete message instances.
§Quick start
For one-off validation, use the validate convenience function:
use prost_protovalidate::validate;
match validate(&msg) {
Ok(()) => { /* message is valid */ }
Err(e) => eprintln!("validation failed: {e}"),
}For repeated validations, construct a Validator once to cache compiled
rules across calls:
use prost_protovalidate::Validator;
let validator = Validator::new();
validator.validate(&msg).expect("message should be valid");§Feature flags
| Feature | Default | Description |
|---|---|---|
cel | Yes | CEL expression evaluation and chrono time support. Disable for a lighter dependency footprint when only standard rules are used. |
tonic | No | Adds tonic integration: a From<ValidationError> for tonic::Status impl and a ValidateRequest extension trait so gRPC handlers can call req.validate_inner()?. |
tonic-types | No | Implies tonic. Attaches a google.rpc.BadRequest detail with one FieldViolation per Violation to validation-failure statuses. |
Without the cel feature, any message or field annotated with CEL
expressions (via both cel and legacy cel_expression, including
buf.validate.predefined rules) will produce a CompilationError at
validation time. Standard rules (range checks, string constraints, format
validators, etc.) work without cel.
§Error types
| Type | When |
|---|---|
ValidationError | One or more constraint violations detected |
CompilationError | A CEL expression or constraint definition failed to parse |
RuntimeError | An unexpected failure during evaluation |
All three are unified under Error.
§Re-exported types
The types module re-exports prost-protovalidate-types
so consumers do not need to depend on it directly.
Re-exports§
pub use prost_protovalidate_types as types;pub use regex;
Modules§
- time
- Time helpers shared between the runtime validator and code generated
by
prost-protovalidate-build. - tonic
- Optional integration with
tonic(gRPC). - validators
- Format validators for well-known string constraints.
Structs§
- Compilation
Error - Returned when a validation rule cannot be compiled from its descriptor.
- Runtime
Error - Returned when runtime evaluation of dynamic rules fails.
- Validation
Error - Returned when one or more validation rules are violated.
- Validator
- Thread-safe validator for Protocol Buffer messages.
- Violation
- A single instance where a validation rule was not met.
Enums§
- Error
- Top-level error type returned by validation.
- Validation
Option - Options for configuring a single
Validator::validate_withcall. - Validator
Option - Options for configuring the
Validatorat construction time.
Traits§
- Filter
- Controls which fields/messages are validated.
- Validate
- Compile-time validation for Protocol Buffer messages with generated validators.
Functions§
- normalize_
edition_ descriptor_ set - Normalize protobuf Edition 2023 descriptors to proto3 format for
compatibility with
prost-reflect0.16 which does not support editions. Normalize aFileDescriptorSetso that any Edition 2023 files are rewritten asproto3. Returns the original bytes unchanged if no edition files are detected. - validate
- Validate a message using a global
Validatorinstance.