schema

package
v1.7.3 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: MIT Imports: 23 Imported by: 0

README

Swagger Schema Validation

This packages provides functionality for validate http responses, including body schema against a swagger definition using go-swagger library.

Open Banking Specs

Setup

This package uses flattened version of Open Banking API specs, you will the swaggger CLI tool from:

https://goswagger.io/install.html

To flatten a spec use command:

swagger flatten --with-expand -o account-info-swagger.flattened.json account-info-swagger.json
Usage

This package is a wrapper around swagger library validator with adicional status code and content type check, also allowing implementing other custom validator and version variations.

func main() {
    v, err := NewSwaggerValidator("https://raw.githubusercontent.com/OpenBankingUK/read-write-api-specs/v3.1.0/dist/account-info-swagger.json")
    if err != nil {
        panic(err)
    }
    
    failures, err := v.Validate(r)
    if err != nil {
        panic(err)
    }

    fmt.Printf("Validation failures found:\n%v", failures)    
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("operation for method/path not found")

ErrNotFound -

Functions

func ShouldUseOpenApi3

func ShouldUseOpenApi3(version string) (bool, error)

Types

type Failure

type Failure struct {
	Message string
}

Failure represents a validation failure

type HTTPResponse

type HTTPResponse struct {
	Method     string
	Path       string
	Header     http.Header
	Body       io.Reader
	StatusCode int
}

HTTPResponse represents a response object from a HTTP Call

type Matcher

type Matcher interface {
	Match(pathWithParams, path2 string) bool
}

Matcher exposes a path comparison interface Expected usage to compare a path that has placeholder and a real path. Ex: /accounts/{account_id} == /accounts/1234567890

func NewMatcher

func NewMatcher() Matcher

NewMatcher -

type NullValidator

type NullValidator struct{}

NullValidator - type

func NewNullValidator

func NewNullValidator() NullValidator

NewNullValidator -

func (NullValidator) IsRequestProperty

func (v NullValidator) IsRequestProperty(method, path, propertpath string) (bool, string, error)

IsRequestProperty - nop

func (NullValidator) Validate

func (v NullValidator) Validate(r HTTPResponse) ([]Failure, error)

Validate - nop

type OpenAPI3Validator

type OpenAPI3Validator struct {
	// contains filtered or unexported fields
}

OpenAPI3Validator - type

func NewRawOpenAPI3Validator

func NewRawOpenAPI3Validator(specName, version string) (OpenAPI3Validator, error)

NewRawOpenAPI3Validator -

func (OpenAPI3Validator) IsRequestProperty

func (v OpenAPI3Validator) IsRequestProperty(checkmethod, checkpath, propertyPath string) (bool, string, error)

IsRequestProperty - Find param in schema and determines if it's part of request body

func (OpenAPI3Validator) Validate

func (v OpenAPI3Validator) Validate(r HTTPResponse) ([]Failure, error)

Validate - validates the response

type RequestWrapper

type RequestWrapper struct {
	Method      string
	URL         string
	ContentType string
	Body        string
}

RequestWrapper -

type ResponseWrapper

type ResponseWrapper struct {
	Status      int
	ContentType string
	Body        string
}

ResponseWrapper -

type Validator

type Validator interface {
	Validate(HTTPResponse) ([]Failure, error)
	IsRequestProperty(method, path, propertpath string) (bool, string, error)
}

Validator validates a HTTP response object against a schema

func NewOpenAPI3Validator

func NewOpenAPI3Validator(specName, version string) (Validator, error)

NewOpenAPI3Validator - Create a router for OPenAPI3 based specifications preferring yaml for the spec file

func NewSwaggerOBSpecValidator

func NewSwaggerOBSpecValidator(specName, version string) (Validator, error)

NewSwaggerOBSpecValidator -

func NewSwaggerValidator

func NewSwaggerValidator(schemaPath string) (Validator, error)

NewSwaggerValidator returns a swagger validator implementation Takes a schema file path as source, can be remote http(s) or local

Jump to

Keyboard shortcuts

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