#prost #validation #reflection #proto #rules #prost-reflect #protoc-gen-validate

prost-reflect-validate

protoc-gen-validate's validation using prost-reflect

1 unstable release

new 0.1.0 Sep 22, 2024
0.0.1 Sep 17, 2024
0.0.0 Sep 17, 2024

#665 in Encoding

Download history 239/week @ 2024-09-15 170/week @ 2024-09-22

409 downloads per month

Apache-2.0

115KB
3K SLoC

prost-validate

A protobuf library extending prost and prost-reflect with validation support.

This a rust implementation protoc-gen-validate.

Usage

It currently supports validation using reflection.

It must be used with prost and prost-reflect generated code.

All validation rules are documented in the proto file or in the protoc-gen-validate documentation.

Proto definition

syntax = "proto3";

package validate.example;

import "validate/validate.proto";

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

Validation

It exposes a single extension trait ValidatorExt which can be used to validate protobuf reflect messages.

mod proto {
    use once_cell::sync::Lazy;
    use prost_reflect::DescriptorPool;

    static DESCRIPTOR_POOL: Lazy<DescriptorPool> = Lazy::new(|| DescriptorPool::decode(include_bytes!(concat!(env!("OUT_DIR"), "/file_descriptor_set.bin")).as_ref()).unwrap());
    include!(concat!(env!("OUT_DIR"), "/validate.example.rs"));
}

fn main() {
    use prost_reflect_validate::ValidatorExt;
    use crate::proto::ExampleMessage;

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

Ouput

Validation failed: validate.example.ExampleMessage.content: must be Hello, world!
Validation passed

Minimum Supported Rust Version

Rust 1.64 or higher.

The minimum supported Rust version may be changed in the future, but it will be done with a minor version bump.

Dependencies

~7–16MB
~208K SLoC