protovalidate

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: Apache-2.0 Imports: 9 Imported by: 3

Documentation

Overview

Package protovalidate is a request validator that uses https://github.com/bufbuild/protovalidate-go under the hood.

In case of a validation failure, an `InvalidArgument` gRPC status is returned, along with a description of the validation failure.

It supports two ways of work:

1. use new annotations that will be catched and processed by `protovalidate-go` package.

2. use legacy mode, annotations will be same as for `protoc-gen-validate` and `Validate()` method will be generated.

Example of a service:

syntax = "proto3";
package cloud.instance.v1;

import "buf/validate/validate.proto";
import "validate/validate.proto";

service InstanceService {
 // GetInstance is an example of request that uses a new constraints
 rpc GetInstance(GetInstanceRequest) returns (GetInstanceResponse) {}

 // Legacy is an example of request that uses protoc-gen-validate constraints
 // their support enabled in protovalidate library constructor
 rpc Legacy(LegacyRequest) returns (LegacyResponse) {}
}

message GetInstanceRequest {
 string instance_id = 1 [(buf.validate.field).string.uuid = true];
}

message GetInstanceResponse {}

message LegacyRequest {
 string email = 1 [(validate.rules).string.email = true]; // https://github.com/bufbuild/protoc-gen-validate
}

message LegacyResponse {}

Please consult https://github.com/bufbuild/protovalidate for details and other parameters of customization.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamServerInterceptor

func StreamServerInterceptor(validator *protovalidate.Validator, opts ...Option) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new streaming server interceptor that validates incoming messages.

Example
package main

import (
	"net"

	"github.com/bufbuild/protovalidate-go"
	protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
	testvalidatev1 "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate/v1"
	"google.golang.org/grpc"
)

type StreamService struct {
	testvalidatev1.TestValidateServiceServer
}

func (s *StreamService) SendStream(_ *testvalidatev1.SendStreamRequest, _ testvalidatev1.TestValidateService_SendStreamServer) error {
	return nil
}

func main() {
	validator, err := protovalidate.New()
	if err != nil {
		panic(err) // only for example purposes
	}

	var (
		srv = grpc.NewServer(
			grpc.StreamInterceptor(
				protovalidate_middleware.StreamServerInterceptor(validator,
					protovalidate_middleware.WithIgnoreMessages(
						(&testvalidatev1.SendStreamRequest{}).ProtoReflect().Type(),
					)),
			),
		)
		svc = &StreamService{}
	)

	testvalidatev1.RegisterTestValidateServiceServer(srv, svc)

	listener, err := net.Listen("tcp", ":3000")
	if err != nil {
		panic(err) // only for example purposes
	}

	if err = srv.Serve(listener); err != nil {
		panic(err) // only for example purposes
	}
}
Output:

func UnaryServerInterceptor

func UnaryServerInterceptor(validator *protovalidate.Validator, opts ...Option) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor that validates incoming messages.

Example
package main

import (
	"context"
	"net"

	"github.com/bufbuild/protovalidate-go"
	protovalidate_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
	testvalidatev1 "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testvalidate/v1"
	"google.golang.org/grpc"
)

type UnaryService struct {
	testvalidatev1.TestValidateServiceServer
}

func (s *UnaryService) Send(_ context.Context, _ *testvalidatev1.SendRequest) (*testvalidatev1.SendResponse, error) {
	return &testvalidatev1.SendResponse{}, nil
}

func main() {
	validator, err := protovalidate.New()
	if err != nil {
		panic(err) // only for example purposes
	}

	var (
		srv = grpc.NewServer(
			grpc.UnaryInterceptor(
				protovalidate_middleware.UnaryServerInterceptor(validator,
					protovalidate_middleware.WithIgnoreMessages(
						(&testvalidatev1.SendRequest{}).ProtoReflect().Type(),
					),
				),
			),
		)
		svc = &UnaryService{}
	)

	testvalidatev1.RegisterTestValidateServiceServer(srv, svc)

	listener, err := net.Listen("tcp", ":3000")
	if err != nil {
		panic(err) // only for example purposes
	}

	if err = srv.Serve(listener); err != nil {
		panic(err) // only for example purposes
	}
}
Output:

Types

type Option

type Option func(*options)

An Option lets you add options to protovalidate interceptors using With* funcs.

func WithIgnoreMessages added in v2.1.0

func WithIgnoreMessages(msgs ...protoreflect.MessageType) Option

WithIgnoreMessages sets the messages that should be ignored by the validator. Use with caution and ensure validation is performed elsewhere.

Jump to

Keyboard shortcuts

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