Documentation
¶
Overview ¶
Package openapi provides supported-adapter OpenAPI validation middleware.
Use New with WithSpec or LoadSpecFile when HTTP services need request validation against an OpenAPI document. ResponseValidationOptions can enable response checks, streaming opt-outs, body limits, and safe error hooks.
Validation failures are mapped to Problem Details without exposing raw request bodies. The middleware does not own route registration or contract evolution; pair it with generated OpenAPI checks and release contract diffs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorHandler ¶
ErrorHandler handles OpenAPI validation failures.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware validates requests against an OpenAPI spec.
func New ¶
func New(spec *openapi3.T, opts ...Option) (*Middleware, error)
New constructs a validation middleware from a spec.
func NewFromFile ¶
func NewFromFile(path string, opts ...Option) (*Middleware, error)
NewFromFile loads and validates a spec from disk.
func (*Middleware) Handler ¶
func (m *Middleware) Handler(next http.Handler) http.Handler
Handler wraps the next handler with OpenAPI request validation.
func (*Middleware) Middleware ¶
func (m *Middleware) Middleware() func(http.Handler) http.Handler
Middleware implements ports.Middleware by returning the handler adapter.
type Option ¶
type Option func(*Options)
Option customizes middleware behavior.
func WithErrorHandler ¶
func WithErrorHandler(fn ErrorHandler) Option
WithErrorHandler overrides the error writer.
func WithFilterOptions ¶
func WithFilterOptions(opts openapi3filter.Options) Option
WithFilterOptions overrides request validation options.
func WithIgnoreNotFound ¶
WithIgnoreNotFound controls how unmatched routes are handled.
func WithResponseValidation ¶
func WithResponseValidation(opts ResponseValidationOptions) Option
WithResponseValidation configures response validation.
type Options ¶
type Options struct {
Spec *openapi3.T
FilterOptions openapi3filter.Options
ErrorHandler ErrorHandler
IgnoreNotFound bool
ResponseValidation ResponseValidationOptions
}
Options configures request validation middleware.
type ResponseValidationOptions ¶
type ResponseValidationOptions struct {
Enabled bool
MaxBodyBytes int64
ShouldValidate func(*http.Request) bool
ErrorHandler ErrorHandler
}
ResponseValidationOptions configures response validation. When enabled, responses are buffered for validation and do not preserve optional http.ResponseWriter interfaces such as Flusher, Hijacker, or Pusher. Use ShouldValidate to exclude streaming, upgrade, or large-download routes from response buffering while keeping request validation enabled.