-
Notifications
You must be signed in to change notification settings - Fork 16
/
validate.go
109 lines (90 loc) · 3.41 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package daemon
import (
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/parameters"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi-validator/requests"
"github.com/pb33f/libopenapi-validator/responses"
"github.com/pb33f/ranch/model"
"net/http"
)
func (ws *WiretapService) validateResponse(
request *model.Request,
responseValidator responses.ResponseBodyValidator,
returnedResponse *http.Response) {
var validationErrors []*errors.ValidationError
if ws.document != nil && ws.docModel != nil {
_, validationErrors = responseValidator.ValidateResponseBody(request.HttpRequest, returnedResponse)
}
// wipe out any path not found errors, they are not relevant to the response.
var cleanedErrors []*errors.ValidationError
for x := range validationErrors {
if !validationErrors[x].IsPathMissingError() {
cleanedErrors = append(cleanedErrors, validationErrors[x])
}
}
transaction := buildResponse(request, returnedResponse)
if len(cleanedErrors) > 0 {
transaction.ResponseValidation = cleanedErrors
}
ws.transactionStore.Put(request.Id.String(), transaction, nil)
if len(cleanedErrors) > 0 {
ws.broadcastResponseValidationErrors(request, returnedResponse, cleanedErrors)
} else {
ws.broadcastResponse(request, returnedResponse)
}
}
func (ws *WiretapService) validateRequest(
modelRequest *model.Request,
httpRequest *http.Request,
requestValidator requests.RequestBodyValidator,
paramValidator parameters.ParameterValidator,
responseValidator responses.ResponseBodyValidator) {
var validationErrors, cleanedErrors []*errors.ValidationError
if ws.document != nil && ws.docModel != nil {
// find path and populate validators.
path, pathErrors, pv := paths.FindPath(httpRequest, ws.docModel)
requestValidator.SetPathItem(path, pv)
paramValidator.SetPathItem(path, pv)
responseValidator.SetPathItem(path, pv)
// record any path errors.
validationErrors = append(validationErrors, pathErrors...)
// validate params
_, queryParams := paramValidator.ValidateQueryParams(httpRequest)
_, headerParams := paramValidator.ValidateHeaderParams(httpRequest)
_, cookieParams := paramValidator.ValidateCookieParams(httpRequest)
_, pathParams := paramValidator.ValidatePathParams(httpRequest)
validationErrors = append(validationErrors, queryParams...)
validationErrors = append(validationErrors, headerParams...)
validationErrors = append(validationErrors, cookieParams...)
validationErrors = append(validationErrors, pathParams...)
// validate modelRequest
_, requestErrors := requestValidator.ValidateRequestBody(httpRequest)
validationErrors = append(validationErrors, requestErrors...)
}
pm := false
for i := range validationErrors {
if validationErrors[i].IsPathMissingError() {
if !pm {
cleanedErrors = append(cleanedErrors, validationErrors[i])
pm = true
}
} else {
cleanedErrors = append(cleanedErrors, validationErrors[i])
}
}
// record results
transaction := buildRequest(modelRequest)
if len(cleanedErrors) > 0 {
transaction.RequestValidation = cleanedErrors
}
ws.transactionStore.Put(modelRequest.Id.String(), modelRequest, nil)
// broadcast what we found.
if len(cleanedErrors) > 0 {
ws.broadcastRequestValidationErrors(modelRequest, cleanedErrors)
} else {
ws.broadcastRequest(modelRequest)
}
}