Plugin for protocol buffer compiler.
If you use a goclay to generate HTTP handlers, and a validator.v10 for field validation, then this plugin is for you
The plugin is useful when you want to do validation on the front. To use the validation rules from your proto file, generate information about them in the life format and use this data on the front. Then there is no need for manual synchronization of validation rules.
By running the command you can see the result of the plugin:
make generate
See output/example.validate.json
generated from example/example.proto
.
GOBIN=./bin go install github.com/sergiusd/protoc-gen-goclayvalid
verbose
- mode verbosityoriginal_field_name
- use original field name instead of json_namepretty
- for pretty json
Source proto file
syntax = "proto3";
package v1;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/api/annotations.proto";
service ExampleService {
rpc ExampleCall(ExampleMessage) returns(ReturnType) {
option (google.api.http) = {
get: "/example/1"
additional_bindings {
post: "/example/1/additional"
body: "*"
}
};
}
}
message ExampleMessage {
string my_string = 1 [(gogoproto.moretags) = "validate:\"required,len=15\""];
}
message ReturnType {}
Generated JSON file
{
"GET /example/1": {
"my_string": ["required", "len=15"]
},
"POST /example/1/additional": {
"my_string": ["required", "len=15"]
}
}
An example of using a set of plugins
protoc-gen-gofast
for gRPC handlersprotoc-gen-goclay
for HTTP handlersprotoc-gen-goclayvalid
for generate JSON with validation rules
protoc \
--plugin=protoc-gen-gofast=bin/protoc-gen-gofast \
--plugin=protoc-gen-goclay=bin/protoc-gen-goclay \
--plugin=protoc-gen-goclayvalid=bin/protoc-gen-goclayvalid \
-I. \
--gofast_out=plugins=grpc:./output \
--goclay_out=impl_path=internal/api/example:./output \
--goclayvalid_out=pretty:./output \
example.proto;