Protoc plugin to generate polyglot message validators for protocol buffers
pkg:github/bufbuild/protoc-gen-validate@1.2.x
npx @tessl/cli install tessl/go-protoc-gen-validate@1.2.0protoc-gen-validate (PGV) is a protoc plugin that generates polyglot message validators for protocol buffer messages. It enables semantic validation rules for protocol buffer fields that cannot be expressed through basic type constraints alone, supporting Go, Java, C++, and Python with generated validation methods.
For validation rules in proto files:
import "validate/validate.proto";For Python runtime validation:
from protoc_gen_validate.validator import validate, ValidationFailedsyntax = "proto3";
package example;
import "validate/validate.proto";
message User {
uint64 id = 1 [(validate.rules).uint64.gt = 999];
string email = 2 [(validate.rules).string.email = true];
string name = 3 [(validate.rules).string = {
pattern: "^[A-Za-z]+( [A-Za-z]+)*$",
max_bytes: 256,
}];
}protoc \
-I . \
-I path/to/validate/ \
--go_out="./generated" \
--validate_out="lang=go:./generated" \
user.protouser := &User{
Id: 1000,
Email: "user@example.com",
Name: "John Doe",
}
if err := user.Validate(); err != nil {
// Handle validation error
}PGV uses a plugin architecture built on the protoc-gen-star framework:
Provides the main protoc plugin functionality for code generation.
// Main entry point for protoc plugin
func main()
// Core module interface
type Module struct {
*pgs.ModuleBase
ctx pgsgo.Context
lang string
}
func Validator() pgs.Module
func ValidatorForLanguage(lang string) pgs.ModuleDefines comprehensive validation rules for all protocol buffer field types.
// Main field rules container
message FieldRules {
optional MessageRules message = 17;
oneof type {
FloatRules float = 1;
DoubleRules double = 2;
Int32Rules int32 = 3;
// ... all numeric types
StringRules string = 14;
BytesRules bytes = 15;
EnumRules enum = 16;
RepeatedRules repeated = 18;
MapRules map = 19;
AnyRules any = 20;
DurationRules duration = 21;
TimestampRules timestamp = 22;
}
}
// Protocol buffer extensions
extend google.protobuf.FieldOptions {
optional FieldRules rules = 1071;
}Template system for generating validation code in multiple languages.
type RegisterFn func(tpl *template.Template, params pgs.Parameters)
type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath
func Template(params pgs.Parameters) map[string][]*template.Template
func FilePathFor(tpl *template.Template) FilePathFnAPIs provided by generated validation code in each target language.
// Go generated methods
func (m *Message) Validate() error
func (m *Message) ValidateAll() error// Java validation interface
class ReflectiveValidatorIndex {
Validator validatorFor(Class<?> clazz)
}# Python runtime validation
def validate(msg) -> None # raises ValidationFailed
def print_validate(msg) -> NoneLanguage-specific runtime libraries and utilities for validation.
class ValidationFailed(Exception):
"""Raised when message validation fails"""
passValidate() and ValidateAll() methods// Module represents the core PGV module
type Module struct {
*pgs.ModuleBase
ctx pgsgo.Context
lang string
}
// Template registration function
type RegisterFn func(tpl *template.Template, params pgs.Parameters)
// File path generation function
type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath// Core validation rule types
message FieldRules { /* see validation rules schema */ }
message StringRules { /* string validation rules */ }
message MessageRules { /* message validation rules */ }
message RepeatedRules { /* repeated field validation rules */ }
message MapRules { /* map field validation rules */ }class ValidationFailed(Exception):
"""Exception raised when validation fails"""
pass