satay_codegen/error/validation.rs
1use oas3::spec::ExtensionError;
2use serde::de::Error as SerdeError;
3
4/// Errors that can occur while validating an OpenAPI document.
5///
6/// This enum is [`non_exhaustive`](https://doc.rust-lang.org/reference/attributes/type_system.html)
7/// so new variants may be added in future releases without a semver break.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum ValidationError {
11 // -- Document and component shape validation --
12 /// The OpenAPI version is not supported.
13 ///
14 /// Error message: `unsupported OpenAPI version `{version}`; Satay supports OpenAPI 3.1`
15 #[error("unsupported OpenAPI version `{version}`; Satay supports OpenAPI 3.1")]
16 UnsupportedOpenApiVersion { version: String },
17
18 /// A schema component uses a type that is not supported.
19 ///
20 /// Error message: `unsupported type `{kind}` in schema `{schema}``
21 #[error("unsupported type `{kind}` in schema `{schema}`")]
22 UnsupportedComponentType { schema: String, kind: String },
23
24 /// A schema component is missing a required `type`, `$ref`, `enum`, or `properties` declaration.
25 ///
26 /// Error message: `schema `{schema}` must declare `type`, `$ref`, `enum`, or `properties``
27 #[error("schema `{schema}` must declare `type`, `$ref`, `enum`, or `properties`")]
28 MissingComponentSchemaType { schema: String },
29
30 /// An object schema is missing the required `properties` field.
31 ///
32 /// Error message: `object schema `{schema}` must declare `properties``
33 #[error("object schema `{schema}` must declare `properties`")]
34 MissingObjectProperties { schema: String },
35
36 /// The OpenAPI document is missing the required `paths` field.
37 ///
38 /// Error message: `OpenAPI document must declare `paths``
39 #[error("OpenAPI document must declare `paths`")]
40 MissingPaths,
41
42 // -- Enum and schema type validation --
43 /// A schema uses an enum with a non-string type.
44 ///
45 /// Error message: `{context} uses enum type `{kind}`; only string enums are supported`
46 #[error("{context} uses enum type `{kind}`; only string enums are supported")]
47 UnsupportedEnumType { context: String, kind: String },
48
49 /// A schema declares an enum that is not an array.
50 ///
51 /// Error message: `{context} has a non-array enum`
52 #[error("{context} has a non-array enum")]
53 NonArrayEnum { context: String },
54
55 /// A schema declares an enum with no values.
56 ///
57 /// Error message: `{context} has an empty enum`
58 #[error("{context} has an empty enum")]
59 EmptyEnum { context: String },
60
61 /// A schema enum contains a non-string value.
62 ///
63 /// Error message: `{context} contains a non-string enum value; only string enums are supported`
64 #[error("{context} contains a non-string enum value; only string enums are supported")]
65 NonStringEnumValue { context: String },
66
67 /// A schema declares a `const` value that is not one of its `enum` values.
68 ///
69 /// Error message: `{context} declares a `const` value that is not in its `enum``
70 #[error("{context} declares a `const` value that is not in its `enum`")]
71 ConstNotInEnum { context: String },
72
73 /// A typed specification extension could not be deserialized.
74 ///
75 /// Error message: `{context}: failed to deserialize specification extension at `{path}`: {source}`
76 #[error("{context}: failed to deserialize specification extension at `{path}`: {source}")]
77 InvalidExtension {
78 context: String,
79 path: String,
80 source: serde_json::Error,
81 },
82
83 /// An `x-satay.enum-variants` entry points at a value that is not in the enum.
84 ///
85 /// Error message: `{context}.x-satay.enum-variants contains `{wire_name}`, which is not declared in the enum`
86 #[error(
87 "{context}.x-satay.enum-variants contains `{wire_name}`, which is not declared in the enum"
88 )]
89 UnknownSatayEnumVariantValue { context: String, wire_name: String },
90
91 /// An `x-satay.enum-variants` entry uses a name reserved for generated fallback variants.
92 ///
93 /// Error message: `{context}.x-satay.enum-variants[{wire_name:?}] uses reserved fallback variant `{rust_name}``
94 #[error(
95 "{context}.x-satay.enum-variants[{wire_name:?}] uses reserved fallback variant `{rust_name}`"
96 )]
97 ReservedSatayEnumVariantName {
98 context: String,
99 wire_name: String,
100 rust_name: String,
101 },
102
103 /// Two `x-satay.enum-variants` entries produce the same Rust variant name.
104 ///
105 /// Error message: `{context}.x-satay.enum-variants maps multiple values to `{rust_name}``
106 #[error("{context}.x-satay.enum-variants maps multiple values to `{rust_name}`")]
107 DuplicateSatayEnumVariantName { context: String, rust_name: String },
108
109 /// A schema has a `required` field that is not an array.
110 ///
111 /// Error message: `{context} has a non-array `required` field`
112 #[error("{context} has a non-array `required` field")]
113 NonArrayRequired { context: String },
114
115 /// A schema `required` array contains a non-string element.
116 ///
117 /// Error message: `{context} has a non-string required field name`
118 #[error("{context} has a non-string required field name")]
119 NonStringRequiredField { context: String },
120
121 /// An integer schema uses an unsupported format.
122 ///
123 /// Error message: `{context} uses unsupported integer format `{format}``
124 #[error("{context} uses unsupported integer format `{format}`")]
125 UnsupportedIntegerFormat { context: String, format: String },
126
127 /// A number schema uses an unsupported format.
128 ///
129 /// Error message: `{context} uses unsupported number format `{format}``
130 #[error("{context} uses unsupported number format `{format}`")]
131 UnsupportedNumberFormat { context: String, format: String },
132
133 /// `x-satay.parse-as` was applied to an unsupported wire schema.
134 ///
135 /// 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`
136 #[error(
137 "{context} uses x-satay.parse-as `{parse_as}` on `{kind}`; supported parse-as wire schemas are string schemas, plus integer schemas for bool"
138 )]
139 SatayParseAsRequiresString {
140 context: String,
141 parse_as: String,
142 kind: String,
143 },
144
145 /// An `x-satay.none-if` array is empty.
146 ///
147 /// Error message: `{context}.x-satay.none-if must contain at least one string`
148 #[error("{context}.x-satay.none-if must contain at least one string")]
149 EmptySatayNoneIf { context: String },
150
151 /// `x-satay.none-if` was applied outside a struct property.
152 ///
153 /// Error message: `{context} uses x-satay.none-if outside a struct field`
154 #[error("{context} uses x-satay.none-if outside a struct field")]
155 SatayNoneIfRequiresStructField { context: String },
156
157 /// `x-satay.none-if` was not paired with a string-backed parser.
158 ///
159 /// Error message: `{context} uses x-satay.none-if without a string-backed x-satay.parse-as`
160 #[error("{context} uses x-satay.none-if without a string-backed x-satay.parse-as")]
161 SatayNoneIfRequiresParsedString { context: String },
162
163 /// `x-satay.none-if` and `x-satay.treat-error-as-none` were combined.
164 ///
165 /// Error message: `{context} cannot combine x-satay.none-if with x-satay.treat-error-as-none`
166 #[error("{context} cannot combine x-satay.none-if with x-satay.treat-error-as-none")]
167 ConflictingSatayNoneHandling { context: String },
168
169 /// `x-satay.integer-type` was applied to a non-integer schema.
170 ///
171 /// 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`
172 #[error(
173 "{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"
174 )]
175 SatayIntegerTypeRequiresInteger {
176 context: String,
177 integer_type: String,
178 kind: String,
179 },
180
181 /// An array schema is missing the required `items` field.
182 ///
183 /// Error message: `{context} array schema must declare `items``
184 #[error("{context} array schema must declare `items`")]
185 MissingArrayItems { context: String },
186
187 /// A schema defines an inline object instead of using a `$ref`.
188 ///
189 /// Error message: `{context} is an inline object schema; move it to components/schemas and use `$ref``
190 #[error("{context} is an inline object schema; move it to components/schemas and use `$ref`")]
191 InlineObjectSchema { context: String },
192
193 /// An object schema has no properties (i.e. acts as a map/dictionary), which is unsupported.
194 ///
195 /// Error message: `{context} is an object with neither `properties` nor a supported `additionalProperties` schema`
196 #[error(
197 "{context} is an object with neither `properties` nor a supported `additionalProperties` schema"
198 )]
199 UnsupportedMapObjectSchema { context: String },
200
201 /// A schema uses an unsupported type.
202 ///
203 /// Error message: `{context} uses unsupported schema type `{kind}``
204 #[error("{context} uses unsupported schema type `{kind}`")]
205 UnsupportedSchemaType { context: String, kind: String },
206
207 /// A schema is missing a required `type`, `$ref`, or `enum` declaration.
208 ///
209 /// Error message: `{context} must declare `type`, `$ref`, or `enum``
210 #[error("{context} must declare `type`, `$ref`, or `enum`")]
211 MissingSchemaType { context: String },
212
213 /// A JSON Schema boolean schema was used; Satay has no IR equivalent yet.
214 ///
215 /// Error message: `{context} is a boolean schema; boolean JSON Schemas are not supported yet`
216 #[error("{context} is a boolean schema; boolean JSON Schemas are not supported yet")]
217 UnsupportedBooleanSchema { context: String },
218
219 /// A schema type array contains more than one non-null type.
220 ///
221 /// Error message: `{context} declares multiple non-null schema types; Satay supports at most one plus null`
222 #[error(
223 "{context} declares multiple non-null schema types; Satay supports at most one plus null"
224 )]
225 MultipleNonNullSchemaTypesUnsupported { context: String },
226
227 /// A schema uses a composition keyword (`allOf`, `anyOf`, `oneOf`) in an unsupported context.
228 ///
229 /// Error message: `{context} uses `{keyword}`, which is not supported in this context`
230 #[error("{context} uses `{keyword}`, which is not supported in this context")]
231 UnsupportedComposition {
232 context: String,
233 keyword: &'static str,
234 },
235
236 /// A schema uses a sibling beside `$ref` that Satay cannot apply.
237 ///
238 /// Error message: `{context} uses unsupported `$ref` sibling `{keyword}``
239 #[error("{context} uses unsupported `$ref` sibling `{keyword}`")]
240 UnsupportedRefSiblingKeyword { context: String, keyword: String },
241
242 /// An `allOf` schema combines supported struct flattening with another schema keyword.
243 ///
244 /// Error message: `{context} uses `allOf` with `{keyword}`; only object branch flattening is supported`
245 #[error("{context} uses `allOf` with `{keyword}`; only object branch flattening is supported")]
246 UnsupportedAllOfSiblingKeyword { context: String, keyword: String },
247
248 /// An `allOf` branch cannot be flattened into a generated Rust struct.
249 ///
250 /// Error message: `{context}.allOf[{index}] must be a local component schema reference or object schema with properties`
251 #[error(
252 "{context}.allOf[{index}] must be a local component schema reference or object schema with properties"
253 )]
254 UnsupportedAllOfBranch { context: String, index: usize },
255
256 /// Two `allOf` branches declare the same object property.
257 ///
258 /// Error message: `{context} declares duplicate `allOf` property `{property}``
259 #[error("{context} declares duplicate `allOf` property `{property}`")]
260 DuplicateAllOfProperty { context: String, property: String },
261
262 /// `allOf` component schemas form a recursive flattening cycle.
263 ///
264 /// Error message: `{context} forms a recursive `allOf` cycle through schema `{schema}``
265 #[error("{context} forms a recursive `allOf` cycle through schema `{schema}`")]
266 RecursiveAllOf { context: String, schema: String },
267
268 /// A discriminator union branch component recursively contains its own union.
269 ///
270 /// Error message: `{context} forms a recursive discriminator cycle through branch schema `{schema}``
271 #[error("{context} forms a recursive discriminator cycle through branch schema `{schema}`")]
272 RecursiveDiscriminatorBranch { context: String, schema: String },
273
274 /// An `anyOf` schema combines a supported union with another schema keyword.
275 ///
276 /// Error message: `{context} uses `anyOf` with `{keyword}`; only annotation siblings are supported`
277 #[error("{context} uses `anyOf` with `{keyword}`; only annotation siblings are supported")]
278 UnsupportedAnyOfSiblingKeyword { context: String, keyword: String },
279
280 /// An `anyOf` branch is not a supported union branch.
281 ///
282 /// Error message: `{context}.anyOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema`
283 #[error(
284 "{context}.anyOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema"
285 )]
286 UnsupportedAnyOfBranch { context: String, index: usize },
287
288 /// A `oneOf` schema combines a supported union with another schema keyword.
289 ///
290 /// Error message: `{context} uses `oneOf` with `{keyword}`; only annotation siblings are supported`
291 #[error("{context} uses `oneOf` with `{keyword}`; only annotation siblings are supported")]
292 UnsupportedOneOfSiblingKeyword { context: String, keyword: String },
293
294 /// A `oneOf` branch is not a supported union branch.
295 ///
296 /// Error message: `{context}.oneOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema`
297 #[error(
298 "{context}.oneOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema"
299 )]
300 UnsupportedOneOfBranch { context: String, index: usize },
301
302 /// A plain `anyOf` or `oneOf` union has more than one null branch.
303 ///
304 /// Error message: `{context}.{keyword}[{index}] duplicates the union null branch`
305 #[error("{context}.{keyword}[{index}] duplicates the union null branch")]
306 DuplicateUnionNullBranch {
307 context: String,
308 keyword: &'static str,
309 index: usize,
310 },
311
312 /// An open string enum `anyOf` repeats an enum or `const` value across branches.
313 ///
314 /// Error message: `{context} declares duplicate open string enum value `{value}` across `anyOf` branches`
315 #[error(
316 "{context} declares duplicate open string enum value `{value}` across `anyOf` branches"
317 )]
318 DuplicateOpenStringEnumValue { context: String, value: String },
319
320 /// A nullable plain `anyOf` or `oneOf` union has no non-null branches.
321 ///
322 /// Error message: `{context}.{keyword} must declare at least one non-null branch`
323 #[error("{context}.{keyword} must declare at least one non-null branch")]
324 NullableUnionWithoutVariants {
325 context: String,
326 keyword: &'static str,
327 },
328
329 /// A plain `anyOf` or `oneOf` union has a branch that is statically shadowed by an earlier branch.
330 ///
331 /// Error message: `{context}.{keyword}[{index}] is shadowed by earlier branch {shadowed_by} under ordered serde untagged deserialization`
332 #[error(
333 "{context}.{keyword}[{index}] is shadowed by earlier branch {shadowed_by} under ordered serde untagged deserialization"
334 )]
335 ShadowedUnionBranch {
336 context: String,
337 keyword: &'static str,
338 index: usize,
339 shadowed_by: usize,
340 },
341
342 /// A composition schema declares no branches.
343 ///
344 /// Raised for empty `anyOf` and, today, for empty component `allOf` that is routed
345 /// through the shared empty composition-shape check.
346 ///
347 /// Error message: `{context} must declare at least one `anyOf` branch`
348 #[error("{context} must declare at least one `anyOf` branch")]
349 EmptyAnyOf { context: String },
350
351 /// `anyOf` component schemas form a recursive union cycle.
352 ///
353 /// Error message: `{context} forms a recursive `anyOf` cycle through schema `{schema}``
354 #[error("{context} forms a recursive `anyOf` cycle through schema `{schema}`")]
355 RecursiveAnyOf { context: String, schema: String },
356
357 /// A discriminator union does not use exactly one non-empty `anyOf` or `oneOf` branch list.
358 ///
359 /// Error message: `{context} discriminator unions must declare exactly one non-empty `anyOf` or `oneOf` branch list`
360 #[error(
361 "{context} discriminator unions must declare exactly one non-empty `anyOf` or `oneOf` branch list"
362 )]
363 InvalidDiscriminatorUnion { context: String },
364
365 /// A discriminator union branch is not a local component schema reference.
366 ///
367 /// Error message: `{context}.{keyword}[{index}] must be a local component schema reference when using `discriminator``
368 #[error(
369 "{context}.{keyword}[{index}] must be a local component schema reference when using `discriminator`"
370 )]
371 UnsupportedDiscriminatorBranch {
372 context: String,
373 keyword: &'static str,
374 index: usize,
375 },
376
377 /// A discriminator union branch target does not generate as an object struct.
378 ///
379 /// Error message: `{context} discriminator branch `{schema}` must be an object struct component`
380 #[error("{context} discriminator branch `{schema}` must be an object struct component")]
381 DiscriminatorBranchNotObject { context: String, schema: String },
382
383 /// A discriminator branch object contains the discriminator property.
384 ///
385 /// Error message: `{context} discriminator branch `{schema}` contains discriminator property `{property}``
386 #[error(
387 "{context} discriminator branch `{schema}` contains discriminator property `{property}`"
388 )]
389 DiscriminatorPropertyConflict {
390 context: String,
391 schema: String,
392 property: String,
393 },
394
395 /// A discriminator branch object contains an invalid embedded discriminator property.
396 ///
397 /// Error message: `{context} discriminator branch `{schema}` property `{property}` must be {expected}`
398 #[error("{context} discriminator branch `{schema}` property `{property}` must be {expected}")]
399 InvalidDiscriminatorProperty {
400 context: String,
401 schema: String,
402 property: String,
403 expected: &'static str,
404 },
405
406 /// A discriminator mapping entry targets a non-local schema or a schema outside the union branches.
407 ///
408 /// Error message: `{context}.discriminator.mapping[{value:?}] targets `{target}`, which is not a local union branch schema`
409 #[error(
410 "{context}.discriminator.mapping[{value:?}] targets `{target}`, which is not a local union branch schema"
411 )]
412 InvalidDiscriminatorMapping {
413 context: String,
414 value: String,
415 target: String,
416 },
417
418 /// Multiple discriminator mapping values target the same union branch schema.
419 ///
420 /// Error message: `{context}.discriminator.mapping maps multiple values to branch schema `{schema}``
421 #[error("{context}.discriminator.mapping maps multiple values to branch schema `{schema}`")]
422 DuplicateDiscriminatorMapping { context: String, schema: String },
423
424 /// A discriminator mapping value disagrees with a branch's embedded discriminator property value.
425 ///
426 /// Error message: `{context}.discriminator.mapping maps value `{value}` to branch schema `{schema}`, but the branch declares discriminator value `{actual}`
427 #[error(
428 "{context}.discriminator.mapping maps value `{value}` to branch schema `{schema}`, but the branch declares discriminator value `{actual}`"
429 )]
430 DiscriminatorMappingValueMismatch {
431 context: String,
432 schema: String,
433 value: String,
434 actual: String,
435 },
436
437 /// Multiple discriminator branches resolve to the same discriminator value after implicit defaults are applied.
438 ///
439 /// Error message: `{context}.discriminator resolves multiple branch schemas to value `{value}``
440 #[error("{context}.discriminator resolves multiple branch schemas to value `{value}`")]
441 DuplicateDiscriminatorValue { context: String, value: String },
442
443 // -- Schema constraint validation --
444 /// A string schema specifies a `minLength` greater than its `maxLength`.
445 ///
446 /// Error message: `{context} has minLength {min_length} greater than maxLength {max_length}`
447 #[error("{context} has minLength {min_length} greater than maxLength {max_length}")]
448 InvalidStringLengthBounds {
449 context: String,
450 min_length: u64,
451 max_length: u64,
452 },
453
454 /// A schema uses `uniqueItems`, which cannot be enforced by generated `Vec`-backed types.
455 ///
456 /// Error message: `{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet`
457 #[error(
458 "{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet"
459 )]
460 UniqueItemsUnsupported { context: String },
461
462 /// An array schema specifies `minItems` greater than `maxItems`.
463 ///
464 /// Error message: `{context} has minItems {min_items} greater than maxItems {max_items}`
465 #[error("{context} has minItems {min_items} greater than maxItems {max_items}")]
466 InvalidArrayLengthBounds {
467 context: String,
468 min_items: u64,
469 max_items: u64,
470 },
471
472 /// A schema uses a keyword that is not safely supported.
473 ///
474 /// Error message: `{context} uses `{keyword}`, which is not safely supported yet`
475 #[error("{context} uses `{keyword}`, which is not safely supported yet")]
476 UnsupportedKeyword { context: String, keyword: String },
477
478 /// A schema keyword that must be a non-negative integer has an invalid value.
479 ///
480 /// Error message: `{context}.{keyword} must be a non-negative integer`
481 #[error("{context}.{keyword} must be a non-negative integer")]
482 InvalidNonNegativeIntegerKeyword {
483 context: String,
484 keyword: &'static str,
485 },
486
487 /// An `exclusiveMinimum`/`exclusiveMaximum` keyword is present but the corresponding bound is missing.
488 ///
489 /// Error message: `{context}.{exclusive_keyword} requires `{keyword}``
490 #[error("{context}.{exclusive_keyword} requires `{keyword}`")]
491 ExclusiveLimitRequiresBound {
492 context: String,
493 exclusive_keyword: &'static str,
494 keyword: &'static str,
495 },
496
497 /// A schema keyword that must be a finite number has a non-finite value.
498 ///
499 /// Error message: `{context}.{keyword} must be a finite number`
500 #[error("{context}.{keyword} must be a finite number")]
501 InvalidFiniteNumberKeyword {
502 context: String,
503 keyword: &'static str,
504 },
505
506 /// A value expected to be an integer is not.
507 ///
508 /// Error message: `{context} must be an integer`
509 #[error("{context} must be an integer")]
510 ExpectedInteger { context: String },
511
512 /// Integer bounds (minimum/maximum) do not permit any value.
513 ///
514 /// Error message: `{context} integer bounds do not allow any value`
515 #[error("{context} integer bounds do not allow any value")]
516 EmptyIntegerBounds { context: String },
517
518 /// An exclusive integer minimum overflows `i64`.
519 ///
520 /// Error message: `exclusive integer minimum overflows`
521 #[error("exclusive integer minimum overflows")]
522 ExclusiveIntegerMinimumOverflow,
523
524 /// An exclusive integer maximum overflows `i64`.
525 ///
526 /// Error message: `exclusive integer maximum overflows`
527 #[error("exclusive integer maximum overflows")]
528 ExclusiveIntegerMaximumOverflow,
529
530 /// Number bounds (minimum/maximum) do not permit any value.
531 ///
532 /// Error message: `{context} number bounds do not allow any value`
533 #[error("{context} number bounds do not allow any value")]
534 EmptyNumberBounds { context: String },
535
536 // -- Operation, parameter, and response validation --
537 /// An operation does not declare any responses.
538 ///
539 /// Error message: `operation `{operation_id}` must declare responses`
540 #[error("operation `{operation_id}` must declare responses")]
541 MissingOperationResponses { operation_id: String },
542
543 /// A value expected to be an array is not.
544 ///
545 /// Error message: `{context} must be an array`
546 #[error("{context} must be an array")]
547 ExpectedArray { context: String },
548
549 /// A parameter uses an unsupported location (e.g. cookie) instead of path, query, or header.
550 ///
551 /// Error message: `{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported`
552 #[error(
553 "{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported"
554 )]
555 UnsupportedParameterLocation {
556 context: String,
557 wire_name: String,
558 location: String,
559 },
560
561 /// A parameter uses `content` instead of `schema`.
562 ///
563 /// Error message: `{context} parameter `{wire_name}` uses `content`; schema parameters are required`
564 #[error("{context} parameter `{wire_name}` uses `content`; schema parameters are required")]
565 ContentParameterUnsupported { context: String, wire_name: String },
566
567 /// A parameter is missing a required `schema` declaration.
568 ///
569 /// Error message: `{context} parameter `{wire_name}` must declare schema`
570 #[error("{context} parameter `{wire_name}` must declare schema")]
571 MissingParameterSchema { context: String, wire_name: String },
572
573 /// A parameter is nullable, which is not supported.
574 ///
575 /// Error message: `parameter `{wire_name}` is nullable; nullable parameters are not supported`
576 #[error("parameter `{wire_name}` is nullable; nullable parameters are not supported")]
577 NullableParameterUnsupported { wire_name: String },
578
579 /// A parameter uses `anyOf`, which is not supported for URI/header encoding yet.
580 ///
581 /// Error message: `parameter `{wire_name}` uses `anyOf`; anyOf parameters are not supported yet`
582 #[error("parameter `{wire_name}` uses `anyOf`; anyOf parameters are not supported yet")]
583 AnyOfParameterUnsupported { wire_name: String },
584
585 /// A parameter is a map or arbitrary JSON value, which has no URI/header encoding.
586 ///
587 /// Error message: `parameter `{wire_name}` is a map or JSON value; map parameters are not supported`
588 #[error("parameter `{wire_name}` is a map or JSON value; map parameters are not supported")]
589 MapParameterUnsupported { wire_name: String },
590
591 /// A path parameter is an array, which is not supported.
592 ///
593 /// Error message: `path parameter `{wire_name}` is an array; array path parameter styles are not supported`
594 #[error(
595 "path parameter `{wire_name}` is an array; array path parameter styles are not supported"
596 )]
597 ArrayPathParameterUnsupported { wire_name: String },
598
599 /// A header parameter is an array, which is not supported.
600 ///
601 /// Error message: `header parameter `{wire_name}` is an array; array header parameter styles are not supported`
602 #[error(
603 "header parameter `{wire_name}` is an array; array header parameter styles are not supported"
604 )]
605 ArrayHeaderParameterUnsupported { wire_name: String },
606
607 /// A path parameter does not set `required: true`.
608 ///
609 /// Error message: `path parameter `{wire_name}` must set required: true`
610 #[error("path parameter `{wire_name}` must set required: true")]
611 PathParameterNotRequired { wire_name: String },
612
613 /// A context is missing a required `content` declaration.
614 ///
615 /// Error message: `{context} must declare content`
616 #[error("{context} must declare content")]
617 MissingContent { context: String },
618
619 /// A context is missing the required `application/json` content type.
620 ///
621 /// Error message: `{context} must declare application/json content`
622 #[error("{context} must declare application/json content")]
623 MissingJsonContent { context: String },
624
625 /// A context's `application/json` content is missing a schema.
626 ///
627 /// Error message: `{context} application/json content must declare schema`
628 #[error("{context} application/json content must declare schema")]
629 MissingJsonSchema { context: String },
630
631 /// A response body uses the `default` status, which is not yet supported for decoding.
632 ///
633 /// Error message: `{context} contains a default response body; default response decoding is not supported yet`
634 #[error(
635 "{context} contains a default response body; default response decoding is not supported yet"
636 )]
637 DefaultResponseBodyUnsupported { context: String },
638
639 /// A response contains an invalid HTTP status code string.
640 ///
641 /// Error message: `{context} contains invalid status code `{status}``
642 #[error("{context} contains invalid status code `{status}`")]
643 InvalidStatusCode { context: String, status: String },
644
645 /// A response contains a status code outside the valid 100–599 range.
646 ///
647 /// Error message: `{context} contains out-of-range status code `{status_code}``
648 #[error("{context} contains out-of-range status code `{status_code}`")]
649 OutOfRangeStatusCode { context: String, status_code: u16 },
650
651 /// A response for a given status code is missing `application/json` content.
652 ///
653 /// Error message: `{context} {status} response must declare application/json content`
654 #[error("{context} {status} response must declare application/json content")]
655 MissingResponseJsonContent { context: String, status: String },
656
657 /// A path template contains a parameter that is never closed.
658 ///
659 /// Error message: `path `{path}` contains an unclosed parameter`
660 #[error("path `{path}` contains an unclosed parameter")]
661 UnclosedPathParameter { path: String },
662
663 /// A path template contains an empty parameter (e.g. `{}`).
664 ///
665 /// Error message: `path `{path}` contains an empty parameter`
666 #[error("path `{path}` contains an empty parameter")]
667 EmptyPathParameter { path: String },
668
669 /// A path template references a parameter that is not declared in the operation's parameters.
670 ///
671 /// Error message: `path `{path}` uses parameter `{name}` but it is not declared`
672 #[error("path `{path}` uses parameter `{name}` but it is not declared")]
673 UndeclaredPathParameter { path: String, name: String },
674
675 /// A parameter is declared for a path but never used in the path template.
676 ///
677 /// Error message: `path parameter `{name}` is declared but not used in path `{path}``
678 #[error("path parameter `{name}` is declared but not used in path `{path}`")]
679 UnusedPathParameter { path: String, name: String },
680
681 // -- Reference resolution and JSON shape validation --
682 /// A `$ref` could not be resolved because the referenced component failed validation.
683 ///
684 /// Error message: `failed to resolve reference `{reference}` in {context}: {source}`
685 #[error("failed to resolve reference `{reference}` in {context}: {source}")]
686 ResolveReference {
687 reference: String,
688 context: String,
689 #[source]
690 source: Box<ValidationError>,
691 },
692
693 /// A reference points to an external document; only local (`#`) references are supported.
694 ///
695 /// Error message: `only local references are supported`
696 #[error("only local references are supported")]
697 NonLocalReference,
698
699 /// A local reference is not a valid JSON pointer.
700 ///
701 /// Error message: `local reference must be a JSON pointer`
702 #[error("local reference must be a JSON pointer")]
703 InvalidLocalReference,
704
705 /// A JSON pointer is missing a required token segment.
706 ///
707 /// Error message: `missing `{token}``
708 #[error("missing `{token}`")]
709 MissingJsonPointerToken { token: String },
710
711 /// A `$ref` does not point to the expected `#/components/{section}/…` path.
712 ///
713 /// Error message: `reference `{reference}` must point to #/components/{section}/...`
714 #[error("reference `{reference}` must point to #/components/{section}/...")]
715 InvalidComponentReference {
716 reference: String,
717 section: &'static str,
718 },
719
720 /// A local `$ref` chain references itself.
721 ///
722 /// Error message: `circular reference `{reference}``
723 #[error("circular reference `{reference}`")]
724 CircularReference { reference: String },
725
726 /// A value expected to be an object is not.
727 ///
728 /// Error message: `{context} must be an object`
729 #[error("{context} must be an object")]
730 ExpectedObject { context: String },
731}
732
733impl ValidationError {
734 /// Wraps an [`ExtensionError`] with a codegen context string such as a schema
735 /// or operation identifier.
736 pub(crate) fn extension_error(context: &str, source: ExtensionError) -> Self {
737 match source {
738 ExtensionError::InvalidValue { path, source, .. } => {
739 ValidationError::InvalidExtension {
740 context: context.to_owned(),
741 path,
742 source,
743 }
744 }
745 // `x-satay` is always a valid extension name when used by codegen.
746 // The invalid-name case only fires when a caller passes a bad name
747 // itself; preserve it here as a fallback diagnostic.
748 other => ValidationError::InvalidExtension {
749 context: context.to_owned(),
750 path: name_or_other_path(other),
751 source: SerdeError::custom("invalid specification extension"),
752 },
753 }
754 }
755}
756
757fn name_or_other_path(error: oas3::spec::ExtensionError) -> String {
758 match error {
759 ExtensionError::InvalidName { name } => name,
760 _ => "x-satay".to_owned(),
761 }
762}