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");

§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;

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.

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.