-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresult.go
More file actions
148 lines (120 loc) · 4.43 KB
/
Copy pathresult.go
File metadata and controls
148 lines (120 loc) · 4.43 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package httpvalidator
import (
"github.com/erraggy/oastools/internal/issues"
"github.com/erraggy/oastools/internal/severity"
)
// ValidationError represents a single HTTP validation issue.
// This is an alias to issues.Issue for consistency with other oastools packages.
type ValidationError = issues.Issue
// Severity levels for validation errors.
type Severity = severity.Severity
// Severity constants re-exported for convenience.
const (
SeverityError = severity.SeverityError
SeverityWarning = severity.SeverityWarning
SeverityInfo = severity.SeverityInfo
SeverityCritical = severity.SeverityCritical
)
// ValidationLocation indicates where in the HTTP message the error occurred.
type ValidationLocation string
// Validation location constants.
const (
LocationPath ValidationLocation = "path"
LocationQuery ValidationLocation = "query"
LocationHeader ValidationLocation = "header"
LocationCookie ValidationLocation = "cookie"
LocationRequestBody ValidationLocation = "requestBody"
LocationResponse ValidationLocation = "response"
)
// RequestValidationResult contains the results of validating an HTTP request
// against an OpenAPI specification.
type RequestValidationResult struct {
// Valid is true if the request passes all validation checks.
Valid bool
// Errors contains all validation errors found.
Errors []ValidationError
// Warnings contains best-practice warnings (if IncludeWarnings is enabled).
Warnings []ValidationError
// MatchedPath is the OpenAPI path template that matched the request
// (e.g., "/pets/{petId}"). Empty if no path matched.
MatchedPath string
// MatchedMethod is the HTTP method of the request (e.g., "GET", "POST").
MatchedMethod string
// PathParams contains the extracted and validated path parameters.
// Keys are parameter names, values are the deserialized values.
PathParams map[string]any
// QueryParams contains the extracted and validated query parameters.
QueryParams map[string]any
// HeaderParams contains the extracted and validated header parameters.
HeaderParams map[string]any
// CookieParams contains the extracted and validated cookie parameters.
CookieParams map[string]any
}
// ResponseValidationResult contains the results of validating an HTTP response
// against an OpenAPI specification.
type ResponseValidationResult struct {
// Valid is true if the response passes all validation checks.
Valid bool
// Errors contains all validation errors found.
Errors []ValidationError
// Warnings contains best-practice warnings (if IncludeWarnings is enabled).
Warnings []ValidationError
// StatusCode is the HTTP status code of the response.
StatusCode int
// ContentType is the Content-Type of the response.
ContentType string
// MatchedPath is the OpenAPI path template that matched the original request.
MatchedPath string
// MatchedMethod is the HTTP method of the original request.
MatchedMethod string
}
// newRequestResult creates a new RequestValidationResult with initialized maps.
func newRequestResult() *RequestValidationResult {
return &RequestValidationResult{
Valid: true,
PathParams: make(map[string]any),
QueryParams: make(map[string]any),
HeaderParams: make(map[string]any),
CookieParams: make(map[string]any),
}
}
// newResponseResult creates a new ResponseValidationResult.
func newResponseResult() *ResponseValidationResult {
return &ResponseValidationResult{
Valid: true,
}
}
// addError adds an error to the request result and marks it as invalid.
func (r *RequestValidationResult) addError(path, message string, sev Severity) {
r.Valid = false
r.Errors = append(r.Errors, ValidationError{
Path: path,
Message: message,
Severity: sev,
})
}
// addWarning adds a warning to the request result.
func (r *RequestValidationResult) addWarning(path, message string) {
r.Warnings = append(r.Warnings, ValidationError{
Path: path,
Message: message,
Severity: SeverityWarning,
})
}
// addError adds an error to the response result and marks it as invalid.
func (r *ResponseValidationResult) addError(path, message string, sev Severity) {
r.Valid = false
r.Errors = append(r.Errors, ValidationError{
Path: path,
Message: message,
Severity: sev,
})
}
// addWarning adds a warning to the response result.
func (r *ResponseValidationResult) addWarning(path, message string) {
r.Warnings = append(r.Warnings, ValidationError{
Path: path,
Message: message,
Severity: SeverityWarning,
})
}