-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathstrict_errors.go
More file actions
154 lines (143 loc) · 4.54 KB
/
strict_errors.go
File metadata and controls
154 lines (143 loc) · 4.54 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
149
150
151
152
153
154
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
// SPDX-License-Identifier: MIT
package errors
import (
"fmt"
"strings"
)
// StrictValidationType is the validation type for strict mode errors.
const StrictValidationType = "strict"
// StrictValidationSubTypes for different kinds of undeclared values.
const (
StrictSubTypeProperty = "undeclared-property"
StrictSubTypeHeader = "undeclared-header"
StrictSubTypeQuery = "undeclared-query-param"
StrictSubTypeCookie = "undeclared-cookie"
)
// UndeclaredPropertyError creates a ValidationError for an undeclared property.
func UndeclaredPropertyError(
path string,
name string,
value any,
declaredProperties []string,
direction string,
requestPath string,
requestMethod string,
specLine int,
specCol int,
) *ValidationError {
dirStr := direction
if dirStr == "" {
dirStr = "request"
}
return &ValidationError{
ValidationType: StrictValidationType,
ValidationSubType: StrictSubTypeProperty,
Message: fmt.Sprintf("%s property '%s' at '%s' is not declared in schema",
dirStr, name, path),
Reason: fmt.Sprintf("Strict mode: found property not in schema. "+
"Declared properties: [%s]", strings.Join(declaredProperties, ", ")),
HowToFix: fmt.Sprintf("Add '%s' to the schema, remove it from the %s, "+
"or add '%s' to StrictIgnorePaths", name, dirStr, path),
RequestPath: requestPath,
RequestMethod: requestMethod,
ParameterName: name,
Context: truncateForContext(value),
SpecLine: specLine,
SpecCol: specCol,
}
}
// UndeclaredHeaderError creates a ValidationError for an undeclared header.
func UndeclaredHeaderError(
name string,
value string,
declaredHeaders []string,
direction string,
requestPath string,
requestMethod string,
) *ValidationError {
dirStr := direction
if dirStr == "" {
dirStr = "request"
}
return &ValidationError{
ValidationType: StrictValidationType,
ValidationSubType: StrictSubTypeHeader,
Message: fmt.Sprintf("%s header '%s' is not declared in specification",
dirStr, name),
Reason: fmt.Sprintf("Strict mode: found header not in spec. "+
"Declared headers: [%s]", strings.Join(declaredHeaders, ", ")),
HowToFix: fmt.Sprintf("Add '%s' to the operation's parameters, remove it from the %s, "+
"or add it to StrictIgnoredHeaders", name, dirStr),
RequestPath: requestPath,
RequestMethod: requestMethod,
ParameterName: name,
Context: value,
}
}
// UndeclaredQueryParamError creates a ValidationError for an undeclared query parameter.
func UndeclaredQueryParamError(
path string,
name string,
value any,
declaredParams []string,
requestPath string,
requestMethod string,
) *ValidationError {
return &ValidationError{
ValidationType: StrictValidationType,
ValidationSubType: StrictSubTypeQuery,
Message: fmt.Sprintf("query parameter '%s' at '%s' is not declared in specification", name, path),
Reason: fmt.Sprintf("Strict mode: found query parameter not in spec. "+
"Declared parameters: [%s]", strings.Join(declaredParams, ", ")),
HowToFix: fmt.Sprintf("Add '%s' to the operation's query parameters, remove it from the request, "+
"or add '%s' to StrictIgnorePaths", name, path),
RequestPath: requestPath,
RequestMethod: requestMethod,
ParameterName: name,
Context: truncateForContext(value),
}
}
// UndeclaredCookieError creates a ValidationError for an undeclared cookie.
func UndeclaredCookieError(
path string,
name string,
value any,
declaredCookies []string,
requestPath string,
requestMethod string,
) *ValidationError {
return &ValidationError{
ValidationType: StrictValidationType,
ValidationSubType: StrictSubTypeCookie,
Message: fmt.Sprintf("cookie '%s' at '%s' is not declared in specification", name, path),
Reason: fmt.Sprintf("Strict mode: found cookie not in spec. "+
"Declared cookies: [%s]", strings.Join(declaredCookies, ", ")),
HowToFix: fmt.Sprintf("Add '%s' to the operation's cookie parameters, remove it from the request, "+
"or add '%s' to StrictIgnorePaths", name, path),
RequestPath: requestPath,
RequestMethod: requestMethod,
ParameterName: name,
Context: truncateForContext(value),
}
}
// truncateForContext creates a truncated string representation for error context.
func truncateForContext(v any) string {
switch val := v.(type) {
case string:
if len(val) > 50 {
return val[:47] + "..."
}
return val
case map[string]any:
return "{...}"
case []any:
return "[...]"
default:
s := fmt.Sprintf("%v", v)
if len(s) > 50 {
return s[:47] + "..."
}
return s
}
}