Skip to main content

Crate prost_protovalidate

Crate prost_protovalidate 

Source
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

FeatureDefaultDescription
celYesCEL expression evaluation and chrono time support. Disable for a lighter dependency footprint when only standard rules are used.
tonicNoAdds tonic integration: a From<ValidationError> for tonic::Status impl and a ValidateRequest extension trait so gRPC handlers can call req.validate_inner()?.
tonic-typesNoImplies 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

TypeWhen
ValidationErrorOne or more constraint violations detected
CompilationErrorA CEL expression or constraint definition failed to parse
RuntimeErrorAn 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§

CompilationError
Returned when a validation rule cannot be compiled from its descriptor.
RuntimeError
Returned when runtime evaluation of dynamic rules fails.
ValidationError
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.
ValidationOption
Options for configuring a single Validator::validate_with call.
ValidatorOption
Options for configuring the Validator at 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-reflect 0.16 which does not support editions. Normalize a FileDescriptorSet so that any Edition 2023 files are rewritten as proto3. Returns the original bytes unchanged if no edition files are detected.
validate
Validate a message using a global Validator instance.