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.
Download validate.proto
Section titled “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:
$ 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.protoImport validate.proto
Section titled “Import validate.proto”Now that Protovalidate is on the filesystem, you can import it within Protobuf files:
syntax = "proto3";
package bufbuild.weather.v1;
import "buf/validate/validate.proto";Update Makefile (optional)
Section titled “Update Makefile (optional)”If you use a Makefile to compile with protoc, update it to include Protovalidate:
.PHONY: protocprotoc: protoc proto/example/v1/building_example.proto \ -I. -I./vendor/github.com/bufbuild/protovalidate/ \ --cpp_out=:./genCompile .proto files
Section titled “Compile .proto files”With validate.proto in the vendor directory and imported within .proto files, you can now compile with protoc’s -I flag:
$ protoc proto/example/v1/building_example.proto \ -I. -I./vendor/github.com/bufbuild/protovalidate/ \ --cpp_out=:./gen$ echo $?0