{"modulePath":"github.com/oapi-codegen/nethttp-middleware","version":"v1.3.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package nethttpmiddleware\n\nProvide HTTP middleware functionality to validate that incoming requests conform to a given OpenAPI 3.x specification.\n\nThis provides middleware for any \\`net/http\\` conforming HTTP Server.\n\nThis package is a lightweight wrapper over [https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3filter](https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3filter) from [https://pkg.go.dev/github.com/getkin/kin-openapi](https://pkg.go.dev/github.com/getkin/kin-openapi).\n\nThis is \\_intended\\_ to be used with code that's generated through [https://pkg.go.dev/github.com/oapi-codegen/oapi-codegen](https://pkg.go.dev/github.com/oapi-codegen/oapi-codegen), but should work otherwise.\n\n## Functions\n\n```go\nfunc OapiRequestValidator(spec *openapi3.T) func(next http.Handler) http.Handler\n```\nOapiRequestValidator Creates the middleware to validate that incoming requests match the given OpenAPI 3.x spec, with a default set of configuration.\n\n```go\nfunc OapiRequestValidatorWithOptions(spec *openapi3.T, options *Options) func(next http.Handler) http.Handler\n```\nOapiRequestValidatorWithOptions Creates the middleware to validate that incoming requests match the given OpenAPI 3.x spec, allowing explicit configuration.\n\nNOTE that this may panic if the OpenAPI spec isn't valid, or if it cannot be used to create the middleware\n\n## Types\n\n```go\ntype ErrorHandler func(w http.ResponseWriter, message string, statusCode int)\n```\nErrorHandler is called when there is an error in validation\n\nIf both an \\`ErrorHandlerWithOpts\\` and \\`ErrorHandler\\` are set, the \\`ErrorHandlerWithOpts\\` takes precedence.\n\nDeprecated: it's recommended you migrate to the ErrorHandlerWithOpts, as it provides more control over how to handle an error that occurs, including giving direct access to the \\`error\\` itself. There are no plans to remove this method.\n\n```go\ntype ErrorHandlerOpts struct {\n\t// StatusCode indicates the HTTP Status Code that the OpenAPI validation middleware _suggests_ is returned to the user.\n\t//\n\t// NOTE that this is very much a suggestion, and can be overridden if you believe you have a better approach.\n\tStatusCode int\n\n\t// MatchedRoute is the underlying path that this request is being matched against.\n\t//\n\t// This is the route according to the OpenAPI validation middleware, and can be used in addition to/instead of the `http.Request`\n\t//\n\t// NOTE that this will be nil if there is no matched route (i.e. a request has been sent to an endpoint not in the OpenAPI spec)\n\tMatchedRoute *ErrorHandlerOptsMatchedRoute\n}\n```\nErrorHandlerOpts contains additional options that are passed to the \\`ErrorHandlerWithOpts\\` function in the case of an error being returned by the middleware\n\n```go\ntype ErrorHandlerOptsMatchedRoute struct {\n\t// Route indicates the Route that this error is received by.\n\t//\n\t// This can be used in addition to/instead of the `http.Request`.\n\tRoute *routers.Route\n\n\t// PathParams are any path parameters that are determined from the request.\n\t//\n\t// This can be used in addition to/instead of the `http.Request`.\n\tPathParams map[string]string\n}\n```\n\n```go\ntype ErrorHandlerWithOpts func(ctx context.Context, err error, w http.ResponseWriter, r *http.Request, opts ErrorHandlerOpts)\n```\nErrorHandlerWithOpts is called when there is an error in validation, with more information about the \\`error\\` that occurred and which request is currently being processed.\n\nThere are a number of known types that the \\`error\\` can be:\n\n\\- \\`\\*openapi3filter.SecurityRequirementsError\\` - if the \\`AuthenticationFunc\\` has failed to authenticate the request - \\`\\*openapi3filter.RequestError\\` - if a bad request has been made\n\nAdditionally, if you have set \\`openapi3filter.Options#MultiError\\`:\n\n\\- \\`openapi3.MultiError\\` ([https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3#MultiError](https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3#MultiError))\n\nIf both an \\`ErrorHandlerWithOpts\\` and \\`ErrorHandler\\` are set, the \\`ErrorHandlerWithOpts\\` takes precedence.\n\nNOTE that this should ideally be used instead of ErrorHandler\n\n```go\ntype MultiErrorHandler func(openapi3.MultiError) (int, error)\n```\nMultiErrorHandler is called when the OpenAPI filter returns an openapi3.MultiError ([https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3#MultiError](https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3#MultiError))\n\n```go\ntype Options struct {\n\t// Options contains any configuration for the underlying `openapi3filter`\n\tOptions openapi3filter.Options\n\t// ErrorHandler is called when a validation error occurs.\n\t//\n\t// If both an `ErrorHandlerWithOpts` and `ErrorHandler` are set, the `ErrorHandlerWithOpts` takes precedence.\n\t//\n\t// If not provided, `http.Error` will be called\n\tErrorHandler ErrorHandler\n\n\t// ErrorHandlerWithOpts is called when there is an error in validation.\n\t//\n\t// If both an `ErrorHandlerWithOpts` and `ErrorHandler` are set, the `ErrorHandlerWithOpts` takes precedence.\n\tErrorHandlerWithOpts ErrorHandlerWithOpts\n\n\t// MultiErrorHandler is called when there is an openapi3.MultiError (https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3#MultiError) returned by the `openapi3filter`.\n\t//\n\t// If not provided `defaultMultiErrorHandler` will be used.\n\t//\n\t// Does not get called when using `ErrorHandlerWithOpts`\n\tMultiErrorHandler MultiErrorHandler\n\t// SilenceServersWarning allows silencing a warning for https://github.com/deepmap/oapi-codegen/issues/882 that reports when an OpenAPI spec has `spec.Servers != nil`\n\tSilenceServersWarning bool\n\t// DoNotValidateServers ensures that there is no Host validation performed (see `SilenceServersWarning` and https://github.com/deepmap/oapi-codegen/issues/882 for more details)\n\tDoNotValidateServers bool\n\t// Prefix allows (optionally) trimming a prefix from the API path.\n\t// This may be useful if your API is routed to an internal path that is different from the OpenAPI specification.\n\tPrefix string\n\n\t// Skipper allows writing a function that runs before any middleware and determines whether the given request should skip any validation middleware\n\tSkipper Skipper\n\n\t// DiscardValidatedValue turns off openapi3filter.Options.KeepValidatedValue,\n\t// which this middleware enables by default. With it enabled, the request body\n\t// seen by handlers exposes the value that was validated through a\n\t// ValidatedValue() method, so a handler (for example an oapi-codegen strict\n\t// server) can bind that value instead of decoding the body a second time.\n\t// Set this to drop the decoded value after validation, for instance to limit\n\t// memory retention for very large request bodies.\n\t//\n\t// Options.Options.KeepValidatedValue is overridden by this setting.\n\tDiscardValidatedValue bool\n}\n```\nOptions allows configuring the OapiRequestValidator.\n\n```go\ntype Skipper func(r *http.Request) bool\n```\nSkipper is a function that runs before any validation middleware, and determines whether the given request should skip any validation middleware\n\nReturn \\`true\\` if the request should be skipped\n\n","path":"github.com/oapi-codegen/nethttp-middleware","name":"nethttpmiddleware","synopsis":"Provide HTTP middleware functionality to validate that incoming requests conform to a given OpenAPI 3.x specification.","isRedistributable":true}
