oapivalidator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package oapivalidator validates Echo requests against an OpenAPI document.

It deliberately keeps generated oapi-codegen handlers transport-only: request validation and authentication run before the generated Echo wrapper, while application authorization remains in handwritten code.

Index

Constants

View Source
const (
	ProblemTypeNotFound             = "urn:devctl:oapivalidator:problem:not-found"
	ProblemTypeMethodNotAllowed     = "urn:devctl:oapivalidator:problem:method-not-allowed"
	ProblemTypeMalformedRequest     = "urn:devctl:oapivalidator:problem:malformed-request"
	ProblemTypeInvalidRequest       = "urn:devctl:oapivalidator:problem:invalid-request"
	ProblemTypeUnsupportedMediaType = "urn:devctl:oapivalidator:problem:unsupported-media-type"
	ProblemTypeUnauthenticated      = "urn:devctl:oapivalidator:problem:unauthenticated"
	ProblemTypeForbidden            = "urn:devctl:oapivalidator:problem:forbidden"
	ProblemTypeInternal             = "urn:devctl:oapivalidator:problem:internal"
)

Variables

View Source
var ErrForbidden = errors.New("forbidden")

ErrForbidden reports valid credentials without the required access.

View Source
var ErrUnauthenticated = errors.New("unauthenticated")

ErrUnauthenticated reports missing or invalid credentials.

Functions

func New

func New(document *openapi3.T, options ...Option) (echo.MiddlewareFunc, error)

New constructs request validation middleware for document.

Types

type AuthenticationInput

type AuthenticationInput struct {
	Request            *http.Request
	OperationID        string
	SecuritySchemeName string
	SecurityScheme     *openapi3.SecurityScheme
	Scopes             []string
}

AuthenticationInput describes one security scheme in the current OpenAPI security requirement.

type Authenticator

type Authenticator interface {
	// Authenticate validates the requested scheme and returns the context that
	// subsequent schemes and the endpoint handler receive.
	Authenticate(ctx context.Context, input AuthenticationInput) (nextCtx context.Context, err error)
}

Authenticator validates one OpenAPI security scheme at a time.

type AuthenticatorFunc

type AuthenticatorFunc func(ctx context.Context, input AuthenticationInput) (nextCtx context.Context, err error)

AuthenticatorFunc adapts a function to Authenticator.

func (AuthenticatorFunc) Authenticate

func (fn AuthenticatorFunc) Authenticate(ctx context.Context, input AuthenticationInput) (context.Context, error)

Authenticate implements Authenticator.

type Failure

type Failure struct {
	Kind        FailureKind
	Status      int
	OperationID string
	Errors      []FieldError
	Truncated   bool
	Cause       error
	// contains filtered or unexported fields
}

Failure retains the private cause while exposing safe client diagnostics.

func (*Failure) Error

func (failure *Failure) Error() string

Error implements error without exposing the underlying validator message.

func (*Failure) Problem

func (failure *Failure) Problem() Problem

Problem returns the safe RFC 9457 representation of failure.

func (*Failure) Unwrap

func (failure *Failure) Unwrap() error

Unwrap exposes the original failure to server-side errors.Is/errors.As.

type FailureHandler

type FailureHandler interface {
	// Handle writes or returns the response for failure.
	Handle(c *echo.Context, failure *Failure) error
}

FailureHandler handles a normalized validation failure.

type FailureHandlerFunc

type FailureHandlerFunc func(c *echo.Context, failure *Failure) error

FailureHandlerFunc adapts a function to FailureHandler.

func (FailureHandlerFunc) Handle

func (fn FailureHandlerFunc) Handle(c *echo.Context, failure *Failure) error

Handle implements FailureHandler.

type FailureKind

type FailureKind string

FailureKind identifies a stable category of request validation failure.

const (
	FailureNotFound             FailureKind = "not_found"
	FailureMethodNotAllowed     FailureKind = "method_not_allowed"
	FailureMalformedRequest     FailureKind = "malformed_request"
	FailureInvalidRequest       FailureKind = "invalid_request"
	FailureUnsupportedMediaType FailureKind = "unsupported_media_type"
	FailureUnauthenticated      FailureKind = "unauthenticated"
	FailureForbidden            FailureKind = "forbidden"
	FailureInternal             FailureKind = "internal"
)

type FieldError

type FieldError struct {
	Code      string   `json:"code"`
	Detail    string   `json:"detail"`
	In        Location `json:"in,omitempty"`
	Pointer   string   `json:"pointer,omitempty"`
	Parameter string   `json:"parameter,omitempty"`
}

FieldError is a safe, normalized request validation error.

type Location

type Location string

Location identifies the part of the request containing an invalid value.

const (
	LocationBody   Location = "body"
	LocationPath   Location = "path"
	LocationQuery  Location = "query"
	LocationHeader Location = "header"
	LocationCookie Location = "cookie"
)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures request validation middleware.

func WithAuthenticator

func WithAuthenticator(authenticator Authenticator) Option

WithAuthenticator configures OpenAPI security-scheme authentication.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL configures the path prefix used when generated handlers are registered with oapi-codegen RegisterHandlersOptions.BaseURL.

func WithFailureHandler

func WithFailureHandler(handler FailureHandler) Option

WithFailureHandler replaces the default RFC 9457 problem writer.

func WithMaxReportedErrors

func WithMaxReportedErrors(maxErrors int) Option

WithMaxReportedErrors limits the normalized validation errors returned to a client. Validation itself still examines the complete request.

type Problem

type Problem struct {
	Type      string       `json:"type"`
	Title     string       `json:"title"`
	Status    int          `json:"status"`
	Detail    string       `json:"detail,omitempty"`
	Instance  string       `json:"instance,omitempty"`
	Errors    []FieldError `json:"errors,omitempty"`
	Truncated bool         `json:"truncated,omitempty"`
}

Problem is an RFC 9457 problem details response.

Directories

Path Synopsis
internal
testapi
Package testapi contains an oapi-codegen Echo 5 strict-server fixture.
Package testapi contains an oapi-codegen Echo 5 strict-server fixture.
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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