Skip to main content

Protocol Buffer Validation for Python

Project description

protovalidate-python

CI Conformance PyPI version

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:

Other protovalidate runtime implementations include:

And others coming soon:

  • 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"
  };
}

Generating Code with buf

When using the runtime library after installing it with pip, it's necessary to generate the Python code for the core buf.protovalidate Protobuf package. buf provides an efficient method for this:

  1. Initialize a New Configuration File:

    buf mod init
    

    This initializes the buf.yaml configuration file at the root of the Protobuf source files.

  2. Module Configuration and Dependencies:

    # buf.yaml
    version: v1
    deps: 
      - buf.build/bufbuild/protovalidate
    

    Ensure your dependencies are up-to-date with:

    buf mod update
    
  3. Setup Code Generation:

    # buf.gen.yaml
    version: v1
    plugins:
      - plugin: buf.build/protocolbuffers/python:v23.4
        out: gen
    
  4. Generate Code: To generate the required Python code:

    buf generate --include-imports
    
  5. Specify import paths: Ensure that the generated code is importable by setting the PYTHONPATH environment variable:

    export PYTHONPATH=$PYTHONPATH:gen
    

If your goal is to generate code specifically for the buf.protovalidate Protobuf package, run:

buf generate buf.build/bufbuild/protovalidate

Note: For users familiar with protoc, while it's an alternative to buf, it is recommended to use tooling or frameworks like Bazel for direct code generation, as it provides an encapsulated environment for such tasks.

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

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

protovalidate-0.5.0.tar.gz (118.6 kB view details)

Uploaded Source

Built Distribution

protovalidate-0.5.0-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file protovalidate-0.5.0.tar.gz.

File metadata

  • Download URL: protovalidate-0.5.0.tar.gz
  • Upload date:
  • Size: 118.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for protovalidate-0.5.0.tar.gz
Algorithm Hash digest
SHA256 24d9cc385fa952635e39d97d48ed7f903e4e9c96771452135dc7dc013a1b5896
MD5 b305d2f513d349bc12705939a6b45fae
BLAKE2b-256 1a34d884adcf99745b8790c7082b0b6cb8d8cc64c3f2f5f70f69f1982883ef5c

See more details on using hashes here.

File details

Details for the file protovalidate-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for protovalidate-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e7beb807145ac1a16e25d804c134545f6f1d5ba30e6e6a08a3bfa530c3ad5b3
MD5 20e69deda145e907b574e5db9e5fc7b5
BLAKE2b-256 964593739c387233949c607e035dec7fdb460f60fee9d3a678c862c0d7db2808

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page