Skip to main content

grpc proto validator.

Project description

plugin of validate request field for python grpc server.

https://travis-ci.com/v1c77/py_grpc_validator.svg?branch=master https://codecov.io/gh/v1c77/py_grpc_validator/branch/master/graph/badge.svg
TODO:
  • type hint for different field type.(such as only string field can do a string length check.)

  • test case & Makefile.

  • a cli to help protoc with validator.proto.

  • anymore bugs or improves?

How To

  1. install

pip install grpc-proto-validator

2. copy validator.proto to your project proto path and do protoc.(TODO a cli help to copy or build proto.)

The validator.proto is included in this package, you can find it in python site-packages path.

A example on how to generate the grpc files:

$ python -m grpc_tools.protoc \
  --proto_path={/path/to/validator/proto} \
  --proto_path={/path/to/your/project/proto} \
  --python_out={/path/to/project/src} \
  --grpc_python_out={/path/to/project/src} \
  path/to/your.proto
  1. change your service and *_pb2 code.

To avoid protobuf import error, change one line in the generated python file(like hello_bro_pb2.py).

from:

import validator_pb2 as validator__pb2

to:

import grpc_proto_validator.validator_pb2 as validator__pb2.

Then create service with validator:

import six
from grpc_validator.validator import ValidateMetaclass

class YourService(six.with_metaclass(ValidateMetaclass,
                         YourService_pb2_grpc.YourServiceServer)):

    def behaviour1(self, request, context)
        ...

Also, you can select individual service handler to do the field check:

from grpc_validator.validator import validator_wrap


class YourService(YourService_pb2_grpc.YourServiceServer):

    def behaviour(self, request, context):
        """request will not be validated."""
        ...

    @validator_wrap
    def behaviour2(self, request, context):
        ...

Notice

Someone has used go-proto-validator before will notice that I almost reuse its proto file which make it easier to co-work with our golang service team.

But please don’t mix these two proto files. !!!THEY ARE NOT IN COMMON USE!!!

Any help or idea is favourably receive. Please just make a issue.

LICENSE

MIT.

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

grpc-proto-validator-0.1.2.dev0.tar.gz (9.5 kB view hashes)

Uploaded Source

Built Distribution

grpc_proto_validator-0.1.2.dev0-py2.py3-none-any.whl (7.7 kB view hashes)

Uploaded Python 2 Python 3

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