satay_codegen/error/validation.rs
1/// Errors that can occur while validating an OpenAPI document.
2///
3/// This enum is [`non_exhaustive`](https://doc.rust-lang.org/reference/attributes/type_system.html)
4/// so new variants may be added in future releases without a semver break.
5#[derive(Debug, thiserror::Error)]
6#[non_exhaustive]
7pub enum ValidationError {
8 // -- Document and component shape validation --
9 /// The OpenAPI version is not supported.
10 ///
11 /// Error message: `unsupported OpenAPI version `{version}`; Satay supports OpenAPI 3.1`
12 #[error("unsupported OpenAPI version `{version}`; Satay supports OpenAPI 3.1")]
13 UnsupportedOpenApiVersion { version: String },
14
15 /// A schema component uses a type that is not supported.
16 ///
17 /// Error message: `unsupported type `{kind}` in schema `{schema}``
18 #[error("unsupported type `{kind}` in schema `{schema}`")]
19 UnsupportedComponentType { schema: String, kind: String },
20
21 /// A schema component is missing a required `type`, `$ref`, `enum`, or `properties` declaration.
22 ///
23 /// Error message: `schema `{schema}` must declare `type`, `$ref`, `enum`, or `properties``
24 #[error("schema `{schema}` must declare `type`, `$ref`, `enum`, or `properties`")]
25 MissingComponentSchemaType { schema: String },
26
27 /// An object schema is missing the required `properties` field.
28 ///
29 /// Error message: `object schema `{schema}` must declare `properties``
30 #[error("object schema `{schema}` must declare `properties`")]
31 MissingObjectProperties { schema: String },
32
33 /// The OpenAPI document is missing the required `paths` field.
34 ///
35 /// Error message: `OpenAPI document must declare `paths``
36 #[error("OpenAPI document must declare `paths`")]
37 MissingPaths,
38
39 // -- Enum and schema type validation --
40 /// A schema uses an enum with a non-string type.
41 ///
42 /// Error message: `{context} uses enum type `{kind}`; only string enums are supported`
43 #[error("{context} uses enum type `{kind}`; only string enums are supported")]
44 UnsupportedEnumType { context: String, kind: String },
45
46 /// A schema declares an enum that is not an array.
47 ///
48 /// Error message: `{context} has a non-array enum`
49 #[error("{context} has a non-array enum")]
50 NonArrayEnum { context: String },
51
52 /// A schema declares an enum with no values.
53 ///
54 /// Error message: `{context} has an empty enum`
55 #[error("{context} has an empty enum")]
56 EmptyEnum { context: String },
57
58 /// A schema enum contains a non-string value.
59 ///
60 /// Error message: `{context} contains a non-string enum value; only string enums are supported`
61 #[error("{context} contains a non-string enum value; only string enums are supported")]
62 NonStringEnumValue { context: String },
63
64 /// An `x-satay.enum-variants` value is not an object.
65 ///
66 /// Error message: `{context}.x-satay.enum-variants must be an object`
67 #[error("{context}.x-satay.enum-variants must be an object")]
68 InvalidSatayEnumVariants { context: String },
69
70 /// An `x-satay.enum-variants` entry points at a value that is not in the enum.
71 ///
72 /// Error message: `{context}.x-satay.enum-variants contains `{wire_name}`, which is not declared in the enum`
73 #[error(
74 "{context}.x-satay.enum-variants contains `{wire_name}`, which is not declared in the enum"
75 )]
76 UnknownSatayEnumVariantValue { context: String, wire_name: String },
77
78 /// An `x-satay.enum-variants` entry has a non-string Rust variant name.
79 ///
80 /// Error message: `{context}.x-satay.enum-variants[{wire_name:?}] must be a string`
81 #[error("{context}.x-satay.enum-variants[{wire_name:?}] must be a string")]
82 InvalidSatayEnumVariantName { context: String, wire_name: String },
83
84 /// Two `x-satay.enum-variants` entries produce the same Rust variant name.
85 ///
86 /// Error message: `{context}.x-satay.enum-variants maps multiple values to `{rust_name}``
87 #[error("{context}.x-satay.enum-variants maps multiple values to `{rust_name}`")]
88 DuplicateSatayEnumVariantName { context: String, rust_name: String },
89
90 /// A schema has a `required` field that is not an array.
91 ///
92 /// Error message: `{context} has a non-array `required` field`
93 #[error("{context} has a non-array `required` field")]
94 NonArrayRequired { context: String },
95
96 /// A schema `required` array contains a non-string element.
97 ///
98 /// Error message: `{context} has a non-string required field name`
99 #[error("{context} has a non-string required field name")]
100 NonStringRequiredField { context: String },
101
102 /// An integer schema uses an unsupported format.
103 ///
104 /// Error message: `{context} uses unsupported integer format `{format}``
105 #[error("{context} uses unsupported integer format `{format}`")]
106 UnsupportedIntegerFormat { context: String, format: String },
107
108 /// A number schema uses an unsupported format.
109 ///
110 /// Error message: `{context} uses unsupported number format `{format}``
111 #[error("{context} uses unsupported number format `{format}`")]
112 UnsupportedNumberFormat { context: String, format: String },
113
114 /// An `x-satay.parse-as` value is not a supported target type.
115 ///
116 /// Error message: `{context} uses unsupported x-satay.parse-as `{parse_as}``
117 #[error("{context} uses unsupported x-satay.parse-as `{parse_as}`")]
118 UnsupportedSatayParseAs { context: String, parse_as: String },
119
120 /// An `x-satay.parse-as` value is not a string.
121 ///
122 /// Error message: `{context}.x-satay.parse-as must be a string`
123 #[error("{context}.x-satay.parse-as must be a string")]
124 InvalidSatayParseAs { context: String },
125
126 /// `x-satay.parse-as` was applied to an unsupported wire schema.
127 ///
128 /// Error message: `{context} uses x-satay.parse-as `{parse_as}` on `{kind}`; supported parse-as wire schemas are string schemas, plus integer schemas for bool`
129 #[error(
130 "{context} uses x-satay.parse-as `{parse_as}` on `{kind}`; supported parse-as wire schemas are string schemas, plus integer schemas for bool"
131 )]
132 SatayParseAsRequiresString {
133 context: String,
134 parse_as: String,
135 kind: String,
136 },
137
138 /// An `x-satay.integer-type` value is not a supported Rust integer type.
139 ///
140 /// Error message: `{context} uses unsupported x-satay.integer-type `{integer_type}``
141 #[error("{context} uses unsupported x-satay.integer-type `{integer_type}`")]
142 UnsupportedSatayIntegerType {
143 context: String,
144 integer_type: String,
145 },
146
147 /// An `x-satay.integer-type` value is not a string.
148 ///
149 /// Error message: `{context}.x-satay.integer-type must be a string`
150 #[error("{context}.x-satay.integer-type must be a string")]
151 InvalidSatayIntegerType { context: String },
152
153 /// `x-satay.integer-type` was applied to a non-integer schema.
154 ///
155 /// Error message: `{context} uses x-satay.integer-type `{integer_type}` on `{kind}`; supported integer-type wire schemas are integer schemas and string schemas with x-satay.parse-as integer-range`
156 #[error(
157 "{context} uses x-satay.integer-type `{integer_type}` on `{kind}`; supported integer-type wire schemas are integer schemas and string schemas with x-satay.parse-as integer-range"
158 )]
159 SatayIntegerTypeRequiresInteger {
160 context: String,
161 integer_type: String,
162 kind: String,
163 },
164
165 /// An array schema is missing the required `items` field.
166 ///
167 /// Error message: `{context} array schema must declare `items``
168 #[error("{context} array schema must declare `items`")]
169 MissingArrayItems { context: String },
170
171 /// A schema defines an inline object instead of using a `$ref`.
172 ///
173 /// Error message: `{context} is an inline object schema; move it to components/schemas and use `$ref``
174 #[error("{context} is an inline object schema; move it to components/schemas and use `$ref`")]
175 InlineObjectSchema { context: String },
176
177 /// An object schema has no properties (i.e. acts as a map/dictionary), which is unsupported.
178 ///
179 /// Error message: `{context} is an object without properties; map/object schemas are not supported yet`
180 #[error("{context} is an object without properties; map/object schemas are not supported yet")]
181 UnsupportedMapObjectSchema { context: String },
182
183 /// A schema uses an unsupported type.
184 ///
185 /// Error message: `{context} uses unsupported schema type `{kind}``
186 #[error("{context} uses unsupported schema type `{kind}`")]
187 UnsupportedSchemaType { context: String, kind: String },
188
189 /// A schema is missing a required `type`, `$ref`, or `enum` declaration.
190 ///
191 /// Error message: `{context} must declare `type`, `$ref`, or `enum``
192 #[error("{context} must declare `type`, `$ref`, or `enum`")]
193 MissingSchemaType { context: String },
194
195 /// A JSON Schema boolean schema was used; Satay has no IR equivalent yet.
196 ///
197 /// Error message: `{context} is a boolean schema; boolean JSON Schemas are not supported yet`
198 #[error("{context} is a boolean schema; boolean JSON Schemas are not supported yet")]
199 UnsupportedBooleanSchema { context: String },
200
201 /// A schema type array contains more than one non-null type.
202 ///
203 /// Error message: `{context} declares multiple non-null schema types; Satay supports at most one plus null`
204 #[error(
205 "{context} declares multiple non-null schema types; Satay supports at most one plus null"
206 )]
207 MultipleNonNullSchemaTypesUnsupported { context: String },
208
209 /// A schema uses a composition keyword (`allOf`, `anyOf`, `oneOf`) that is outside MVP scope.
210 ///
211 /// Error message: `{context} uses `{keyword}`, which is not in the MVP scope`
212 #[error("{context} uses `{keyword}`, which is not in the MVP scope")]
213 UnsupportedComposition {
214 context: String,
215 keyword: &'static str,
216 },
217
218 // -- Schema constraint validation --
219 /// A string schema specifies a `minLength` greater than its `maxLength`.
220 ///
221 /// Error message: `{context} has minLength {min_length} greater than maxLength {max_length}`
222 #[error("{context} has minLength {min_length} greater than maxLength {max_length}")]
223 InvalidStringLengthBounds {
224 context: String,
225 min_length: u64,
226 max_length: u64,
227 },
228
229 /// A schema uses `uniqueItems`, which cannot be enforced by generated `Vec`-backed types.
230 ///
231 /// Error message: `{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet`
232 #[error(
233 "{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet"
234 )]
235 UniqueItemsUnsupported { context: String },
236
237 /// An array schema specifies `minItems` greater than `maxItems`.
238 ///
239 /// Error message: `{context} has minItems {min_items} greater than maxItems {max_items}`
240 #[error("{context} has minItems {min_items} greater than maxItems {max_items}")]
241 InvalidArrayLengthBounds {
242 context: String,
243 min_items: u64,
244 max_items: u64,
245 },
246
247 /// A schema uses a keyword that is not safely supported.
248 ///
249 /// Error message: `{context} uses `{keyword}`, which is not safely supported yet`
250 #[error("{context} uses `{keyword}`, which is not safely supported yet")]
251 UnsupportedKeyword {
252 context: String,
253 keyword: &'static str,
254 },
255
256 /// A schema keyword that must be a non-negative integer has an invalid value.
257 ///
258 /// Error message: `{context}.{keyword} must be a non-negative integer`
259 #[error("{context}.{keyword} must be a non-negative integer")]
260 InvalidNonNegativeIntegerKeyword {
261 context: String,
262 keyword: &'static str,
263 },
264
265 /// A schema keyword that must be a boolean has an invalid value.
266 ///
267 /// Error message: `{context}.{keyword} must be a boolean`
268 #[error("{context}.{keyword} must be a boolean")]
269 InvalidBooleanKeyword {
270 context: String,
271 keyword: &'static str,
272 },
273
274 /// An `exclusiveMinimum`/`exclusiveMaximum` keyword is present but the corresponding bound is missing.
275 ///
276 /// Error message: `{context}.{exclusive_keyword} requires `{keyword}``
277 #[error("{context}.{exclusive_keyword} requires `{keyword}`")]
278 ExclusiveLimitRequiresBound {
279 context: String,
280 exclusive_keyword: &'static str,
281 keyword: &'static str,
282 },
283
284 /// A schema keyword that must be a finite number has a non-finite value.
285 ///
286 /// Error message: `{context}.{keyword} must be a finite number`
287 #[error("{context}.{keyword} must be a finite number")]
288 InvalidFiniteNumberKeyword {
289 context: String,
290 keyword: &'static str,
291 },
292
293 /// A value expected to be an integer is not.
294 ///
295 /// Error message: `{context} must be an integer`
296 #[error("{context} must be an integer")]
297 ExpectedInteger { context: String },
298
299 /// Integer bounds (minimum/maximum) do not permit any value.
300 ///
301 /// Error message: `{context} integer bounds do not allow any value`
302 #[error("{context} integer bounds do not allow any value")]
303 EmptyIntegerBounds { context: String },
304
305 /// An exclusive integer minimum overflows `i64`.
306 ///
307 /// Error message: `exclusive integer minimum overflows`
308 #[error("exclusive integer minimum overflows")]
309 ExclusiveIntegerMinimumOverflow,
310
311 /// An exclusive integer maximum overflows `i64`.
312 ///
313 /// Error message: `exclusive integer maximum overflows`
314 #[error("exclusive integer maximum overflows")]
315 ExclusiveIntegerMaximumOverflow,
316
317 /// Number bounds (minimum/maximum) do not permit any value.
318 ///
319 /// Error message: `{context} number bounds do not allow any value`
320 #[error("{context} number bounds do not allow any value")]
321 EmptyNumberBounds { context: String },
322
323 // -- Operation, parameter, and response validation --
324 /// An operation does not declare any responses.
325 ///
326 /// Error message: `operation `{operation_id}` must declare responses`
327 #[error("operation `{operation_id}` must declare responses")]
328 MissingOperationResponses { operation_id: String },
329
330 /// A value expected to be an array is not.
331 ///
332 /// Error message: `{context} must be an array`
333 #[error("{context} must be an array")]
334 ExpectedArray { context: String },
335
336 /// A parameter uses an unsupported location (e.g. cookie) instead of path, query, or header.
337 ///
338 /// Error message: `{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported`
339 #[error(
340 "{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported"
341 )]
342 UnsupportedParameterLocation {
343 context: String,
344 wire_name: String,
345 location: String,
346 },
347
348 /// A parameter uses `content` instead of `schema`.
349 ///
350 /// Error message: `{context} parameter `{wire_name}` uses `content`; schema parameters are required`
351 #[error("{context} parameter `{wire_name}` uses `content`; schema parameters are required")]
352 ContentParameterUnsupported { context: String, wire_name: String },
353
354 /// A parameter is missing a required `schema` declaration.
355 ///
356 /// Error message: `{context} parameter `{wire_name}` must declare schema`
357 #[error("{context} parameter `{wire_name}` must declare schema")]
358 MissingParameterSchema { context: String, wire_name: String },
359
360 /// A parameter is nullable, which is not supported.
361 ///
362 /// Error message: `parameter `{wire_name}` is nullable; nullable parameters are not supported`
363 #[error("parameter `{wire_name}` is nullable; nullable parameters are not supported")]
364 NullableParameterUnsupported { wire_name: String },
365
366 /// A path parameter is an array, which is not supported.
367 ///
368 /// Error message: `path parameter `{wire_name}` is an array; array path parameter styles are not supported`
369 #[error(
370 "path parameter `{wire_name}` is an array; array path parameter styles are not supported"
371 )]
372 ArrayPathParameterUnsupported { wire_name: String },
373
374 /// A header parameter is an array, which is not supported.
375 ///
376 /// Error message: `header parameter `{wire_name}` is an array; array header parameter styles are not supported`
377 #[error(
378 "header parameter `{wire_name}` is an array; array header parameter styles are not supported"
379 )]
380 ArrayHeaderParameterUnsupported { wire_name: String },
381
382 /// A path parameter does not set `required: true`.
383 ///
384 /// Error message: `path parameter `{wire_name}` must set required: true`
385 #[error("path parameter `{wire_name}` must set required: true")]
386 PathParameterNotRequired { wire_name: String },
387
388 /// A context is missing a required `content` declaration.
389 ///
390 /// Error message: `{context} must declare content`
391 #[error("{context} must declare content")]
392 MissingContent { context: String },
393
394 /// A context is missing the required `application/json` content type.
395 ///
396 /// Error message: `{context} must declare application/json content`
397 #[error("{context} must declare application/json content")]
398 MissingJsonContent { context: String },
399
400 /// A context's `application/json` content is missing a schema.
401 ///
402 /// Error message: `{context} application/json content must declare schema`
403 #[error("{context} application/json content must declare schema")]
404 MissingJsonSchema { context: String },
405
406 /// A response body uses the `default` status, which is not yet supported for decoding.
407 ///
408 /// Error message: `{context} contains a default response body; default response decoding is not supported yet`
409 #[error(
410 "{context} contains a default response body; default response decoding is not supported yet"
411 )]
412 DefaultResponseBodyUnsupported { context: String },
413
414 /// A response contains an invalid HTTP status code string.
415 ///
416 /// Error message: `{context} contains invalid status code `{status}``
417 #[error("{context} contains invalid status code `{status}`")]
418 InvalidStatusCode { context: String, status: String },
419
420 /// A response contains a status code outside the valid 100–599 range.
421 ///
422 /// Error message: `{context} contains out-of-range status code `{status_code}``
423 #[error("{context} contains out-of-range status code `{status_code}`")]
424 OutOfRangeStatusCode { context: String, status_code: u16 },
425
426 /// A response for a given status code is missing `application/json` content.
427 ///
428 /// Error message: `{context} {status} response must declare application/json content`
429 #[error("{context} {status} response must declare application/json content")]
430 MissingResponseJsonContent { context: String, status: String },
431
432 /// A path template contains a parameter that is never closed.
433 ///
434 /// Error message: `path `{path}` contains an unclosed parameter`
435 #[error("path `{path}` contains an unclosed parameter")]
436 UnclosedPathParameter { path: String },
437
438 /// A path template contains an empty parameter (e.g. `{}`).
439 ///
440 /// Error message: `path `{path}` contains an empty parameter`
441 #[error("path `{path}` contains an empty parameter")]
442 EmptyPathParameter { path: String },
443
444 /// A path template references a parameter that is not declared in the operation's parameters.
445 ///
446 /// Error message: `path `{path}` uses parameter `{name}` but it is not declared`
447 #[error("path `{path}` uses parameter `{name}` but it is not declared")]
448 UndeclaredPathParameter { path: String, name: String },
449
450 /// A parameter is declared for a path but never used in the path template.
451 ///
452 /// Error message: `path parameter `{name}` is declared but not used in path `{path}``
453 #[error("path parameter `{name}` is declared but not used in path `{path}`")]
454 UnusedPathParameter { path: String, name: String },
455
456 // -- Reference resolution and JSON shape validation --
457 /// A `$ref` could not be resolved because the referenced component failed validation.
458 ///
459 /// Error message: `failed to resolve reference `{reference}` in {context}: {source}`
460 #[error("failed to resolve reference `{reference}` in {context}: {source}")]
461 ResolveReference {
462 reference: String,
463 context: String,
464 #[source]
465 source: Box<ValidationError>,
466 },
467
468 /// A reference points to an external document; only local (`#`) references are supported.
469 ///
470 /// Error message: `only local references are supported`
471 #[error("only local references are supported")]
472 NonLocalReference,
473
474 /// A local reference is not a valid JSON pointer.
475 ///
476 /// Error message: `local reference must be a JSON pointer`
477 #[error("local reference must be a JSON pointer")]
478 InvalidLocalReference,
479
480 /// A JSON pointer is missing a required token segment.
481 ///
482 /// Error message: `missing `{token}``
483 #[error("missing `{token}`")]
484 MissingJsonPointerToken { token: String },
485
486 /// A `$ref` does not point to the expected `#/components/{section}/…` path.
487 ///
488 /// Error message: `reference `{reference}` must point to #/components/{section}/...`
489 #[error("reference `{reference}` must point to #/components/{section}/...")]
490 InvalidComponentReference {
491 reference: String,
492 section: &'static str,
493 },
494
495 /// A local `$ref` chain references itself.
496 ///
497 /// Error message: `circular reference `{reference}``
498 #[error("circular reference `{reference}`")]
499 CircularReference { reference: String },
500
501 /// A value expected to be an object is not.
502 ///
503 /// Error message: `{context} must be an object`
504 #[error("{context} must be an object")]
505 ExpectedObject { context: String },
506
507 /// A nested field expected to be an object is not.
508 ///
509 /// Error message: `{context}.{field} must be an object`
510 #[error("{context}.{field} must be an object")]
511 ExpectedObjectField {
512 context: String,
513 field: &'static str,
514 },
515}