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 ¶
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 ¶
var ErrForbidden = errors.New("forbidden")
ErrForbidden reports valid credentials without the required access.
var ErrUnauthenticated = errors.New("unauthenticated")
ErrUnauthenticated reports missing or invalid credentials.
Functions ¶
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.
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 ¶
FailureHandlerFunc adapts a function to 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.
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 ¶
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 ¶
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.