Install & Compatibility
Where this runs
tested against v2.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
py 3.9
✕ build_error
✕ build_error
36MB installed
● package 36MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
protovalidate
✓ import protovalidate
✗ import protovalidate
This quickstart demonstrates how to use `protovalidate` to validate a Protobuf message. It assumes you have a `.proto` file (e.g., `example.proto`) with `buf.validate` rules defined, and that you have compiled it into a Python module (e.g., `example_pb2.py`) using `protoc`. The example includes a mock `example_pb2` to make it runnable without prior compilation, but in a real scenario, you would rely on generated code. It shows how to import the `validate` function and check a message for violations.
import os
import sys
from pathlib import Path
# This quickstart assumes you have a proto file 'example.proto'
# that has been compiled to 'example_pb2.py' using protoc.
# For example, if example.proto contains:
#
# syntax = "proto3";
# package example;
# import buf/validate/validate.proto;
#
# message User {
# string name = 1 [(buf.validate.field).string.min_len = 1];
# int32 age = 2 [(buf.validate.field).int32.gt = 0];
# }
#
# You would compile it with:
# protoc --python_out=. --proto_path=. --proto_path=$(go env GOPATH)/src/github.com/bufbuild/protovalidate example.proto
# (or similar, ensuring validate.proto is in the proto path)
# For demonstration, we'll create a dummy 'example_pb2' module if not found
# In a real app, this module would be generated by protoc.
try:
# In a real application, this would be generated by protoc
# and imported normally. We include this workaround for a runnable quickstart.
if 'example_pb2' not in sys.modules:
# Mock a minimal pb2 module for demonstration if not generated
class User:
def __init__(self, name=None, age=None):
self.name = name
self.age = age
def SerializeToString(self):
# In real scenario, this would serialize the actual proto
return b''
def __str__(self):
return f"User(name='{self.name}', age={self.age})"
sys.modules['example_pb2'] = type('module', (object,), {'User': User})
from example_pb2 import User # This will now resolve to our mock User or actual generated one
from protovalidate import validate
# Valid user
valid_user = User(name="Alice", age=30)
violations_valid = validate(valid_user)
print(f"Valid User: {valid_user}")
if violations_valid:
for violation in violations_valid:
print(f" Violation: {violation.field_path} - {violation.message}")
else:
print(" No violations.")
print("\n---")
# Invalid user (empty name, negative age)
invalid_user = User(name="", age=-5)
violations_invalid = validate(invalid_user)
print(f"Invalid User: {invalid_user}")
if violations_invalid:
for violation in violations_invalid:
print(f" Violation: {violation.field_path} - {violation.message}")
else:
print(" No violations.")
except ImportError as e:
print(f"Error: Could not import protobuf-generated modules. Please ensure 'example.proto' is compiled to 'example_pb2.py'.\nDetails: {e}")
print("For a full setup, refer to the protovalidate-python documentation on generating protobuf files with validation rules.")
Upgrade
Version history
2.0.0latest on PyPI · released Aug 19, 2026
Audit
Dependencies
protobufrequiredRequired for Protobuf message serialization/deserialization and reflection.
cel-pythonrequiredRequired for evaluating Common Expression Language (CEL) rules defined in Protovalidate.
google-re2optionalOptional dependency for supporting RE2 regex syntax in validation rules; installed via `protovalidate[re2]`.