prost-validate-build
A protobuf library extending prost with validation support.
prost-validate-build is a crate that can be used to generate protobuf message validation from
protoc-gen-validate annotations.
This crate is intended to be used in a build.rs script to generate the validation code for the messages.
It depends on the prost-validate crate's derive feature to generate the validation code.
Usage
It can be used to compile the .proto files into Rust using prost-build
or to simply generate the prost-build configuration.
All validation rules are documented in the proto file or in the protoc-gen-validate documentation.
Example Protobuf definition
proto/message.proto:
syntax = "proto3";
package validate.example;
import "validate/validate.proto";
message ExampleMessage {
string content = 1 [(validate.rules).string = {const: "Hello, world!"}];
}
Generating Rust code with validation
build.rs:
Generating prost-build configuration for usage with other generators
Example for prost-reflect-build
build.rs:
Include the generated code
include!;
Using the generated code
Output:
Validation failed: "validate.example.ExampleMessage.content": must be equal to "Hello, world!"
Validation passed