validator

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: Apache-2.0 Imports: 5 Imported by: 1

README

Golang ProtoBuf Validator Compiler

Travis Build Apache 2.0 License

A protoc plugin that generates Validate() error functions on Go proto structs based on field options inside .proto files. The validation functions are code-generated and thus don't suffer on performance from tag-based reflection on deeply-nested messages.

Requirements

Using Protobuf validators is currently verified to work with:

It should still be possible to use it in project using earlier Go versions. However if you want to contribute to this repository you'll need at least 1.11 for Go module support.

Paint me a code picture

Let's take the following proto3 snippet:

syntax = "proto3";
package validator.examples;
import "github.com/maanasasubrahmanyam-sd/go-proto-validators/validator.proto";

message InnerMessage {
  // some_integer can only be in range (0, 100).
  int32 some_integer = 1 [(validator.field) = {int_gt: 0, int_lt: 100}];
  // some_float can only be in range (0;1).
  double some_float = 2 [(validator.field) = {float_gte: 0, float_lte: 1}];
}

message OuterMessage {
  // important_string must be a lowercase alpha-numeric of 5 to 30 characters (RE2 syntax).
  string important_string = 1 [(validator.field) = {regex: "^[a-z0-9]{5,30}$"}];
  // proto3 doesn't have `required`, the `msg_exist` enforces presence of InnerMessage.
  InnerMessage inner = 2 [(validator.field) = {msg_exists : true}];
}

First, the required keyword is back for proto3, under the guise of msg_exists. The painful if-nil checks are taken care of!

Second, the expected values in fields are now part of the contract .proto file. No more hunting down conditions in code!

Third, the generated code is understandable and has clear understandable error messages. Take a look:

func (this *InnerMessage) Validate() error {
	if !(this.SomeInteger > 0) {
		return fmt.Errorf("validation error: InnerMessage.SomeInteger must be greater than '0'")
	}
	if !(this.SomeInteger < 100) {
		return fmt.Errorf("validation error: InnerMessage.SomeInteger must be less than '100'")
	}
	if !(this.SomeFloat >= 0) {
		return fmt.Errorf("validation error: InnerMessage.SomeFloat must be greater than or equal to '0'")
	}
	if !(this.SomeFloat <= 1) {
		return fmt.Errorf("validation error: InnerMessage.SomeFloat must be less than or equal to '1'")
	}
	return nil
}

var _regex_OuterMessage_ImportantString = regexp.MustCompile("^[a-z0-9]{5,30}$")

func (this *OuterMessage) Validate() error {
	if !_regex_OuterMessage_ImportantString.MatchString(this.ImportantString) {
		return fmt.Errorf("validation error: OuterMessage.ImportantString must conform to regex '^[a-z0-9]{5,30}$'")
	}
	if nil == this.Inner {
		return fmt.Errorf("validation error: OuterMessage.Inner message must exist")
	}
	if this.Inner != nil {
		if err := validators.CallValidatorIfExists(this.Inner); err != nil {
			return err
		}
	}
	return nil
}

Installing and using

The protoc compiler expects to find plugins named proto-gen-XYZ on the execution $PATH. So first:

export PATH=${PATH}:${GOPATH}/bin

Then, do the usual

go get github.com/maanasasubrahmanyam-sd/go-proto-validators/protoc-gen-govalidators

Your protoc builds probably look very simple like:

protoc  \
  --proto_path=. \
  --go_out=. \
  *.proto

That's fine, until you encounter .proto includes. Because go-proto-validators uses field options inside the .proto files themselves, it's .proto definition (and the Google descriptor.proto itself) need to on the protoc include path. Hence the above becomes:

protoc  \
  --proto_path=${GOPATH}/src \
  --proto_path=${GOPATH}/src/github.com/google/protobuf/src \
  --proto_path=. \
  --go_out=. \
  --govalidators_out=. \
  *.proto

Or with gogo protobufs:

protoc  \
  --proto_path=${GOPATH}/src \
  --proto_path=${GOPATH}/src/github.com/gogo/protobuf/protobuf \
  --proto_path=. \
  --gogo_out=. \
  --govalidators_out=gogoimport=true:. \
  *.proto

Basically the magical incantation (apart from includes) is the --govalidators_out. That triggers the protoc-gen-govalidators plugin to generate mymessage.validator.pb.go. That's it :)

License

go-proto-validators is released under the Apache 2.0 license. See the LICENSE file for details.

maanasa@Maanasas-MacBook-Air go-proto-validators % go get github.com/gogo/protobuf/protoc-gen-gogo@v1.3.0 go get: downgraded github.com/gogo/protobuf v1.3.2 => v1.3.0 maanasa@Maanasas-MacBook-Air go-proto-validators % export PATH="$PATH:$(go env GOPATH)/bin" maanasa@Maanasas-MacBook-Air go-proto-validators % GO111MODULE=on PROTOBUF_VERSION=3.10.0 make regenerate

export PATH=""/Users/maanasa/go-workspace/src/github.com/maanasasubrahmanyam-sd/go-proto-validators/"deps/bin:/Users/maanasa/go/bin":${PATH}; protoc
--proto_path=deps
--proto_path=deps/include
--proto_path=deps/github.com/gogo/protobuf/protobuf
--proto_path=.
--gogo_out=Mgoogle/protobuf/descriptor.proto=github.com/gogo/protobuf/protoc-gen-gogo/descriptor:.
validator.proto

protoc
--proto_path=deps
--proto_path=deps/include
--proto_path=deps/github.com/gogo/protobuf@v1.3.0/protobuf
--proto_path=.
--gogo_out=Mdescriptor.proto=:.
validator.proto

Documentation

Index

Constants

This section is empty.

Variables

View Source
var E_Field = &proto.ExtensionDesc{
	ExtendedType:  (*descriptor.FieldOptions)(nil),
	ExtensionType: (*FieldValidator)(nil),
	Field:         65020,
	Name:          "validator.field",
	Tag:           "bytes,65020,opt,name=field",
	Filename:      "validator.proto",
}

Functions

func CallValidatorIfExists

func CallValidatorIfExists(candidate interface{}) error

func FieldError

func FieldError(fieldName string, err error) error

FieldError wraps a given Validator error providing a message call stack.

Types

type FieldValidator

type FieldValidator struct {
	Alpha                *bool    `protobuf:"varint,1,opt,name=alpha" json:"alpha,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*FieldValidator) Descriptor

func (*FieldValidator) Descriptor() ([]byte, []int)

func (*FieldValidator) GetAlpha

func (m *FieldValidator) GetAlpha() bool

func (*FieldValidator) ProtoMessage

func (*FieldValidator) ProtoMessage()

func (*FieldValidator) Reset

func (m *FieldValidator) Reset()

func (*FieldValidator) String

func (m *FieldValidator) String() string

func (*FieldValidator) XXX_DiscardUnknown

func (m *FieldValidator) XXX_DiscardUnknown()

func (*FieldValidator) XXX_Marshal

func (m *FieldValidator) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*FieldValidator) XXX_Merge

func (m *FieldValidator) XXX_Merge(src proto.Message)

func (*FieldValidator) XXX_Size

func (m *FieldValidator) XXX_Size() int

func (*FieldValidator) XXX_Unmarshal

func (m *FieldValidator) XXX_Unmarshal(b []byte) error

type Validator

type Validator interface {
	Validate() error
}

Validator is a general interface that allows a message to be validated.

Directories

Path Synopsis
github.com

Jump to

Keyboard shortcuts

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