Debian Codegraph @

parameters/parameters.go

raw
1 // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package parameters
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 // ParameterValidator is an interface that defines the methods for validating parameters
16 // There are 4 types of parameters: query, header, cookie and path.
17 //
18 // ValidateQueryParams will validate the query parameters for the request
19 // ValidateHeaderParams will validate the header parameters for the request
20 // ValidateCookieParamsWithPathItem will validate the cookie parameters for the request
21 // ValidatePathParams will validate the path parameters for the request
22 //
23 // Each method accepts an *http.Request and returns true if validation passed,
24 // false if validation failed and a slice of ValidationError pointers.
25 type ParameterValidator interface {
26 // ValidateQueryParams accepts an *http.Request and validates the query parameters against the OpenAPI specification.
27 // The method will locate the correct path, and operation, based on the verb. The parameters for the operation
28 // will be matched and validated against what has been supplied in the http.Request query string.
29 ValidateQueryParams(request *http.Request) (bool, []*errors.ValidationError)
30
31 // ValidateQueryParamsWithPathItem accepts an *http.Request and validates the query parameters against the OpenAPI specification.
32 // The method will locate the correct path, and operation, based on the verb. The parameters for the operation
33 // will be matched and validated against what has been supplied in the http.Request query string.
34 ValidateQueryParamsWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError)
35
36 // ValidateHeaderParams validates the header parameters contained within *http.Request. It returns a boolean
37 // stating true if validation passed (false for failed), and a slice of errors if validation failed.
38 ValidateHeaderParams(request *http.Request) (bool, []*errors.ValidationError)
39
40 // ValidateHeaderParamsWithPathItem validates the header parameters contained within *http.Request. It returns a boolean
41 // stating true if validation passed (false for failed), and a slice of errors if validation failed.
42 ValidateHeaderParamsWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError)
43
44 // ValidateCookieParams validates the cookie parameters contained within *http.Request.
45 // It returns a boolean stating true if validation passed (false for failed), and a slice of errors if validation failed.
46 ValidateCookieParams(request *http.Request) (bool, []*errors.ValidationError)
47
48 // ValidateCookieParamsWithPathItem validates the cookie parameters contained within *http.Request.
49 // It returns a boolean stating true if validation passed (false for failed), and a slice of errors if validation failed.
50 ValidateCookieParamsWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError)
51
52 // ValidatePathParams validates the path parameters contained within *http.Request. It returns a boolean stating true
53 // if validation passed (false for failed), and a slice of errors if validation failed.
54 ValidatePathParams(request *http.Request) (bool, []*errors.ValidationError)
55
56 // ValidatePathParamsWithPathItem validates the path parameters contained within *http.Request. It returns a boolean stating true
57 // if validation passed (false for failed), and a slice of errors if validation failed.
58 ValidatePathParamsWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError)
59
60 // ValidateSecurity validates the security requirements for the operation. It returns a boolean stating true
61 // if validation passed (false for failed), and a slice of errors if validation failed.
62 ValidateSecurity(request *http.Request) (bool, []*errors.ValidationError)
63
64 // ValidateSecurityWithPathItem validates the security requirements for the operation. It returns a boolean stating true
65 // if validation passed (false for failed), and a slice of errors if validation failed.
66 ValidateSecurityWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError)
67 }
68
69 // NewParameterValidator will create a new ParameterValidator from an OpenAPI 3+ document
70 func NewParameterValidator(document *v3.Document, opts ...config.Option) ParameterValidator {
71 options := config.NewValidationOptions(opts...)
72
73 return &paramValidator{options: options, document: document}
74 }
75
76 type paramValidator struct {
77 options *config.ValidationOptions
78 document *v3.Document
79 }