Debian Codegraph @

errors/strict_errors.go

raw
1 // Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package errors
5
6 import (
7 "fmt"
8 "strings"
9 )
10
11 // StrictValidationType is the validation type for strict mode errors.
12 const StrictValidationType = "strict"
13
14 // StrictValidationSubTypes for different kinds of strict validation errors.
15 const (
16 StrictSubTypeProperty = "undeclared-property"
17 StrictSubTypeHeader = "undeclared-header"
18 StrictSubTypeQuery = "undeclared-query-param"
19 StrictSubTypeCookie = "undeclared-cookie"
20 StrictSubTypeReadOnlyProperty = "readonly-property"
21 StrictSubTypeWriteOnlyProperty = "writeonly-property"
22 )
23
24 // UndeclaredPropertyError creates a ValidationError for an undeclared property.
25 func UndeclaredPropertyError(
26 path string,
27 name string,
28 value any,
29 declaredProperties []string,
30 direction string,
31 requestPath string,
32 requestMethod string,
33 specLine int,
34 specCol int,
35 ) *ValidationError {
36 dirStr := direction
37 if dirStr == "" {
38 dirStr = "request"
39 }
40
41 return &ValidationError{
42 ValidationType: StrictValidationType,
43 ValidationSubType: StrictSubTypeProperty,
44 Message: fmt.Sprintf("%s property '%s' at '%s' is not declared in schema",
45 dirStr, name, path),
46 Reason: fmt.Sprintf("Strict mode: found property not in schema. "+
47 "Declared properties: [%s]", strings.Join(declaredProperties, ", ")),
48 HowToFix: fmt.Sprintf("Add '%s' to the schema, remove it from the %s, "+
49 "or add '%s' to StrictIgnorePaths", name, dirStr, path),
50 RequestPath: requestPath,
51 RequestMethod: requestMethod,
52 ParameterName: name,
53 Context: truncateForContext(value),
54 SpecLine: specLine,
55 SpecCol: specCol,
56 }
57 }
58
59 // UndeclaredHeaderError creates a ValidationError for an undeclared header.
60 func UndeclaredHeaderError(
61 name string,
62 value string,
63 declaredHeaders []string,
64 direction string,
65 requestPath string,
66 requestMethod string,
67 ) *ValidationError {
68 dirStr := direction
69 if dirStr == "" {
70 dirStr = "request"
71 }
72
73 return &ValidationError{
74 ValidationType: StrictValidationType,
75 ValidationSubType: StrictSubTypeHeader,
76 Message: fmt.Sprintf("%s header '%s' is not declared in specification",
77 dirStr, name),
78 Reason: fmt.Sprintf("Strict mode: found header not in spec. "+
79 "Declared headers: [%s]", strings.Join(declaredHeaders, ", ")),
80 HowToFix: fmt.Sprintf("Add '%s' to the operation's parameters, remove it from the %s, "+
81 "or add it to StrictIgnoredHeaders", name, dirStr),
82 RequestPath: requestPath,
83 RequestMethod: requestMethod,
84 ParameterName: name,
85 Context: value,
86 }
87 }
88
89 // UndeclaredQueryParamError creates a ValidationError for an undeclared query parameter.
90 func UndeclaredQueryParamError(
91 path string,
92 name string,
93 value any,
94 declaredParams []string,
95 requestPath string,
96 requestMethod string,
97 ) *ValidationError {
98 return &ValidationError{
99 ValidationType: StrictValidationType,
100 ValidationSubType: StrictSubTypeQuery,
101 Message: fmt.Sprintf("query parameter '%s' at '%s' is not declared in specification", name, path),
102 Reason: fmt.Sprintf("Strict mode: found query parameter not in spec. "+
103 "Declared parameters: [%s]", strings.Join(declaredParams, ", ")),
104 HowToFix: fmt.Sprintf("Add '%s' to the operation's query parameters, remove it from the request, "+
105 "or add '%s' to StrictIgnorePaths", name, path),
106 RequestPath: requestPath,
107 RequestMethod: requestMethod,
108 ParameterName: name,
109 Context: truncateForContext(value),
110 }
111 }
112
113 // UndeclaredCookieError creates a ValidationError for an undeclared cookie.
114 func UndeclaredCookieError(
115 path string,
116 name string,
117 value any,
118 declaredCookies []string,
119 requestPath string,
120 requestMethod string,
121 ) *ValidationError {
122 return &ValidationError{
123 ValidationType: StrictValidationType,
124 ValidationSubType: StrictSubTypeCookie,
125 Message: fmt.Sprintf("cookie '%s' at '%s' is not declared in specification", name, path),
126 Reason: fmt.Sprintf("Strict mode: found cookie not in spec. "+
127 "Declared cookies: [%s]", strings.Join(declaredCookies, ", ")),
128 HowToFix: fmt.Sprintf("Add '%s' to the operation's cookie parameters, remove it from the request, "+
129 "or add '%s' to StrictIgnorePaths", name, path),
130 RequestPath: requestPath,
131 RequestMethod: requestMethod,
132 ParameterName: name,
133 Context: truncateForContext(value),
134 }
135 }
136
137 // ReadOnlyPropertyError creates a ValidationError for a readOnly property in a request.
138 func ReadOnlyPropertyError(
139 path string,
140 name string,
141 value any,
142 requestPath string,
143 requestMethod string,
144 specLine int,
145 specCol int,
146 ) *ValidationError {
147 return &ValidationError{
148 ValidationType: StrictValidationType,
149 ValidationSubType: StrictSubTypeReadOnlyProperty,
150 Message: fmt.Sprintf("request property '%s' at '%s' is readOnly and should not be sent in the request",
151 name, path),
152 Reason: fmt.Sprintf("Strict mode: property '%s' is marked readOnly in the schema",
153 name),
154 HowToFix: fmt.Sprintf("Remove the readOnly annotation from '%s' in the schema, "+
155 "remove it from the request, or add '%s' to StrictIgnorePaths", name, path),
156 RequestPath: requestPath,
157 RequestMethod: requestMethod,
158 ParameterName: name,
159 Context: truncateForContext(value),
160 SpecLine: specLine,
161 SpecCol: specCol,
162 }
163 }
164
165 // WriteOnlyPropertyError creates a ValidationError for a writeOnly property in a response.
166 func WriteOnlyPropertyError(
167 path string,
168 name string,
169 value any,
170 requestPath string,
171 requestMethod string,
172 specLine int,
173 specCol int,
174 ) *ValidationError {
175 return &ValidationError{
176 ValidationType: StrictValidationType,
177 ValidationSubType: StrictSubTypeWriteOnlyProperty,
178 Message: fmt.Sprintf("response property '%s' at '%s' is writeOnly and should not be returned in the response",
179 name, path),
180 Reason: fmt.Sprintf("Strict mode: property '%s' is marked writeOnly in the schema",
181 name),
182 HowToFix: fmt.Sprintf("Remove the writeOnly annotation from '%s' in the schema, "+
183 "remove it from the response, or add '%s' to StrictIgnorePaths", name, path),
184 RequestPath: requestPath,
185 RequestMethod: requestMethod,
186 ParameterName: name,
187 Context: truncateForContext(value),
188 SpecLine: specLine,
189 SpecCol: specCol,
190 }
191 }
192
193 // truncateForContext creates a truncated string representation for error context.
194 func truncateForContext(v any) string {
195 switch val := v.(type) {
196 case string:
197 if len(val) > 50 {
198 return val[:47] + "..."
199 }
200 return val
201 case map[string]any:
202 return "{...}"
203 case []any:
204 return "[...]"
205 default:
206 s := fmt.Sprintf("%v", v)
207 if len(s) > 50 {
208 return s[:47] + "..."
209 }
210 return s
211 }
212 }