| Crates.io | prost-validate-build |
| lib.rs | prost-validate-build |
| version | 0.2.7 |
| created_at | 2024-10-09 17:10:41.169131+00 |
| updated_at | 2025-06-06 10:55:53.308794+00 |
| description | protoc-gen-validate's validation generation using prost-build |
| homepage | |
| repository | https://github.com/linka-cloud/prost-validate |
| max_upload_size | |
| id | 1402757 |
| size | 17,678 |
prost-validate-buildA 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.
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.
cargo add prost-validate --features derive
cargo add prost-validate-build --build
proto/message.proto:
syntax = "proto3";
package validate.example;
import "validate/validate.proto";
message ExampleMessage {
string content = 1 [(validate.rules).string = {const: "Hello, world!"}];
}
build.rs:
fn main() -> Result<(), Box<dyn std::error::Error>> {
prost_validate_build::Builder::new()
.compile_protos(&["message.proto"], &["proto", "../prost-validate-types/proto"])?;
Ok(())
}
prost-build configuration for usage with other generatorsprost-reflect-build and tonic-buildservice.proto:
syntax = "proto3";
package validate.example;
import "validate/validate.proto";
import "message.proto";
service ExampleService {
rpc ExampleMethod (ExampleMessage) returns (ExampleMessage);
}
build.rs:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = &["message.proto", "service.proto"];
let includes = &["proto", "../prost-validate-types/proto"];
let mut config = {
let mut c = prost_build::Config::new();
c.service_generator(tonic_build::configure().service_generator());
c
};
prost_validate_build::Builder::new().configure(&mut config, files, includes)?;
prost_reflect_build::Builder::new()
.descriptor_pool("DESCRIPTOR_POOL")
.compile_protos_with_config(config, files, includes)?;
Ok(())
}
include!(concat!(env!("OUT_DIR"), "/validate.example.rs"));
fn main() {
use example_proto::ExampleMessage;
use prost_validate::Validator;
match ExampleMessage::default().validate() {
Ok(_) => println!("Validation passed"),
Err(e) => eprintln!("Validation failed: {}", e),
}
let msg = ExampleMessage {
content: "Hello, world!".to_string(),
};
match msg.validate() {
Ok(_) => println!("Validation passed"),
Err(e) => eprintln!("Validation failed: {}", e),
}
}
Output:
Validation failed: "validate.example.ExampleMessage.content": must be equal to "Hello, world!"
Validation passed