Validate middleware uses proto-gen-validate generated code for parameter validation. You could write parameter validation rules in proto files and generate codes, in order to automatically parameter validation.
First you should install proto-gen-validate
go install github.com/envoyproxy/protoc-gen-validate@latestIf any error appears in generation process or there are // no validation rules for xxxx in the generated codes, you could try git clone github.com/envoyproxy/protoc-gen-validate then run make build
Here are some examples of parameter validation for several common situations, you may also refer to more examples in proto-gen-validate
// id must be greater than 0int64 id = 1 [(validate.rules).int64 = {gt: 0}];// age must be in the range (0, 120]int32 age = 2 [(validate.rules).int32 = {gt:0, lte: 120}];// code must be either 1, 2, or 3uint32 code = 3 [(validate.rules).uint32 = {in: [1,2,3]}];// score cannot be 0 nor 0.99float score = 1 [(validate.rules).float = {not_in: [0, 99.99]}];// state must be set to truebool state = 5 [(validate.rules).bool.const = true];// x cannot be set to truebool state = 5 [(validate.rules).bool.const = false];// x must be set to "/hello"string path = 6 [(validate.rules).string.const = "/hello"];// phone must be exactly 11 characters longstring phone = 7 [(validate.rules).string.len = 11];// explain must be at least 10 characters longstring explain = 8 [(validate.rules).string.min_len = 10];// name must be between 1 and 10 characters, inclusivestring name = 9 [(validate.rules).string = {min_len: 1, max_len: 10}];// card must be a non-empty, case-insensitive hexadecimal stringstring card = 10 [(validate.rules).string.pattern = "(?i)^[0-9a-f]+$"];// x must be a valid email address (via RFC 1034)string email = 11 [(validate.rules).string.email = true];// info cannot be unsetInfo info = 11 [(validate.rules).message.required = true];message Info { string address = 1;}1.Directly use the protoc command to generate
protoc --proto_path=. \ --proto_path=./third_party \ --go_out=paths=source_relative:. \ --validate_out=paths=source_relative,lang=go:. \ xxxx.proto2.Add the validate command in Makefile
.PHONY: validate# generate validate protovalidate: protoc --proto_path=. \ --proto_path=./third_party \ --go_out=paths=source_relative:. \ --validate_out=paths=source_relative,lang=go:. \ $(API_PROTO_FILES)Execute command
make validateWe can inject the validate middleware into HTTP or gRPC, and the validate middleware automatically validates the parameters according to the rules written in the proto when request entering.
httpSrv := http.NewServer( http.Address(":8000"), http.Middleware( validate.Validator(), ))grpcSrv := grpc.NewServer( grpc.Address(":9000"), grpc.Middleware( validate.Validator(), ))