Skip to main content

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    /// An `anyOf` schema combines a supported union with another schema keyword.
219    ///
220    /// Error message: `{context} uses `anyOf` with `{keyword}`; only annotation siblings are supported`
221    #[error("{context} uses `anyOf` with `{keyword}`; only annotation siblings are supported")]
222    UnsupportedAnyOfSiblingKeyword { context: String, keyword: String },
223
224    /// An `anyOf` branch is not a local component schema reference.
225    ///
226    /// Error message: `{context}.anyOf[{index}] must be a local component schema reference`
227    #[error("{context}.anyOf[{index}] must be a local component schema reference")]
228    UnsupportedAnyOfBranch { context: String, index: usize },
229
230    /// An `anyOf` schema declares no branches.
231    ///
232    /// Error message: `{context} must declare at least one `anyOf` branch`
233    #[error("{context} must declare at least one `anyOf` branch")]
234    EmptyAnyOf { context: String },
235
236    /// `anyOf` component schemas form a recursive union cycle.
237    ///
238    /// Error message: `{context} forms a recursive `anyOf` cycle through schema `{schema}``
239    #[error("{context} forms a recursive `anyOf` cycle through schema `{schema}`")]
240    RecursiveAnyOf { context: String, schema: String },
241
242    // -- Schema constraint validation --
243    /// A string schema specifies a `minLength` greater than its `maxLength`.
244    ///
245    /// Error message: `{context} has minLength {min_length} greater than maxLength {max_length}`
246    #[error("{context} has minLength {min_length} greater than maxLength {max_length}")]
247    InvalidStringLengthBounds {
248        context: String,
249        min_length: u64,
250        max_length: u64,
251    },
252
253    /// A schema uses `uniqueItems`, which cannot be enforced by generated `Vec`-backed types.
254    ///
255    /// Error message: `{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet`
256    #[error(
257        "{context} uses `uniqueItems`; generated Vec-backed types cannot enforce uniqueness yet"
258    )]
259    UniqueItemsUnsupported { context: String },
260
261    /// An array schema specifies `minItems` greater than `maxItems`.
262    ///
263    /// Error message: `{context} has minItems {min_items} greater than maxItems {max_items}`
264    #[error("{context} has minItems {min_items} greater than maxItems {max_items}")]
265    InvalidArrayLengthBounds {
266        context: String,
267        min_items: u64,
268        max_items: u64,
269    },
270
271    /// A schema uses a keyword that is not safely supported.
272    ///
273    /// Error message: `{context} uses `{keyword}`, which is not safely supported yet`
274    #[error("{context} uses `{keyword}`, which is not safely supported yet")]
275    UnsupportedKeyword {
276        context: String,
277        keyword: &'static str,
278    },
279
280    /// A schema keyword that must be a non-negative integer has an invalid value.
281    ///
282    /// Error message: `{context}.{keyword} must be a non-negative integer`
283    #[error("{context}.{keyword} must be a non-negative integer")]
284    InvalidNonNegativeIntegerKeyword {
285        context: String,
286        keyword: &'static str,
287    },
288
289    /// A schema keyword that must be a boolean has an invalid value.
290    ///
291    /// Error message: `{context}.{keyword} must be a boolean`
292    #[error("{context}.{keyword} must be a boolean")]
293    InvalidBooleanKeyword {
294        context: String,
295        keyword: &'static str,
296    },
297
298    /// An `exclusiveMinimum`/`exclusiveMaximum` keyword is present but the corresponding bound is missing.
299    ///
300    /// Error message: `{context}.{exclusive_keyword} requires `{keyword}``
301    #[error("{context}.{exclusive_keyword} requires `{keyword}`")]
302    ExclusiveLimitRequiresBound {
303        context: String,
304        exclusive_keyword: &'static str,
305        keyword: &'static str,
306    },
307
308    /// A schema keyword that must be a finite number has a non-finite value.
309    ///
310    /// Error message: `{context}.{keyword} must be a finite number`
311    #[error("{context}.{keyword} must be a finite number")]
312    InvalidFiniteNumberKeyword {
313        context: String,
314        keyword: &'static str,
315    },
316
317    /// A value expected to be an integer is not.
318    ///
319    /// Error message: `{context} must be an integer`
320    #[error("{context} must be an integer")]
321    ExpectedInteger { context: String },
322
323    /// Integer bounds (minimum/maximum) do not permit any value.
324    ///
325    /// Error message: `{context} integer bounds do not allow any value`
326    #[error("{context} integer bounds do not allow any value")]
327    EmptyIntegerBounds { context: String },
328
329    /// An exclusive integer minimum overflows `i64`.
330    ///
331    /// Error message: `exclusive integer minimum overflows`
332    #[error("exclusive integer minimum overflows")]
333    ExclusiveIntegerMinimumOverflow,
334
335    /// An exclusive integer maximum overflows `i64`.
336    ///
337    /// Error message: `exclusive integer maximum overflows`
338    #[error("exclusive integer maximum overflows")]
339    ExclusiveIntegerMaximumOverflow,
340
341    /// Number bounds (minimum/maximum) do not permit any value.
342    ///
343    /// Error message: `{context} number bounds do not allow any value`
344    #[error("{context} number bounds do not allow any value")]
345    EmptyNumberBounds { context: String },
346
347    // -- Operation, parameter, and response validation --
348    /// An operation does not declare any responses.
349    ///
350    /// Error message: `operation `{operation_id}` must declare responses`
351    #[error("operation `{operation_id}` must declare responses")]
352    MissingOperationResponses { operation_id: String },
353
354    /// A value expected to be an array is not.
355    ///
356    /// Error message: `{context} must be an array`
357    #[error("{context} must be an array")]
358    ExpectedArray { context: String },
359
360    /// A parameter uses an unsupported location (e.g. cookie) instead of path, query, or header.
361    ///
362    /// Error message: `{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported`
363    #[error(
364        "{context} parameter `{wire_name}` is in `{location}`; only path, query, and header parameters are supported"
365    )]
366    UnsupportedParameterLocation {
367        context: String,
368        wire_name: String,
369        location: String,
370    },
371
372    /// A parameter uses `content` instead of `schema`.
373    ///
374    /// Error message: `{context} parameter `{wire_name}` uses `content`; schema parameters are required`
375    #[error("{context} parameter `{wire_name}` uses `content`; schema parameters are required")]
376    ContentParameterUnsupported { context: String, wire_name: String },
377
378    /// A parameter is missing a required `schema` declaration.
379    ///
380    /// Error message: `{context} parameter `{wire_name}` must declare schema`
381    #[error("{context} parameter `{wire_name}` must declare schema")]
382    MissingParameterSchema { context: String, wire_name: String },
383
384    /// A parameter is nullable, which is not supported.
385    ///
386    /// Error message: `parameter `{wire_name}` is nullable; nullable parameters are not supported`
387    #[error("parameter `{wire_name}` is nullable; nullable parameters are not supported")]
388    NullableParameterUnsupported { wire_name: String },
389
390    /// A parameter uses `anyOf`, which is not supported for URI/header encoding yet.
391    ///
392    /// Error message: `parameter `{wire_name}` uses `anyOf`; anyOf parameters are not supported yet`
393    #[error("parameter `{wire_name}` uses `anyOf`; anyOf parameters are not supported yet")]
394    AnyOfParameterUnsupported { wire_name: String },
395
396    /// A path parameter is an array, which is not supported.
397    ///
398    /// Error message: `path parameter `{wire_name}` is an array; array path parameter styles are not supported`
399    #[error(
400        "path parameter `{wire_name}` is an array; array path parameter styles are not supported"
401    )]
402    ArrayPathParameterUnsupported { wire_name: String },
403
404    /// A header parameter is an array, which is not supported.
405    ///
406    /// Error message: `header parameter `{wire_name}` is an array; array header parameter styles are not supported`
407    #[error(
408        "header parameter `{wire_name}` is an array; array header parameter styles are not supported"
409    )]
410    ArrayHeaderParameterUnsupported { wire_name: String },
411
412    /// A path parameter does not set `required: true`.
413    ///
414    /// Error message: `path parameter `{wire_name}` must set required: true`
415    #[error("path parameter `{wire_name}` must set required: true")]
416    PathParameterNotRequired { wire_name: String },
417
418    /// A context is missing a required `content` declaration.
419    ///
420    /// Error message: `{context} must declare content`
421    #[error("{context} must declare content")]
422    MissingContent { context: String },
423
424    /// A context is missing the required `application/json` content type.
425    ///
426    /// Error message: `{context} must declare application/json content`
427    #[error("{context} must declare application/json content")]
428    MissingJsonContent { context: String },
429
430    /// A context's `application/json` content is missing a schema.
431    ///
432    /// Error message: `{context} application/json content must declare schema`
433    #[error("{context} application/json content must declare schema")]
434    MissingJsonSchema { context: String },
435
436    /// A response body uses the `default` status, which is not yet supported for decoding.
437    ///
438    /// Error message: `{context} contains a default response body; default response decoding is not supported yet`
439    #[error(
440        "{context} contains a default response body; default response decoding is not supported yet"
441    )]
442    DefaultResponseBodyUnsupported { context: String },
443
444    /// A response contains an invalid HTTP status code string.
445    ///
446    /// Error message: `{context} contains invalid status code `{status}``
447    #[error("{context} contains invalid status code `{status}`")]
448    InvalidStatusCode { context: String, status: String },
449
450    /// A response contains a status code outside the valid 100–599 range.
451    ///
452    /// Error message: `{context} contains out-of-range status code `{status_code}``
453    #[error("{context} contains out-of-range status code `{status_code}`")]
454    OutOfRangeStatusCode { context: String, status_code: u16 },
455
456    /// A response for a given status code is missing `application/json` content.
457    ///
458    /// Error message: `{context} {status} response must declare application/json content`
459    #[error("{context} {status} response must declare application/json content")]
460    MissingResponseJsonContent { context: String, status: String },
461
462    /// A path template contains a parameter that is never closed.
463    ///
464    /// Error message: `path `{path}` contains an unclosed parameter`
465    #[error("path `{path}` contains an unclosed parameter")]
466    UnclosedPathParameter { path: String },
467
468    /// A path template contains an empty parameter (e.g. `{}`).
469    ///
470    /// Error message: `path `{path}` contains an empty parameter`
471    #[error("path `{path}` contains an empty parameter")]
472    EmptyPathParameter { path: String },
473
474    /// A path template references a parameter that is not declared in the operation's parameters.
475    ///
476    /// Error message: `path `{path}` uses parameter `{name}` but it is not declared`
477    #[error("path `{path}` uses parameter `{name}` but it is not declared")]
478    UndeclaredPathParameter { path: String, name: String },
479
480    /// A parameter is declared for a path but never used in the path template.
481    ///
482    /// Error message: `path parameter `{name}` is declared but not used in path `{path}``
483    #[error("path parameter `{name}` is declared but not used in path `{path}`")]
484    UnusedPathParameter { path: String, name: String },
485
486    // -- Reference resolution and JSON shape validation --
487    /// A `$ref` could not be resolved because the referenced component failed validation.
488    ///
489    /// Error message: `failed to resolve reference `{reference}` in {context}: {source}`
490    #[error("failed to resolve reference `{reference}` in {context}: {source}")]
491    ResolveReference {
492        reference: String,
493        context: String,
494        #[source]
495        source: Box<ValidationError>,
496    },
497
498    /// A reference points to an external document; only local (`#`) references are supported.
499    ///
500    /// Error message: `only local references are supported`
501    #[error("only local references are supported")]
502    NonLocalReference,
503
504    /// A local reference is not a valid JSON pointer.
505    ///
506    /// Error message: `local reference must be a JSON pointer`
507    #[error("local reference must be a JSON pointer")]
508    InvalidLocalReference,
509
510    /// A JSON pointer is missing a required token segment.
511    ///
512    /// Error message: `missing `{token}``
513    #[error("missing `{token}`")]
514    MissingJsonPointerToken { token: String },
515
516    /// A `$ref` does not point to the expected `#/components/{section}/…` path.
517    ///
518    /// Error message: `reference `{reference}` must point to #/components/{section}/...`
519    #[error("reference `{reference}` must point to #/components/{section}/...")]
520    InvalidComponentReference {
521        reference: String,
522        section: &'static str,
523    },
524
525    /// A local `$ref` chain references itself.
526    ///
527    /// Error message: `circular reference `{reference}``
528    #[error("circular reference `{reference}`")]
529    CircularReference { reference: String },
530
531    /// A value expected to be an object is not.
532    ///
533    /// Error message: `{context} must be an object`
534    #[error("{context} must be an object")]
535    ExpectedObject { context: String },
536
537    /// A nested field expected to be an object is not.
538    ///
539    /// Error message: `{context}.{field} must be an object`
540    #[error("{context}.{field} must be an object")]
541    ExpectedObjectField {
542        context: String,
543        field: &'static str,
544    },
545}