Package: protovalidate
protovalidate-pythonprotovalidate-pythonis the Python implementation ofprotovalidate,
designed to validate Protobuf messages at runtime based on user-defined validation constraints. Powered by Google's
Common Expression Language (CEL), it provides a flexible and efficient foundation
for defining and evaluating custom validation rules. The primary goal ofprotovalidateis to help developers ensure
data consistency and integrity across the network without requiring generated code.TheprotovalidateprojectHead over to the coreprotovalidaterepository for:The API definition:
used to describe validation constraintsDocumentation: how to applyprotovalidateeffectivelyMigration tooling: incrementally migrate
fromprotoc-gen-validateConformance testing utilities: for
acceptance testing ofprotovalidateimplementationsOtherprotovalidateruntime implementations include:Go:protovalidate-goC++:protovalidate-ccJava:protovalidate-javaAnd others coming soon:TypeScript:protovalidate-tsInstallationTo install the package, use pip:pipinstallprotovalidateMake sure you have the latest version ofprotovalidate-pythonby checking the
project'sPyPI page.UsageImplementing validation constraintsValidation constraints are defined directly within.protofiles. Documentation for adding constraints can be found in
theprotovalidateprojectREADMEand
itscomprehensive docs.syntax="proto3";packagemy.package;import"google/protobuf/timestamp.proto";import"buf/validate/validate.proto";messageTransaction{uint64id=1[(buf.validate.field).uint64.gt=999];google.protobuf.Timestamppurchase_date=2;google.protobuf.Timestampdelivery_date=3;stringprice=4[(buf.validate.field).cel={id:"transaction.price",message:"price must be positive and include a valid currency symbol ($ or £)",expression:"(this.startswith('$') or this.startswith('£')) and float(this[1:]) > 0"}];option(buf.validate.message).cel={id:"transaction.delivery_date",message:"delivery date must be after purchase date",expression:"this.delivery_date > this.purchase_date"};}Generating Code withbufWhen using the runtime library after installing it withpip, it's necessary to generate the Python code for the corebuf.protovalidateProtobuf package.bufprovides an efficient method for this:Initialize a New Configuration File:bufmodinitThis initializes thebuf.yamlconfiguration file at the root of the Protobuf source files.Module Configuration and Dependencies:# buf.yamlversion:v1deps:-buf.build/bufbuild/protovalidateEnsure your dependencies are up-to-date with:bufmodupdateSetup Code Generation:# buf.gen.yamlversion:v1plugins:-plugin:buf.build/protocolbuffers/python:v23.4out:genGenerate Code:
To generate the required Python code:bufgenerate--include-importsIf your goal is to generate code specifically for thebuf.protovalidateProtobuf package, run:bufgeneratebuf.build/bufbuild/protovalidateNote:For users familiar withprotoc, while it's an alternative tobuf, it is recommended to use tooling or frameworks like Bazel for direct code generation, as it provides an encapsulated environment for such tasks.Exampleimportprotovalidatefromgoogle.protobuf.timestamp_pb2importTimestampfrommy.packageimportTransactiontransaction=Transaction()transaction.id=1234transaction.price="$5.67"transaction.purchase_date.CopyFrom(Timestamp())transaction.delivery_date.CopyFrom(Timestamp())try:protovalidate.validate(transaction)exceptprotovalidate.ValidationErrorase:# Report the violationsEcosystemprotovalidatecore repositoryBufCEL SpecLegalOffered under theApache 2 license.