protoc-gen-atlas-validate

command module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

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(),
			},
		)
	)
)

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
example
examplepb
Package examplepb is a reverse proxy.
Package examplepb is a reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL