Giter Club home page Giter Club logo

protovalidate-python's Introduction

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

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.

protovalidate-python's People

Contributors

alfus avatar dependabot[bot] avatar elliotmjackson avatar buildbreaker avatar akshayjshah avatar marekbuild avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.