Skip to content

How to use protoc-gen-validate (PGV) with connect-go ? #426

Answered by s-takehana
s-takehana asked this question in Q&A
Discussion options

You must be logged in to vote
type validator interface {
	ValidateAll() error
}

func NewValidationInterceptor() connect.UnaryInterceptorFunc {
	interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
		return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
			if v, ok := req.Any().(validator); ok {
				if err := v.ValidateAll(); err != nil {
					return nil, connect.NewError(connect.CodeInvalidArgument, err)
				}
			}
			return next(ctx, req)
		})
	}
	return connect.UnaryInterceptorFunc(interceptor)
}

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@akshayjshah
Comment options

@ymmt2005
Comment options

@ymmt2005
Comment options

@akshayjshah
Comment options

@s-takehana
Comment options

Answer selected by s-takehana
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants