Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

go-proto-validators

> 编程语言
Open source

Generate message validators from .proto annotations.

1.1K stars0 likes0 views
WebsiteGitHub

About

Generate message validators from .proto annotations.

Golang ProtoBuf Validator Compiler

A protoc plugin that generates Validate() error functions on Go proto structs based on field options inside .proto files. The validation functions are code-generated and thus don't suffer on performance from tag-based reflection on deeply-nested messages.

Requirements

Using Protobuf validators is currently verified to work with:

  • Go 1.11, 1.12, 1.13
  • Protobuf @ v3.8.0
  • Go Protobuf @ v1.3.2
  • Gogo Protobuf @ v1.3.0

It should still be possible to use it in project using earlier Go versions. However if you want to contribute to this repository you'll need at least 1.11 for Go module support.

Paint me a code picture

Let's take the following proto3 snippet:

…

First, the required keyword is back for proto3, under the guise of msg_exists. The painful if-nil checks are taken care of!

Second, the expected values in fields are now part of the contract .proto file. No more hunting down conditions in code!

Third, the generated code is understandable and has clear understandable error messages. Take a look:

…

Installing and using

The protoc compiler expects to find plugins named proto-gen-XYZ on the execution $PATH. So first:

bash
export PATH=${PATH}:${GOPATH}/bin

Then, do the usual

bash
go get github.com/mwitkow/go-proto-validators/protoc-gen-govalidators

Your protoc builds probably look very simple like:

bash
protoc  \
  --proto_path=. \
  --go_out=. \
  *.proto

That's fine, until you encounter .proto includes. Because go-proto-validators uses field options inside the .proto files themselves, it's .proto definition (and the Google descriptor.proto itself) need to on the protoc include path. Hence the above becomes:

bash
protoc  \
  --proto_path=${GOPATH}/src \
  --proto_path=${GOPATH}/src/github.com/google/protobuf/src \
  --proto_path=. \
  --go_out=. \
  --govalidators_out=. \
  *.proto

Or with gogo protobufs:

bash
protoc  \
  --proto_path=${GOPATH}/src \
  --proto_path=${GOPATH}/src/github.com/gogo/protobuf/protobuf \
  --proto_path=. \
  --gogo_out=. \
  --govalidators_out=gogoimport=true:. \
  *.proto

Basically the magical incantation (apart from includes) is the --govalidators_out. That triggers the protoc-gen-govalidators plugin to generate mymessage.validator.pb.go. That's it :)

License

go-proto-validators is released under the Apache 2.0 license. See the LICENSE file for details.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Gogogolangprotoprotobuf

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言