Skip to main content

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.ignore` was applied outside an object property.
170    ///
171    /// Error message: `{context} uses x-satay.ignore outside an object property`
172    #[error("{context} uses x-satay.ignore outside an object property")]
173    SatayIgnoreRequiresObjectProperty { context: String },
174
175    /// `x-satay.integer-type` was applied to a non-integer schema.
176    ///
177    /// 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`
178    #[error(
179        "{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"
180    )]
181    SatayIntegerTypeRequiresInteger {
182        context: String,
183        integer_type: String,
184        kind: String,
185    },
186
187    /// An array schema is missing the required `items` field.
188    ///
189    /// Error message: `{context} array schema must declare `items``
190    #[error("{context} array schema must declare `items`")]
191    MissingArrayItems { context: String },
192
193    /// A schema defines an inline object instead of using a `$ref`.
194    ///
195    /// Error message: `{context} is an inline object schema; move it to components/schemas and use `$ref``
196    #[error("{context} is an inline object schema; move it to components/schemas and use `$ref`")]
197    InlineObjectSchema { context: String },
198
199    /// An object schema has no properties (i.e. acts as a map/dictionary), which is unsupported.
200    ///
201    /// Error message: `{context} is an object with neither `properties` nor a supported `additionalProperties` schema`
202    #[error(
203        "{context} is an object with neither `properties` nor a supported `additionalProperties` schema"
204    )]
205    UnsupportedMapObjectSchema { context: String },
206
207    /// A schema uses an unsupported type.
208    ///
209    /// Error message: `{context} uses unsupported schema type `{kind}``
210    #[error("{context} uses unsupported schema type `{kind}`")]
211    UnsupportedSchemaType { context: String, kind: String },
212
213    /// A schema is missing a required `type`, `$ref`, or `enum` declaration.
214    ///
215    /// Error message: `{context} must declare `type`, `$ref`, or `enum``
216    #[error("{context} must declare `type`, `$ref`, or `enum`")]
217    MissingSchemaType { context: String },
218
219    /// A JSON Schema boolean schema was used; Satay has no IR equivalent yet.
220    ///
221    /// Error message: `{context} is a boolean schema; boolean JSON Schemas are not supported yet`
222    #[error("{context} is a boolean schema; boolean JSON Schemas are not supported yet")]
223    UnsupportedBooleanSchema { context: String },
224
225    /// A schema type array contains more than one non-null type.
226    ///
227    /// Error message: `{context} declares multiple non-null schema types; Satay supports at most one plus null`
228    #[error(
229        "{context} declares multiple non-null schema types; Satay supports at most one plus null"
230    )]
231    MultipleNonNullSchemaTypesUnsupported { context: String },
232
233    /// A schema uses a composition keyword (`allOf`, `anyOf`, `oneOf`) in an unsupported context.
234    ///
235    /// Error message: `{context} uses `{keyword}`, which is not supported in this context`
236    #[error("{context} uses `{keyword}`, which is not supported in this context")]
237    UnsupportedComposition {
238        context: String,
239        keyword: &'static str,
240    },
241
242    /// A schema uses a sibling beside `$ref` that Satay cannot apply.
243    ///
244    /// Error message: `{context} uses unsupported `$ref` sibling `{keyword}``
245    #[error("{context} uses unsupported `$ref` sibling `{keyword}`")]
246    UnsupportedRefSiblingKeyword { context: String, keyword: String },
247
248    /// An `allOf` schema combines supported struct flattening with another schema keyword.
249    ///
250    /// Error message: `{context} uses `allOf` with `{keyword}`; only object branch flattening is supported`
251    #[error("{context} uses `allOf` with `{keyword}`; only object branch flattening is supported")]
252    UnsupportedAllOfSiblingKeyword { context: String, keyword: String },
253
254    /// An `allOf` branch cannot be flattened into a generated Rust struct.
255    ///
256    /// Error message: `{context}.allOf[{index}] must be a local component schema reference or object schema with properties`
257    #[error(
258        "{context}.allOf[{index}] must be a local component schema reference or object schema with properties"
259    )]
260    UnsupportedAllOfBranch { context: String, index: usize },
261
262    /// Two `allOf` branches declare the same object property.
263    ///
264    /// Error message: `{context} declares duplicate `allOf` property `{property}``
265    #[error("{context} declares duplicate `allOf` property `{property}`")]
266    DuplicateAllOfProperty { context: String, property: String },
267
268    /// `allOf` component schemas form a recursive flattening cycle.
269    ///
270    /// Error message: `{context} forms a recursive `allOf` cycle through schema `{schema}``
271    #[error("{context} forms a recursive `allOf` cycle through schema `{schema}`")]
272    RecursiveAllOf { context: String, schema: String },
273
274    /// A discriminator union branch component recursively contains its own union.
275    ///
276    /// Error message: `{context} forms a recursive discriminator cycle through branch schema `{schema}``
277    #[error("{context} forms a recursive discriminator cycle through branch schema `{schema}`")]
278    RecursiveDiscriminatorBranch { context: String, schema: String },
279
280    /// An `anyOf` schema combines a supported union with another schema keyword.
281    ///
282    /// Error message: `{context} uses `anyOf` with `{keyword}`; only annotation siblings are supported`
283    #[error("{context} uses `anyOf` with `{keyword}`; only annotation siblings are supported")]
284    UnsupportedAnyOfSiblingKeyword { context: String, keyword: String },
285
286    /// An `anyOf` branch is not a supported union branch.
287    ///
288    /// Error message: `{context}.anyOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema`
289    #[error(
290        "{context}.anyOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema"
291    )]
292    UnsupportedAnyOfBranch { context: String, index: usize },
293
294    /// A `oneOf` schema combines a supported union with another schema keyword.
295    ///
296    /// Error message: `{context} uses `oneOf` with `{keyword}`; only annotation siblings are supported`
297    #[error("{context} uses `oneOf` with `{keyword}`; only annotation siblings are supported")]
298    UnsupportedOneOfSiblingKeyword { context: String, keyword: String },
299
300    /// A `oneOf` branch is not a supported union branch.
301    ///
302    /// Error message: `{context}.oneOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema`
303    #[error(
304        "{context}.oneOf[{index}] must be a local component schema reference, inline string enum, inline primitive schema, or null schema"
305    )]
306    UnsupportedOneOfBranch { context: String, index: usize },
307
308    /// A plain `anyOf` or `oneOf` union has more than one null branch.
309    ///
310    /// Error message: `{context}.{keyword}[{index}] duplicates the union null branch`
311    #[error("{context}.{keyword}[{index}] duplicates the union null branch")]
312    DuplicateUnionNullBranch {
313        context: String,
314        keyword: &'static str,
315        index: usize,
316    },
317
318    /// An open string enum `anyOf` repeats an enum or `const` value across branches.
319    ///
320    /// Error message: `{context} declares duplicate open string enum value `{value}` across `anyOf` branches`
321    #[error(
322        "{context} declares duplicate open string enum value `{value}` across `anyOf` branches"
323    )]
324    DuplicateOpenStringEnumValue { context: String, value: String },
325
326    /// A nullable plain `anyOf` or `oneOf` union has no non-null branches.
327    ///
328    /// Error message: `{context}.{keyword} must declare at least one non-null branch`
329    #[error("{context}.{keyword} must declare at least one non-null branch")]
330    NullableUnionWithoutVariants {
331        context: String,
332        keyword: &'static str,
333    },
334
335    /// A plain `anyOf` or `oneOf` union has a branch that is statically shadowed by an earlier branch.
336    ///
337    /// Error message: `{context}.{keyword}[{index}] is shadowed by earlier branch {shadowed_by} under ordered serde untagged deserialization`
338    #[error(
339        "{context}.{keyword}[{index}] is shadowed by earlier branch {shadowed_by} under ordered serde untagged deserialization"
340    )]
341    ShadowedUnionBranch {
342        context: String,
343        keyword: &'static str,
344        index: usize,
345        shadowed_by: usize,
346    },
347
348    /// A composition schema declares no branches.
349    ///
350    /// Raised for empty `anyOf` and, today, for empty component `allOf` that is routed
351    /// through the shared empty composition-shape check.
352    ///
353    /// Error message: `{context} must declare at least one `anyOf` branch`
354    #[error("{context} must declare at least one `anyOf` branch")]
355    EmptyAnyOf { context: String },
356
357    /// `anyOf` component schemas form a recursive union cycle.
358    ///
359    /// Error message: `{context} forms a recursive `anyOf` cycle through schema `{schema}``
360    #[error("{context} forms a recursive `anyOf` cycle through schema `{schema}`")]
361    RecursiveAnyOf { context: String, schema: String },
362
363    /// A discriminator union does not use exactly one non-empty `anyOf` or `oneOf` branch list.
364    ///
365    /// Error message: `{context} discriminator unions must declare exactly one non-empty `anyOf` or `oneOf` branch list`
366    #[error(
367        "{context} discriminator unions must declare exactly one non-empty `anyOf` or `oneOf` branch list"
368    )]
369    InvalidDiscriminatorUnion { context: String },
370
371    /// A discriminator union branch is not a local component schema reference.
372    ///
373    /// Error message: `{context}.{keyword}[{index}] must be a local component schema reference when using `discriminator``
374    #[error(
375        "{context}.{keyword}[{index}] must be a local component schema reference when using `discriminator`"
376    )]
377    UnsupportedDiscriminatorBranch {
378        context: String,
379        keyword: &'static str,
380        index: usize,
381    },
382
383    /// A discriminator union branch target does not generate as an object struct.
384    ///
385    /// Error message: `{context} discriminator branch `{schema}` must be an object struct component`
386    #[error("{context} discriminator branch `{schema}` must be an object struct component")]
387    DiscriminatorBranchNotObject { context: String, schema: String },
388
389    /// A discriminator branch object contains the discriminator property.
390    ///
391    /// Error message: `{context} discriminator branch `{schema}` contains discriminator property `{property}``
392    #[error(
393        "{context} discriminator branch `{schema}` contains discriminator property `{property}`"
394    )]
395    DiscriminatorPropertyConflict {
396        context: String,
397        schema: String,
398        property: String,
399    },
400
401    /// A discriminator branch object contains an invalid embedded discriminator property.
402    ///
403    /// Error message: `{context} discriminator branch `{schema}` property `{property}` must be {expected}`
404    #[error("{context} discriminator branch `{schema}` property `{property}` must be {expected}")]
405    InvalidDiscriminatorProperty {
406        context: String,
407        schema: String,
408        property: String,
409        expected: &'static str,
410    },
411
412    /// A discriminator mapping entry targets a non-local schema or a schema outside the union branches.
413    ///
414    /// Error message: `{context}.discriminator.mapping[{value:?}] targets `{target}`, which is not a local union branch schema`
415    #[error(
416        "{context}.discriminator.mapping[{value:?}] targets `{target}`, which is not a local union branch schema"
417    )]
418    InvalidDiscriminatorMapping {
419        context: String,
420        value: String,
421        target: String,
422    },
423
424    /// Multiple discriminator mapping values target the same union branch schema.
425    ///
426    /// Error message: `{context}.discriminator.mapping maps multiple values to branch schema `{schema}``
427    #[error("{context}.discriminator.mapping maps multiple values to branch schema `{schema}`")]
428    DuplicateDiscriminatorMapping { context: String, schema: String },
429
430    /// A discriminator mapping value disagrees with a branch's embedded discriminator property value.
431    ///
432    /// Error message: `{context}.discriminator.mapping maps value `{value}` to branch schema `{schema}`, but the branch declares discriminator value `{actual}`
433    #[error(
434        "{context}.discriminator.mapping maps value `{value}` to branch schema `{schema}`, but the branch declares discriminator value `{actual}`"
435    )]
436    DiscriminatorMappingValueMismatch {
437        context: String,
438        schema: String,
439        value: String,
440        actual: String,
441    },
442
443    /// Multiple discriminator branches resolve to the same discriminator value after implicit defaults are applied.
444    ///
445    /// Error message: `{context}.discriminator resolves multiple branch schemas to value `{value}``
446    #[error("{context}.discriminator resolves multiple branch schemas to value `{value}`")]
447    DuplicateDiscriminatorValue { context: String, value: String },
448
449    // -- Schema constraint validation --
450    /// A string schema specifies a `minLength` greater than its `maxLength`.
451    ///
452    /// Error message: `{context} has minLength {min_length} greater than maxLength {max_length}`
453    #[error("{context} has minLength {min_length} greater than maxLength {max_length}")]
454    InvalidStringLengthBounds {
455        context: String,
456        min_length: u64,
457        max_length: u64,
458    },
459
460    /// A schema uses `uniqueItems`, which cannot be enforced by generated `Vec`-backed types.
461    ///
462    /// Error message: `{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet`
463    #[error(
464        "{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet"
465    )]
466    UniqueItemsUnsupported { context: String },
467
468    /// An array schema specifies `minItems` greater than `maxItems`.
469    ///
470    /// Error message: `{context} has minItems {min_items} greater than maxItems {max_items}`
471    #[error("{context} has minItems {min_items} greater than maxItems {max_items}")]
472    InvalidArrayLengthBounds {
473        context: String,
474        min_items: u64,
475        max_items: u64,
476    },
477
478    /// A schema uses a keyword that is not safely supported.
479    ///
480    /// Error message: `{context} uses `{keyword}`, which is not safely supported yet`
481    #[error("{context} uses `{keyword}`, which is not safely supported yet")]
482    UnsupportedKeyword { context: String, keyword: String },
483
484    /// A schema keyword that must be a non-negative integer has an invalid value.
485    ///
486    /// Error message: `{context}.{keyword} must be a non-negative integer`
487    #[error("{context}.{keyword} must be a non-negative integer")]
488    InvalidNonNegativeIntegerKeyword {
489        context: String,
490        keyword: &'static str,
491    },
492
493    /// An `exclusiveMinimum`/`exclusiveMaximum` keyword is present but the corresponding bound is missing.
494    ///
495    /// Error message: `{context}.{exclusive_keyword} requires `{keyword}``
496    #[error("{context}.{exclusive_keyword} requires `{keyword}`")]
497    ExclusiveLimitRequiresBound {
498        context: String,
499        exclusive_keyword: &'static str,
500        keyword: &'static str,
501    },
502
503    /// A schema keyword that must be a finite number has a non-finite value.
504    ///
505    /// Error message: `{context}.{keyword} must be a finite number`
506    #[error("{context}.{keyword} must be a finite number")]
507    InvalidFiniteNumberKeyword {
508        context: String,
509        keyword: &'static str,
510    },
511
512    /// A value expected to be an integer is not.
513    ///
514    /// Error message: `{context} must be an integer`
515    #[error("{context} must be an integer")]
516    ExpectedInteger { context: String },
517
518    /// Integer bounds (minimum/maximum) do not permit any value.
519    ///
520    /// Error message: `{context} integer bounds do not allow any value`
521    #[error("{context} integer bounds do not allow any value")]
522    EmptyIntegerBounds { context: String },
523
524    /// An exclusive integer minimum overflows `i64`.
525    ///
526    /// Error message: `exclusive integer minimum overflows`
527    #[error("exclusive integer minimum overflows")]
528    ExclusiveIntegerMinimumOverflow,
529
530    /// An exclusive integer maximum overflows `i64`.
531    ///
532    /// Error message: `exclusive integer maximum overflows`
533    #[error("exclusive integer maximum overflows")]
534    ExclusiveIntegerMaximumOverflow,
535
536    /// Number bounds (minimum/maximum) do not permit any value.
537    ///
538    /// Error message: `{context} number bounds do not allow any value`
539    #[error("{context} number bounds do not allow any value")]
540    EmptyNumberBounds { context: String },
541
542    // -- Operation, parameter, and response validation --
543    /// An operation does not declare any responses.
544    ///
545    /// Error message: `operation `{operation_id}` must declare responses`
546    #[error("operation `{operation_id}` must declare responses")]
547    MissingOperationResponses { operation_id: String },
548
549    /// A value expected to be an array is not.
550    ///
551    /// Error message: `{context} must be an array`
552    #[error("{context} must be an array")]
553    ExpectedArray { context: String },
554
555    /// A parameter uses an unsupported location (e.g. cookie) instead of path, query, or header.
556    ///
557    /// Error message: `{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported`
558    #[error(
559        "{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported"
560    )]
561    UnsupportedParameterLocation {
562        context: String,
563        wire_name: String,
564        location: String,
565    },
566
567    /// A parameter uses `content` instead of `schema`.
568    ///
569    /// Error message: `{context} parameter `{wire_name}` uses `content`; schema parameters are required`
570    #[error("{context} parameter `{wire_name}` uses `content`; schema parameters are required")]
571    ContentParameterUnsupported { context: String, wire_name: String },
572
573    /// A parameter is missing a required `schema` declaration.
574    ///
575    /// Error message: `{context} parameter `{wire_name}` must declare schema`
576    #[error("{context} parameter `{wire_name}` must declare schema")]
577    MissingParameterSchema { context: String, wire_name: String },
578
579    /// A parameter is nullable, which is not supported.
580    ///
581    /// Error message: `parameter `{wire_name}` is nullable; nullable parameters are not supported`
582    #[error("parameter `{wire_name}` is nullable; nullable parameters are not supported")]
583    NullableParameterUnsupported { wire_name: String },
584
585    /// A parameter uses `anyOf`, which is not supported for URI/header encoding yet.
586    ///
587    /// Error message: `parameter `{wire_name}` uses `anyOf`; anyOf parameters are not supported yet`
588    #[error("parameter `{wire_name}` uses `anyOf`; anyOf parameters are not supported yet")]
589    AnyOfParameterUnsupported { wire_name: String },
590
591    /// A parameter is a map or arbitrary JSON value, which has no URI/header encoding.
592    ///
593    /// Error message: `parameter `{wire_name}` is a map or JSON value; map parameters are not supported`
594    #[error("parameter `{wire_name}` is a map or JSON value; map parameters are not supported")]
595    MapParameterUnsupported { wire_name: String },
596
597    /// A path parameter is an array, which is not supported.
598    ///
599    /// Error message: `path parameter `{wire_name}` is an array; array path parameter styles are not supported`
600    #[error(
601        "path parameter `{wire_name}` is an array; array path parameter styles are not supported"
602    )]
603    ArrayPathParameterUnsupported { wire_name: String },
604
605    /// A header parameter is an array, which is not supported.
606    ///
607    /// Error message: `header parameter `{wire_name}` is an array; array header parameter styles are not supported`
608    #[error(
609        "header parameter `{wire_name}` is an array; array header parameter styles are not supported"
610    )]
611    ArrayHeaderParameterUnsupported { wire_name: String },
612
613    /// A path parameter does not set `required: true`.
614    ///
615    /// Error message: `path parameter `{wire_name}` must set required: true`
616    #[error("path parameter `{wire_name}` must set required: true")]
617    PathParameterNotRequired { wire_name: String },
618
619    /// A context is missing a required `content` declaration.
620    ///
621    /// Error message: `{context} must declare content`
622    #[error("{context} must declare content")]
623    MissingContent { context: String },
624
625    /// A context is missing the required `application/json` content type.
626    ///
627    /// Error message: `{context} must declare application/json content`
628    #[error("{context} must declare application/json content")]
629    MissingJsonContent { context: String },
630
631    /// A context's `application/json` content is missing a schema.
632    ///
633    /// Error message: `{context} application/json content must declare schema`
634    #[error("{context} application/json content must declare schema")]
635    MissingJsonSchema { context: String },
636
637    /// A response body uses the `default` status, which is not yet supported for decoding.
638    ///
639    /// Error message: `{context} contains a default response body; default response decoding is not supported yet`
640    #[error(
641        "{context} contains a default response body; default response decoding is not supported yet"
642    )]
643    DefaultResponseBodyUnsupported { context: String },
644
645    /// A response contains an invalid HTTP status code string.
646    ///
647    /// Error message: `{context} contains invalid status code `{status}``
648    #[error("{context} contains invalid status code `{status}`")]
649    InvalidStatusCode { context: String, status: String },
650
651    /// A response contains a status code outside the valid 100–599 range.
652    ///
653    /// Error message: `{context} contains out-of-range status code `{status_code}``
654    #[error("{context} contains out-of-range status code `{status_code}`")]
655    OutOfRangeStatusCode { context: String, status_code: u16 },
656
657    /// A response for a given status code is missing `application/json` content.
658    ///
659    /// Error message: `{context} {status} response must declare application/json content`
660    #[error("{context} {status} response must declare application/json content")]
661    MissingResponseJsonContent { context: String, status: String },
662
663    /// `x-satay.output` was configured on an operation with no JSON response body.
664    ///
665    /// Error message: `operation `{operation_id}` uses x-satay.output but has no JSON response body`
666    #[error("operation `{operation_id}` uses x-satay.output but has no JSON response body")]
667    SatayOutputRequiresResponseBody { operation_id: String },
668
669    /// A response projection selector was applied to a schema that is not an object with fields.
670    ///
671    /// Error message: `{context} cannot apply x-satay.output.{selector}; expected an object schema with properties`
672    #[error(
673        "{context} cannot apply x-satay.output.{selector}; expected an object schema with properties"
674    )]
675    SatayOutputExpectedObject {
676        context: String,
677        selector: &'static str,
678    },
679
680    /// A response projection selector names a field absent from its object schema.
681    ///
682    /// Error message: `{context} x-satay.output.{selector} `{field}` does not match a declared property`
683    #[error("{context} x-satay.output.{selector} `{field}` does not match a declared property")]
684    UnknownSatayOutputField {
685        context: String,
686        selector: &'static str,
687        field: String,
688    },
689
690    /// `x-satay.output.map-field` follows an unwrapped value that is not an array.
691    ///
692    /// Error message: `{context} cannot apply x-satay.output.map-field after `{field}`; the unwrapped schema must be an array`
693    #[error(
694        "{context} cannot apply x-satay.output.map-field after `{field}`; the unwrapped schema must be an array"
695    )]
696    SatayOutputMapRequiresArray { context: String, field: String },
697
698    /// A path template contains a parameter that is never closed.
699    ///
700    /// Error message: `path `{path}` contains an unclosed parameter`
701    #[error("path `{path}` contains an unclosed parameter")]
702    UnclosedPathParameter { path: String },
703
704    /// A path template contains an empty parameter (e.g. `{}`).
705    ///
706    /// Error message: `path `{path}` contains an empty parameter`
707    #[error("path `{path}` contains an empty parameter")]
708    EmptyPathParameter { path: String },
709
710    /// A path template references a parameter that is not declared in the operation's parameters.
711    ///
712    /// Error message: `path `{path}` uses parameter `{name}` but it is not declared`
713    #[error("path `{path}` uses parameter `{name}` but it is not declared")]
714    UndeclaredPathParameter { path: String, name: String },
715
716    /// A parameter is declared for a path but never used in the path template.
717    ///
718    /// Error message: `path parameter `{name}` is declared but not used in path `{path}``
719    #[error("path parameter `{name}` is declared but not used in path `{path}`")]
720    UnusedPathParameter { path: String, name: String },
721
722    // -- Reference resolution and JSON shape validation --
723    /// A `$ref` could not be resolved because the referenced component failed validation.
724    ///
725    /// Error message: `failed to resolve reference `{reference}` in {context}: {source}`
726    #[error("failed to resolve reference `{reference}` in {context}: {source}")]
727    ResolveReference {
728        reference: String,
729        context: String,
730        #[source]
731        source: Box<ValidationError>,
732    },
733
734    /// A reference points to an external document; only local (`#`) references are supported.
735    ///
736    /// Error message: `only local references are supported`
737    #[error("only local references are supported")]
738    NonLocalReference,
739
740    /// A local reference is not a valid JSON pointer.
741    ///
742    /// Error message: `local reference must be a JSON pointer`
743    #[error("local reference must be a JSON pointer")]
744    InvalidLocalReference,
745
746    /// A JSON pointer is missing a required token segment.
747    ///
748    /// Error message: `missing `{token}``
749    #[error("missing `{token}`")]
750    MissingJsonPointerToken { token: String },
751
752    /// A `$ref` does not point to the expected `#/components/{section}/…` path.
753    ///
754    /// Error message: `reference `{reference}` must point to #/components/{section}/...`
755    #[error("reference `{reference}` must point to #/components/{section}/...")]
756    InvalidComponentReference {
757        reference: String,
758        section: &'static str,
759    },
760
761    /// A local `$ref` chain references itself.
762    ///
763    /// Error message: `circular reference `{reference}``
764    #[error("circular reference `{reference}`")]
765    CircularReference { reference: String },
766
767    /// A value expected to be an object is not.
768    ///
769    /// Error message: `{context} must be an object`
770    #[error("{context} must be an object")]
771    ExpectedObject { context: String },
772}
773
774impl ValidationError {
775    /// Wraps an [`ExtensionError`] with a codegen context string such as a schema
776    /// or operation identifier.
777    pub(crate) fn extension_error(context: &str, source: ExtensionError) -> Self {
778        match source {
779            ExtensionError::InvalidValue { path, source, .. } => {
780                ValidationError::InvalidExtension {
781                    context: context.to_owned(),
782                    path,
783                    source,
784                }
785            }
786            // `x-satay` is always a valid extension name when used by codegen.
787            // The invalid-name case only fires when a caller passes a bad name
788            // itself; preserve it here as a fallback diagnostic.
789            other => ValidationError::InvalidExtension {
790                context: context.to_owned(),
791                path: name_or_other_path(other),
792                source: SerdeError::custom("invalid specification extension"),
793            },
794        }
795    }
796}
797
798fn name_or_other_path(error: oas3::spec::ExtensionError) -> String {
799    match error {
800        ExtensionError::InvalidName { name } => name,
801        _ => "x-satay".to_owned(),
802    }
803}