Documentation
¶
Overview ¶
Package validate provides connect-go v2 interceptors that validate messages against constraints specified in their Protobuf schemas. Because the interceptors are powered by protovalidate, validation is flexible, efficient, and consistent across languages - without additional code generation.
Server-side use:
server := connect.NewServer(validate.NewServerInterceptor())
Client-side use (discouraged unless the client always has an up-to-date schema):
client := pingv1connect.NewPingServiceClient(
connect.NewClient(transport, validate.NewClientInterceptor()),
)
For connect-go v1, use NewInterceptor.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewClientInterceptor ¶ added in v0.8.0
func NewClientInterceptor(opts ...Option) connect.ClientInterceptor
NewClientInterceptor returns a [connect.ClientInterceptor] that validates request messages sent to the server. When WithValidateResponses is set, it also validates response messages received from the server.
Client-side use is discouraged unless the client always has an up-to-date schema; prefer running validation on the server.
Invalid request messages short-circuit with [connect.CodeInvalidArgument]. Invalid response messages short-circuit with [connect.CodeInternal]. Both errors carry a detailed representation of the error as an error detail unless WithoutErrorDetails is set.
func NewServerInterceptor ¶ added in v0.8.0
func NewServerInterceptor(opts ...Option) connect.ServerInterceptor
NewServerInterceptor returns a [connect.ServerInterceptor] that validates request messages received from the client. When WithValidateResponses is set, it also validates response messages sent by the server.
Invalid request messages short-circuit with [connect.CodeInvalidArgument]. Invalid response messages short-circuit with [connect.CodeInternal]. Both errors carry a detailed representation of the error as an error detail unless WithoutErrorDetails is set.
Types ¶
type Interceptor ¶
type Interceptor struct {
// contains filtered or unexported fields
}
Interceptor is a connect-go v1 [connect.Interceptor] that ensures that RPC request messages match the constraints expressed in their Protobuf schemas. It does not validate response messages unless the WithValidateResponses option is specified. For connect-go v2, use NewServerInterceptor or NewClientInterceptor instead.
By default, Interceptors use a validator that lazily compiles constraints and works with any Protobuf message. This is a simple, widely-applicable configuration: after compiling and caching the constraints for a Protobuf message type once, validation is very efficient. To customize the validator, use WithValidator and protovalidate.ValidatorOption.
RPCs with invalid request messages short-circuit with an error. The error always uses [connect.CodeInvalidArgument] and has a detailed representation of the error attached as a [connect.ErrorDetail].
This interceptor is primarily intended for use on handlers. Client-side use is possible, but discouraged unless the client always has an up-to-date schema.
func NewInterceptor ¶
func NewInterceptor(opts ...Option) *Interceptor
NewInterceptor builds a connect-go v1 Interceptor. The default configuration is appropriate for most use cases.
func (*Interceptor) WrapStreamingClient ¶
func (i *Interceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc
WrapStreamingClient implements connect.Interceptor.
func (*Interceptor) WrapStreamingHandler ¶
func (i *Interceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc
WrapStreamingHandler implements connect.Interceptor.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
An Option configures an interceptor built by NewServerInterceptor, NewClientInterceptor, or NewInterceptor.
func WithValidateResponses ¶ added in v0.4.0
func WithValidateResponses() Option
WithValidateResponses configures the interceptor to also validate responses in addition to validating requests.
By default:
- Unary: Response messages from the server are not validated.
- Client streams: Received messages are not validated.
- Server streams: Sent messages are not validated.
However, these messages are all validated if this option is set.
func WithValidator ¶
func WithValidator(validator protovalidate.Validator) Option
WithValidator configures the interceptor to use a customized protovalidate.Validator. By default, protovalidate.GlobalValidator is used. See protovalidate.ValidatorOption for the range of available customizations.
func WithoutErrorDetails ¶ added in v0.6.0
func WithoutErrorDetails() Option
WithoutErrorDetails configures the interceptor to elide error details from validation errors. By default, a protovalidate.ValidationError is added as a detail when validation errors are returned.