Protocol Buffer Validation for Python
Project description
protovalidate-python
protovalidate-python
is the Python implementation of protovalidate
, 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 of protovalidate
is to help developers ensure data consistency and integrity across the network without requiring generated code.
The protovalidate
project
Head over to the core protovalidate
repository for:
- The API definition: used to describe validation constraints
- Documentation: how to apply
protovalidate
effectively - Migration tooling: incrementally migrate from
protoc-gen-validate
- Conformance testing utilities: for acceptance testing of
protovalidate
implementations
Other protovalidate
runtime implementations include:
- Go:
protovalidate-go
- C++:
protovalidate-cc
And others coming soon:
- Java:
protovalidate-java
- TypeScript:
protovalidate-ts
Installation
To install the package, use pip:
pip install protovalidate
Make sure you have the latest version of protovalidate-python
by checking the project's PyPI page.
Usage
Implementing validation constraints
Validation constraints are defined directly within .proto
files. Documentation for adding constraints can be found in the protovalidate
project README and its comprehensive docs.
syntax = "proto3";
package my.package;
import "google/protobuf/timestamp.proto";
import "buf/validate/validate.proto";
message Transaction {
uint64 id = 1 [(buf.validate.field).uint64.gt = 999];
google.protobuf.Timestamp purchase_date = 2;
google.protobuf.Timestamp delivery_date = 3;
string price = 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"
};
}
Example
import protovalidate
from google.protobuf.timestamp_pb2 import Timestamp
from my.package import Transaction
transaction = Transaction()
transaction.id = 1234
transaction.price = "$5.67"
transaction.purchase_date.CopyFrom(Timestamp())
transaction.delivery_date.CopyFrom(Timestamp())
try:
protovalidate.validate(transaction)
except protovalidate.ValidationError as e:
# Report the violations
Ecosystem
protovalidate
core repository- Buf
- CEL Spec
Legal
Offered under the Apache 2 license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for protovalidate-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f91192e2933613c19cefb3fca824e3074d50cac4c338a719ad5e3b7364257df9 |
|
MD5 | 00d3f00d2fc65401d15cd12e1bca78e9 |
|
BLAKE2b-256 | 0b3da261933157f19c34bb21d78d5d7bd2ebef488fe9525425e0dfa27dd16731 |