Debian Codegraph @

responses/response_body.go

raw
1 // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package responses
5
6 import (
7 "net/http"
8
9 v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
10
11 "github.com/pb33f/libopenapi-validator/config"
12 "github.com/pb33f/libopenapi-validator/errors"
13 )
14
15 // ResponseBodyValidator is an interface that defines the methods for validating response bodies for Operations.
16 //
17 // ValidateResponseBody method accepts an *http.Request and returns true if validation passed,
18 // false if validation failed and a slice of ValidationError pointers.
19 type ResponseBodyValidator interface {
20 // ValidateResponseBody will validate the response body for a http.Response pointer. The request is used to
21 // locate the operation in the specification, the response is used to ensure the response code, media type and the
22 // schema of the response body are valid.
23 ValidateResponseBody(request *http.Request, response *http.Response) (bool, []*errors.ValidationError)
24
25 // ValidateResponseBodyWithPathItem will validate the response body for a http.Response pointer. The request is used to
26 // locate the operation in the specification, the response is used to ensure the response code, media type and the
27 // schema of the response body are valid.
28 ValidateResponseBodyWithPathItem(request *http.Request, response *http.Response, pathItem *v3.PathItem, pathFound string) (bool, []*errors.ValidationError)
29 }
30
31 // NewResponseBodyValidator will create a new ResponseBodyValidator from an OpenAPI 3+ document
32 func NewResponseBodyValidator(document *v3.Document, opts ...config.Option) ResponseBodyValidator {
33 options := config.NewValidationOptions(opts...)
34
35 return &responseBodyValidator{options: options, document: document}
36 }
37
38 type responseBodyValidator struct {
39 options *config.ValidationOptions
40 document *v3.Document
41 }