middleware

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package middleware implements middleware function for go-chi or net/http, which validates incoming HTTP requests to make sure that they conform to the given OAPI 3.0 specification. When OAPI validation failes on the request, we return an HTTP/400.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OapiRequestValidator

func OapiRequestValidator(swagger *openapi3.T) func(next http.Handler) http.Handler

OapiRequestValidator Creates middleware to validate request by swagger spec. This middleware is good for net/http either since go-chi is 100% compatible with net/http.

func OapiRequestValidatorWithOptions

func OapiRequestValidatorWithOptions(sw *openapi3.T, opts *Options) func(next http.Handler) http.Handler

OapiRequestValidatorWithOptions Creates middleware to validate request by swagger spec against an OpenAPI 3 specification. This middleware is good for net/http either since go-chi is 100% compatible with net/http.

Parameters:

  • swagger: Pointer to an OpenAPI 3 specification object
  • options: Optional configuration parameters for the validator

Returns a middleware function that can be used with HTTP handlers.

The middleware performs the following:

  • Validates requests against the provided OpenAPI specification
  • Supports URL-encoded path parameters
  • Handles validation errors through a custom error handler if provided in options
  • Can continue processing despite validation errors if ContinueOnError is set

Warning: If the OpenAPI spec includes Servers configuration, the middleware performs Host header validation which may result in 400 Bad Request responses for otherwise valid requests. This behavior can be silenced by setting Options.SilenceServersWarning to true.

The returned middleware function wraps the next handler in the chain and performs validation before passing the request through.

func SwaggerPathsToGorillaPaths added in v1.0.4

func SwaggerPathsToGorillaPaths(paths *openapi3.Paths) *openapi3.Paths

Types

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, statusCode int)

ErrorHandler is called when there is an error in validation

type MultiErrorHandler

type MultiErrorHandler func(openapi3.MultiError) (int, error)

MultiErrorHandler is called when oapi returns a MultiError type

type Options

type Options struct {
	Options           openapi3filter.Options
	ErrorHandler      ErrorHandler
	MultiErrorHandler MultiErrorHandler
	// SilenceServersWarning allows silencing a warning for https://github.com/clinia/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`
	SilenceServersWarning bool
	// If true, the next handler will be called even when validation fails
	ContinueOnError bool
}

Options to customize request validation, openapi3filter specified options will be passed through.

Jump to

Keyboard shortcuts

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