fibermiddleware

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

Fiber v3 Middleware

⚠️ This README may be for the latest development version, which may contain unreleased changes. Please ensure you're looking at the README for the latest release version.

Middleware for the Fiber v3 web server for use with oapi-codegen.

[!NOTE] Using Fiber v2? You want oapi-codegen/fiber-middleware instead.

Validated request body

The middleware enables kin-openapi's openapi3filter.Options.KeepValidatedValue by default. Fiber does not run on net/http, so the validator works on a converted copy of the request; after a request body passes validation, the middleware stores the decoded value that was validated in the Fiber context under ValidatedBodyLocalsKey (x-oapi-codegen-validated-body), readable with ValidatedBody(c). The body bytes seen by the handler (c.Body()) are unchanged. oapi-codegen strict servers that support it bind that value instead of decoding the body a second time, so the handler sees exactly what was validated (for example, encoding/json's case-insensitive member matching can no longer make a request validate as one value but bind as another).

To opt out, for instance to limit memory retention for very large request bodies, set Options.DiscardValidatedValue = true. This setting overrides Options.Options.KeepValidatedValue.

Licensed under the Apache-2.0.

Documentation

Overview

Package middleware implements middleware function for server implementations, which validates incoming HTTP requests to make sure that they conform to the given OAPI 3.0 specification. When OAPI validation fails on the request, we return an HTTP/400.

Index

Constants

View Source
const ValidatedBodyLocalsKey = "x-oapi-codegen-validated-body"

ValidatedBodyLocalsKey is the c.Locals key under which the middleware stores the decoded request body value that passed validation (see Options.DiscardValidatedValue). The value is what kin-openapi validated: nil, bool, string, json.Number, []any or map[string]any.

Strict servers generated by oapi-codegen look the value up under this same key, where it is defined independently so generated code does not import this module. Keep the two in sync.

Variables

This section is empty.

Functions

func GetFiberContext

func GetFiberContext(c context.Context) fiber.Ctx

GetFiberContext gets the fiber context from within requests. It returns nil if not found or wrong type.

func GetUserData

func GetUserData(c context.Context) interface{}

func OapiRequestValidator

func OapiRequestValidator(swagger *openapi3.T) fiber.Handler

OapiRequestValidator is a fiber middleware function which validates incoming HTTP requests to make sure that they conform to the given OAPI 3.0 specification. When OAPI validation fails on the request, we return an HTTP/400 with error message

func OapiRequestValidatorWithOptions

func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) fiber.Handler

OapiRequestValidatorWithOptions creates a validator from a swagger object, with validation options

func OapiValidatorFromYamlFile

func OapiValidatorFromYamlFile(path string) (fiber.Handler, error)

OapiValidatorFromYamlFile creates a validator middleware from a YAML file path

func ValidateRequestFromContext

func ValidateRequestFromContext(c fiber.Ctx, router routers.Router, options *Options) error

ValidateRequestFromContext is called from the middleware above and actually does the work of validating a request.

func ValidatedBody added in v0.3.0

func ValidatedBody(c fiber.Ctx) (any, bool)

ValidatedBody returns the request body value that passed validation, as stored by the middleware under ValidatedBodyLocalsKey, and whether one was stored for this request.

Types

type ErrorHandler

type ErrorHandler func(c fiber.Ctx, message string, statusCode int)

ErrorHandler is called when there is an error in validation

type MultiErrorHandler

type MultiErrorHandler func(openapi3.MultiError) error

MultiErrorHandler is called when oapi returns a MultiError type

type Options

type Options struct {
	Options           openapi3filter.Options
	ErrorHandler      ErrorHandler
	ParamDecoder      openapi3filter.ContentParameterDecoder
	UserData          interface{}
	MultiErrorHandler MultiErrorHandler
	// DiscardValidatedValue turns off openapi3filter.Options.KeepValidatedValue,
	// which this middleware enables by default. With it enabled, the decoded
	// request body value that passed validation is stored in the Fiber context
	// under ValidatedBodyLocalsKey, so a handler (for example an oapi-codegen
	// strict server) can bind that value instead of decoding the body a second
	// time. Set this to drop the decoded value after validation, for instance to
	// limit memory retention for very large request bodies.
	//
	// Options.Options.KeepValidatedValue is overridden by this setting.
	DiscardValidatedValue bool
}

Options to customize request validation. These are passed through to openapi3filter.

Jump to

Keyboard shortcuts

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