Giter Club home page Giter Club logo

protoc-gen-atlas-validate's Introduction

protoc-gen-atlas-validate

The main purpose of this plugin is to generate a code under pb.atlas.validate.go that serves several purposes:

  • Ability to configure 'allow unknown fields' on several levels: per method, per service, per proto-file

  • Validate basic types.

  • (TBD-TODO) Possibly this can be transformed to a full-fledged ad-hoc JSON marshaller with per-service/method/file options similar to ones that OpenAPI provides.

Usage

Include following lines in your .proto file:

Import

import "github.com/infobloxopen/protoc-gen-atlas-validate/options/atlas_validate.proto";

Specifying options

Service option:

service Groups {
        option (atlas_validate.service).allow_unknown_fields = true;
        rpc Create(Group) returns (EmptyResponse) {
...

Method option:

        rpc Update(UpdateProfileRequest) returns (EmptyResponse) {
                option (atlas_validate.method).allow_unknown_fields = true;
                option (google.api.http) = {
                        put: "/profiles/{payload.id}";
                        body: "payload";
                };
        }
}

Global option:

option (atlas_validate.file).allow_unknown_fields = false;

Field option:

message User {
   //Field denied for create
   int64  id    = 1 [(atlas_validate.field).deny =  create];
   // Field denied for update and required for create and replace operations
   string name  = 2 [(atlas_validate.field).deny =  update, (atlas_validate.field) = {require: [create, replace]}]; 
   //Field denied for create, replace and update (ReadOnly access)
   string email = 3 [(atlas_validate.field) = {deny: [create, replace, update]}]; 
}

Generation

Note that this plugin heavily relies on patterns generated by protoc-gen-grpc-gateway plugin:

protoc -I/usr/local/include \
	-I. -I$GOPATH/src/ \
	-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway \
	-I./vendor \
	-I$GOPATH/src/github.com/googleapis \
		--grpc-gateway_out="logtostderr=true:$GOPATH/src" \
		--atlas-validate_out="$GOPATH/src" \
			<path-to-your-file>

The following will generate pb.atlas.validate.go file that contains validation logic and MetadataAnnotator that you will have to include in GRPC Server options.

Multiple Files Support

You can specify more than one file belonging to the same package. In this case plugin will generate validate_Object_ and validate_service_rpc_ in appropriate files with one difference that annotator and validate_Patterns are rendered either in a last output file or in file which name matches current package name (NOTE that you cannot specify files from different packages).

Usage

Import atlas-validate Interceptor:

import atlas_validate "github.com/infobloxopen/protoc-gen-atlas-validate/interceptor"

Add generated AtlasValidateAnnotator (from *.pb.atlas.validate.go) to a Metadata anotators:

gateway.WithGatewayOptions(
	runtime.WithMetadata(pb.AtlasValidateAnnotator),
)

Add interceptor that extracts error from metadata and returns it to a user:

gateway.WithDialOptions(
	[]grpc.DialOption{grpc.WithInsecure(), grpc.WithUnaryInterceptor(
		grpc_middleware.ChainUnaryClient(
			[]grpc.UnaryClientInterceptor{
				gateway.ClientUnaryInterceptor,
				atlas_validate.ValidationClientInterceptor(),
			},
		)
	)
)

protoc-gen-atlas-validate's People

Contributors

askurydzin avatar calebjh avatar evgeniy-l 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.