validator

package module
v0.0.0-...-0123857 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 10 Imported by: 0

README

Validator: OpenAPI 3.x validation middleware for Echo

reference report tests coverage

A simple package to validate incoming requests against an OpenAPI specification for Echo.

⚙️ Installation

go get -u czechia.dev/validator

📝 Usage

Other OpenAPI validators use a router from the kin-openapi package within the validator itself. This means a request is 'routed' twice, once by Echo, and then again by the validator.

czechia.cz/validator takes advantage of the fact that Echo has already routed valid requests, so the handler path, parameters, etc. are all available in the Echo context. We simply look for the corresponding path in the OpenAPI specification and validate against it directly.

The validator maintains a cached list of Echo Routes and their associated OpenAPI paths to speed up validation. You can prepopulate the path cache by setting the Echo Route Name as the OpenAPI path and call the validator.Initialize() function.

If the OpenAPI path is not found in the cache at runtime then the validator searches for the path according to the route's parameter names. This means your Echo parameters must have the same names as the OpenAPI parameters.

Example: if the Echo handler path is /hello/:name then we look for the /hello/{name} OpenAPI path.

Ultimtely, if no OpenAPI path is found then the response is always an error.

👀 Example

package main

import (
	"fmt"
	"net/http"

	"czechia.dev/validator"
	"github.com/getkin/kin-openapi/openapi3"
	"github.com/labstack/echo/v4"
)

func main() {
	e := echo.New()
	spec, _ := openapi3.NewLoader().LoadFromFile("test-service.yaml")
	e.Use(validator.New(spec))

	e.GET("/hello/:name", func(ctx echo.Context) error {
		return ctx.String(http.StatusOK, fmt.Sprintf(`{"greeting": "Hello, %s!"}`, ctx.Param("name")))
	}).Name = "/hello/{name}"

	validator.Initialize(e, spec)

	e.Start(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Initialize

func Initialize(e *echo.Echo, spec *openapi3.T)

Initialize populates the validator path cache before starting the server.

You must set the route Name to the OpenAPI path, e.g.:

e.GET("/hello/:name", helloHandler).Name = "/hello/{name}"

func New

func New(spec *openapi3.T, config ...Config) echo.MiddlewareFunc

New creates a new OpenAPI validator for Echo.

Types

type Config

type Config struct {

	// Skipper defines a function to skip this middleware when returned true.
	// This field is used only by Echo.
	//
	// Optional. Default: nil
	Skipper middleware.Skipper

	Options      *openapi3filter.Options
	ParamDecoder openapi3filter.ContentParameterDecoder
}

Config defines the Validator config for the middleware.

Jump to

Keyboard shortcuts

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