Documentation ¶
Overview ¶
Package assert provides methods that allow you to write simple swagger validations.
Example Usage ¶
See it in action:
package main import ( assert "github.com/faabiosr/openapi-assert" "log" "net/http" ) func main() { doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json") if err != nil { log.Fatal(err) } log.Println( assert.RequestMediaType("text/html", doc, "/pet", http.MethodPost), ) }
If you want to assert many times, see below:
package main import ( assert "github.com/faabiosr/openapi-assert" "log" "net/http" ) func main() { doc, err := assert.LoadFromURI("http://petstore.swagger.io/v2/swagger.json") assert := assert.New(doc) if err != nil { log.Fatal(err) } log.Println( assert.RequestMediaType("text/html", "/pet", http.MethodPost), ) log.Println( assert.RequestMediaType("image/gif", "/v2/pet", http.MethodPost), ) }
Index ¶
- Variables
- func RequestHeaders(header http.Header, doc Document, path, method string) error
- func RequestMediaType(mediaType string, doc Document, path, method string) error
- func RequestQuery(query url.Values, doc Document, path, method string) error
- func ResponseHeaders(header http.Header, doc Document, path, method string, statusCode int) error
- func ResponseMediaType(mediaType string, doc Document, path, method string) error
- type Assertions
- func (a *Assertions) RequestHeaders(header http.Header, path, method string) error
- func (a *Assertions) RequestMediaType(mediaType, path, method string) error
- func (a *Assertions) RequestQuery(query url.Values, path, method string) error
- func (a *Assertions) ResponseHeaders(header http.Header, path, method string, statusCode int) error
- func (a *Assertions) ResponseMediaType(mediaType, path, method string) error
- type Document
- type Headers
- type Param
- type Query
- type Required
- type Swagger
- func (s *Swagger) FindNode(segments ...string) (interface{}, error)
- func (s *Swagger) FindPath(uri string) (string, error)
- func (s *Swagger) RequestHeaders(path, method string) (Headers, error)
- func (s *Swagger) RequestMediaTypes(path, method string) ([]string, error)
- func (s *Swagger) RequestQuery(path, method string) (Query, error)
- func (s *Swagger) ResponseHeaders(path, method string, statusCode int) (Headers, error)
- func (s *Swagger) ResponseMediaTypes(path, method string) ([]string, error)
Constants ¶
This section is empty.
Variables ¶
var ( // FailMessage define the base error message. FailMessage = "failed asserting that %s" // ErrMediaType shows the media type error format. ErrMediaType = `'%s' is an allowed media type (%s)` // ErrRequestHeaders shows the request headers error format. ErrRequestHeaders = `'%s' is a valid request header (%s)` // ErrRequestQuery shows the request query error format. ErrRequestQuery = `'%s' is a valid request query (%s)` // ErrResponseHeaders shows the response headers error format. ErrResponseHeaders = `'%s' is a valid response header (%s)` // ErrValidation wrap the json schema validation errors ErrValidation = "unable to load the validation" // ErrJson wrap the json marshall errors ErrJson = "unable to marshal" )
var ( // ErrResourceURI returns an error when uri does not match. ErrResourceURI = "resource uri does not match" // ErrSwaggerLoad returns an error when load swagger document. ErrSwaggerLoad = "unable to load the document by uri" // ErrSwaggerExpand returns an error of expanding schema. ErrSwaggerExpand = "unable to expand the document" // ErrNodeNotFound returns an error when node does not exists. ErrNodeNotFound = "node does not exists" )
Functions ¶
func RequestHeaders ¶
RequestHeaders asserts rquest headers againt a schema header list.
func RequestMediaType ¶
RequestMediaType asserts request media type against a list.
func RequestQuery ¶
RequestQuery asserts request query againt a schema.
func ResponseHeaders ¶
ResponseHeaders asserts response headers againt a schema header list.
Types ¶
type Assertions ¶
type Assertions struct {
// contains filtered or unexported fields
}
Assertions packs all assert methods into one structure
func (*Assertions) RequestHeaders ¶
func (a *Assertions) RequestHeaders(header http.Header, path, method string) error
RequestHeaders asserts rquest headers againt a schema header list.
func (*Assertions) RequestMediaType ¶
func (a *Assertions) RequestMediaType(mediaType, path, method string) error
RequestMediaType asserts request media type against a list.
func (*Assertions) RequestQuery ¶
func (a *Assertions) RequestQuery(query url.Values, path, method string) error
RequestQuery asserts request query againt a schema query list.
func (*Assertions) ResponseHeaders ¶
ResponseHeaders asserts response headers againt a schema header list.
func (*Assertions) ResponseMediaType ¶
func (a *Assertions) ResponseMediaType(mediaType, path, method string) error
ResponseMediaType asserts response media type against a list.
type Document ¶
type Document interface { // FindPath searches for an uri in document and returns the path. FindPath(uri string) (string, error) // FindNode searches a node by using the segments in the document. FindNode(segments ...string) (interface{}, error) // RequestMediaTypes retrives a list of request media types allowed. RequestMediaTypes(path, method string) ([]string, error) // ResponseMediaTypes retrives a list of response media types allowed. ResponseMediaTypes(path, method string) ([]string, error) // RequestHeaders retrieves a list of request headers. RequestHeaders(path, method string) (Headers, error) // ResponseHeaders retrieves a list of response headers. ResponseHeaders(path, method string, statusCode int) (Headers, error) // RequestQuery retrieves a list of request query. RequestQuery(path, method string) (Query, error) }
Document that defines the contract for reading OpenAPI documents.
type Headers ¶
type Headers map[string]interface{}
Headers is a list of headers in json schema format.
type Query ¶
type Query map[string]interface{}
Query is a list of query parameters in json schema format.
type Swagger ¶
type Swagger struct {
// contains filtered or unexported fields
}
Swagger stores the loaded swagger spec.
func LoadFromURI ¶
LoadFromURI loads and expands swagger document by uri.
func (*Swagger) RequestHeaders ¶
RequestHeaders retrieves a list of request headers.
func (*Swagger) RequestMediaTypes ¶
RequestMediaTypes retrives a list of request media types allowed.
func (*Swagger) RequestQuery ¶
RequestQuery retrieves a list of request query.
func (*Swagger) ResponseHeaders ¶
ResponseHeaders retrieves a list of response headers.