openapi

package
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package openapi provides OpenAPI request and response validation middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandler

type ErrorHandler func(http.ResponseWriter, *http.Request, int, error)

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

func WithIgnoreNotFound(ignore bool) Option

WithIgnoreNotFound controls how unmatched routes are handled.

func WithResponseValidation

func WithResponseValidation(opts ResponseValidationOptions) Option

WithResponseValidation configures response validation.

func WithSpec

func WithSpec(spec *openapi3.T) Option

WithSpec sets the OpenAPI spec to validate against.

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.

Jump to

Keyboard shortcuts

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