{"id":3220,"library":"protovalidate","title":"Protocol Buffer Validation for Python","description":"protovalidate is a Python library that provides Protocol Buffer validation based on Buf's Protovalidate specification. It allows defining validation rules directly in `.proto` files using custom options and then enforcing these rules at runtime within Python applications. The current version is 1.1.2, and it follows a frequent release cadence, often aligning with the core Protovalidate specification releases and `protobuf` runtime updates.","status":"active","version":"1.1.2","language":"en","source_language":"en","source_url":"https://github.com/bufbuild/protovalidate-python","tags":["protobuf","validation","data validation","buf","schema validation"],"install":[{"cmd":"pip install protovalidate","lang":"bash","label":"Base installation"},{"cmd":"pip install protovalidate[re2]","lang":"bash","label":"With RE2 regex support"}],"dependencies":[{"reason":"Required for Protobuf message serialization/deserialization and reflection.","package":"protobuf","optional":false},{"reason":"Required for evaluating Common Expression Language (CEL) rules defined in Protovalidate.","package":"cel-python","optional":false},{"reason":"Optional dependency for supporting RE2 regex syntax in validation rules; installed via `protovalidate[re2]`.","package":"google-re2","optional":true}],"imports":[{"note":"The primary validation function is directly exposed at the top level of the package since v0.15.0.","wrong":"from protovalidate.validator import validate","symbol":"validate","correct":"from protovalidate import validate"}],"quickstart":{"code":"import os\nimport sys\nfrom pathlib import Path\n\n# This quickstart assumes you have a proto file 'example.proto'\n# that has been compiled to 'example_pb2.py' using protoc.\n# For example, if example.proto contains:\n#\n# syntax = \"proto3\";\n# package example;\n# import buf/validate/validate.proto;\n#\n# message User {\n#   string name = 1 [(buf.validate.field).string.min_len = 1];\n#   int32 age = 2 [(buf.validate.field).int32.gt = 0];\n# }\n#\n# You would compile it with:\n# protoc --python_out=. --proto_path=. --proto_path=$(go env GOPATH)/src/github.com/bufbuild/protovalidate example.proto\n# (or similar, ensuring validate.proto is in the proto path)\n\n# For demonstration, we'll create a dummy 'example_pb2' module if not found\n# In a real app, this module would be generated by protoc.\n\ntry:\n    # In a real application, this would be generated by protoc\n    # and imported normally. We include this workaround for a runnable quickstart.\n    if 'example_pb2' not in sys.modules:\n        # Mock a minimal pb2 module for demonstration if not generated\n        class User:\n            def __init__(self, name=None, age=None):\n                self.name = name\n                self.age = age\n\n            def SerializeToString(self):\n                # In real scenario, this would serialize the actual proto\n                return b''\n\n            def __str__(self):\n                return f\"User(name='{self.name}', age={self.age})\"\n        sys.modules['example_pb2'] = type('module', (object,), {'User': User})\n\n    from example_pb2 import User # This will now resolve to our mock User or actual generated one\n    from protovalidate import validate\n\n    # Valid user\n    valid_user = User(name=\"Alice\", age=30)\n    violations_valid = validate(valid_user)\n    print(f\"Valid User: {valid_user}\")\n    if violations_valid:\n        for violation in violations_valid:\n            print(f\"  Violation: {violation.field_path} - {violation.message}\")\n    else:\n        print(\"  No violations.\")\n\n    print(\"\\n---\")\n\n    # Invalid user (empty name, negative age)\n    invalid_user = User(name=\"\", age=-5)\n    violations_invalid = validate(invalid_user)\n    print(f\"Invalid User: {invalid_user}\")\n    if violations_invalid:\n        for violation in violations_invalid:\n            print(f\"  Violation: {violation.field_path} - {violation.message}\")\n    else:\n        print(\"  No violations.\")\n\nexcept ImportError as e:\n    print(f\"Error: Could not import protobuf-generated modules. Please ensure 'example.proto' is compiled to 'example_pb2.py'.\\nDetails: {e}\")\n    print(\"For a full setup, refer to the protovalidate-python documentation on generating protobuf files with validation rules.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Pass `fail_fast` directly as a keyword argument to `protovalidate.validate()` or `protovalidate.collect_validations()`. The `into` argument for `collect_validations` is no longer supported; collect violations directly.","message":"The `protovalidate.Config` class was removed and the `into` argument for `collect_validations` was removed.","severity":"breaking","affected_versions":">=0.15.0"},{"fix":"Install `protovalidate` with the `[re2]` extra: `pip install protovalidate[re2]`. Custom regex functions are no longer supported via configuration.","message":"The `regex_matches_func` configuration option was removed. RE2 regular expression syntax now requires the `google-re2` dependency.","severity":"breaking","affected_versions":">=0.14.0"},{"fix":"Update your `.proto` files to use `(buf.validate.field).ignore_if_zero_value = true` instead of `ignore_if_unpopulated`.","message":"The `IGNORE_IF_UNPOPULATED` rule option in `.proto` files was renamed to `IGNORE_IF_ZERO_VALUE`.","severity":"breaking","affected_versions":">=0.13.0"},{"fix":"Ensure your environment uses Python 3.10 or newer. Upgrade your Python installation if necessary.","message":"Python 3.9 support was dropped.","severity":"gotcha","affected_versions":">=1.1.0"},{"fix":"Upgrade to `protovalidate` version `1.1.2` or newer to ensure full compatibility and correct behavior with `protobuf==7`.","message":"Compatibility issues with `protobuf==7` existed in versions prior to `1.1.2`.","severity":"gotcha","affected_versions":"<1.1.2"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}