10 releases

0.2.9 Nov 26, 2025
0.2.8 Nov 25, 2025
0.2.7 Jun 6, 2025
0.2.6 Oct 16, 2024

#29 in #prost

Download history 11055/week @ 2026-03-03 68811/week @ 2026-03-10 71408/week @ 2026-03-17 73536/week @ 2026-03-24 46168/week @ 2026-03-31 59905/week @ 2026-04-07 62058/week @ 2026-04-14 75582/week @ 2026-04-21 73236/week @ 2026-04-28 94866/week @ 2026-05-05 106782/week @ 2026-05-12 98869/week @ 2026-05-19 94122/week @ 2026-05-26 91134/week @ 2026-06-02

410,657 downloads per month
Used in 2 crates

Apache-2.0

39KB
964 lines

crates.io docs.rs deps.rs Continuous integration Apache 2.0

prost-validate

A protobuf library extending prost with validation support.

This is a rust implementation of protoc-gen-validate.

It uses the prost crate to generate the derive based validation code.

For a reflection based implementation see the prost-reflect-validate crate.

Usage

It must be used with prost generated code.

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 definition

proto/message.proto:

syntax = "proto3";

package validate.example;

import "validate/validate.proto";

message ExampleMessage {
  string content = 1 [(validate.rules).string = {const: "Hello, world!"}];
}

Build script

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(())
}

Validation

Include the generated code

include!(concat!(env!("OUT_DIR"), "/validate.example.rs"));

Using the generated code

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

Dependencies

~4–7.5MB
~130K SLoC