Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protovalidate: add option to ignore certain message types #684

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions interceptors/protovalidate/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

// Copyright 2017 David Ackroyd. All Rights Reserved.
// See LICENSE for licensing terms.

package protovalidate

import (
"golang.org/x/exp/slices"
"google.golang.org/protobuf/reflect/protoreflect"
)

type options struct {
ignoreMessages []protoreflect.MessageType
}

type Option func(*options)
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved

func evaluateOpts(opts []Option) *options {
optCopy := &options{}
for _, o := range opts {
o(optCopy)
}
return optCopy
}

// WithIgnoreMessages sets the messages that should be ignored as they are not yet ready
// for validation at the stage this middleware operates
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
func WithIgnoreMessages(msgs ...protoreflect.MessageType) Option {
return func(o *options) {
o.ignoreMessages = msgs
}
}

func (o *options) shouldIgnoreMessage(m protoreflect.MessageType) bool {
return slices.ContainsFunc(o.ignoreMessages, func(t protoreflect.MessageType) bool {
return m == t
})
}
10 changes: 4 additions & 6 deletions interceptors/protovalidate/protovalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (
"google.golang.org/protobuf/proto"
)

// Option interface is currently empty and serves as a placeholder for potential future implementations.
// It allows adding new options without breaking existing code.
type Option interface {
unimplemented()
}

// UnaryServerInterceptor returns a new unary server interceptor that validates incoming messages.
func UnaryServerInterceptor(validator *protovalidate.Validator, opts ...Option) grpc.UnaryServerInterceptor {
return func(
Expand All @@ -28,8 +22,12 @@ func UnaryServerInterceptor(validator *protovalidate.Validator, opts ...Option)
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (resp interface{}, err error) {
o := evaluateOpts(opts)
switch msg := req.(type) {
case proto.Message:
if o.shouldIgnoreMessage(msg.ProtoReflect().Type()) {
break
}
if err = validator.Validate(msg); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
Expand Down
14 changes: 14 additions & 0 deletions interceptors/protovalidate/protovalidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ func TestUnaryServerInterceptor(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, status.Code(err))
})

interceptor = protovalidate_middleware.UnaryServerInterceptor(validator,
protovalidate_middleware.WithIgnoreMessages(testvalidate.BadUnaryRequest.ProtoReflect().Type()),
)

t.Run("invalid_email_ignored", func(t *testing.T) {
info := &grpc.UnaryServerInfo{
FullMethod: "FakeMethod",
}

resp, err := interceptor(context.TODO(), testvalidate.BadUnaryRequest, info, handler)
assert.Nil(t, err)
assert.Equal(t, resp, "good")
})
}

type server struct {
Expand Down
Loading