openapi3middleware

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 11 Imported by: 0

README

CI PkgGoDev

go-openapi3-validation-middleware

net/http middleware to validate HTTP requests/responses against OpenAPI 3 schema using kin-openapi.

Installation

go get github.com/aereal/go-openapi3-validation-middleware

Synopsis

import (
	"net/http"

	"github.com/aereal/go-openapi3-validation-middleware"
	"github.com/getkin/kin-openapi/routers"
)

func main() {
	var router routers.Router // must be built with certain way
	mw := openapi3middleware.WithValidation(openapi3middleware.MiddlewareOptions{Router: router})
	http.Handle("/", mw(http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
		// this handler is called if validation succeeds
	})))
}

License

See LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithRequestValidation

func WithRequestValidation(options MiddlewareOptions) middleware

WithRequestValidation returns a middleware that validates against request. It immediately returns an error response and does not call next handler if validation failed.

func WithResponseValidation

func WithResponseValidation(options MiddlewareOptions) middleware

WithResponseValidation returns a middleware that validates against response. It may consume larger memory because it holds entire response body to validate it later.

func WithValidation

func WithValidation(options MiddlewareOptions) middleware

WithValidation returns a middleware that validates against both request and response.

Types

type MiddlewareOptions

type MiddlewareOptions struct {
	Router                        routers.Router
	ValidationOptions             *openapi3filter.Options
	ReportFindRouteError          func(w http.ResponseWriter, r *http.Request, err error)
	ReportRequestValidationError  func(w http.ResponseWriter, r *http.Request, err error)
	ReportResponseValidationError func(w http.ResponseWriter, r *http.Request, err error)
	TracerProvider                trace.TracerProvider
}

Jump to

Keyboard shortcuts

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