Compile with protoc
Though we recommend using Buf to compile your Protobuf files, you can use Protovalidate with protoc. However, without the Buf CLI, you won't be able to use linting to validate your rules.
This page describes how to add Protovalidate as a dependency and then compile Protovalidate-enabled files with protoc.
Code available
Companion code for this page is available in GitHub.
Download validate.proto
Protovalidate's source .proto
file is available in GitHub. Follow the steps below to use it with protoc
. If you already know how to vendor and include Protobuf dependencies, you'll notice that this is no different from any other dependency.
First, download validate.proto
to the vendor
directory:
Download validate.proto as a vendored file
$ curl --create-dirs \
-O \
--output-dir ./vendor/github.com/bufbuild/protovalidate/buf/validate \
https://raw.githubusercontent.com/bufbuild/protovalidate/refs/heads/main/proto/protovalidate/buf/validate/validate.proto
Import validate.proto
Now that Protovalidate is on the filesystem, you can import it within Protobuf files:
Import buf/validate/validate.proto
syntax = "proto3";
package bufbuild.weather.v1;
import "buf/validate/validate.proto";
Update Makefile (optional)
If you use a Makefile to compile with protoc
, update it to include Protovalidate:
Updating Makefile to include Protovalidate
.PHONY: protoc
protoc:
protoc proto/example/v1/building_example.proto \
+ -I. -I./vendor/github.com/bufbuild/protovalidate/ \
--cpp_out=:./gen
Compile .proto files
With validate.proto
in the vendor
directory and imported within .proto
files, you can now compile with protoc
's -I
flag:
Including Protovalidate protoc
protoc proto/example/v1/building_example.proto \
-I. -I./vendor/github.com/bufbuild/protovalidate/ \
--cpp_out=:./gen
echo $?
0