/protobuf-validation

Protobuf validator for Java

Primary LanguageJavaApache License 2.0Apache-2.0

DEPRECATED AND ARCHIVED

https://github.com/envoyproxy/protoc-gen-validate now supports Java. PGV is much richer in terms of validation rules.

This project has entered ARCHIVED state and will not be maintained.

Protobuf validation libary CircleCI

This library allows you to add validation statements inside the proto files as options to fields.

It supports validation both standalone and as a gRPC client and server interceptor.

Based on initial work by SeriousMa: https://github.com/SeriousMa/grpc-protobuf-validation

Usage

Add rules to your proto files:

import "validation.proto";
message ExampleMessage {
    string name = 1[(validation.regex) = "[A-Z]{2,}"];
}

Standalone protobuf validation

  ProtobufValidator validator = new ProtobufValidator();
  validator.validate(protobufJavaMessage);

An MessageValidationException is thrown in case of any validation errors.

gRPC integration

Example shows gRPC server setup

Create grpc server

 Server greetingServer = ServerBuilder.forPort(8080).addService(ServerInterceptors.intercept(new GreetingServiceImpl(), new ValidationInterceptor())).build();

Create grpc channel

  ManagedChannelBuilder builder = ManagedChannelBuilder.forAddress(url.getHost(), url.getPort()).usePlaintext(true);
  builder.intercept(new ValidationInterceptor());

Supported rules

import "validation.proto";

message HelloRequest {
    string name = 1[(validation.regex) = ""];
    int32 age = 2 [(validation.max) = 100, (validation.min) = 18];
    repeated string hobbies = 3 [(validation.repeatMax) = 5, (validation.repeatMin) = 2];
    map<string, string> bagOfTricks = 4 [(validation.repeatMax) = 10, (validation.repeatMin) = 2];
    Sentiment sentiment = 5 [(validation.required) = true;
    int64 future_timemilles = 6 [(validation.future) = true];
    int64 past_timemilles = 7 [(validation.past) = true];
    string forbidden_field = 8 [(validation.forbidden) = true;
}