@clov-std/elysia-openapi 1.1.0-rc.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/index.js +38 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -40,6 +40,10 @@ The reference is at `http://localhost:3000/openapi`, the JSON at `http://localho
|
|
|
40
40
|
|
|
41
41
|
Each operation gets an `operationId` derived from the method and path (`get` + `/users/:id` gives `getUsersById`), unique across the document.
|
|
42
42
|
|
|
43
|
+
### Validation errors
|
|
44
|
+
|
|
45
|
+
Every operation that validates params, query, headers, cookies or a body documents Elysia's `422`, a `validation` problem document (`application/problem+json`) added to `components.schemas`. A route that declares its own `422` keeps it.
|
|
46
|
+
|
|
43
47
|
### Response headers
|
|
44
48
|
|
|
45
49
|
```ts
|
package/dist/index.js
CHANGED
|
@@ -284,6 +284,41 @@ var mergeRequestBody = (generated, override) => {
|
|
|
284
284
|
}
|
|
285
285
|
return { ...generated, ...override, content };
|
|
286
286
|
};
|
|
287
|
+
var VALIDATION_PROBLEM = "validation";
|
|
288
|
+
var validationProblemSchema = {
|
|
289
|
+
type: "object",
|
|
290
|
+
description: "The request failed validation.",
|
|
291
|
+
required: ["type", "title", "status", "detail"],
|
|
292
|
+
properties: {
|
|
293
|
+
type: { type: "string", const: VALIDATION_PROBLEM },
|
|
294
|
+
code: { type: "string", const: VALIDATION_PROBLEM },
|
|
295
|
+
title: { type: "string", example: "Validation Error" },
|
|
296
|
+
status: { type: "integer", const: 422 },
|
|
297
|
+
detail: { type: "string", description: "The first validation error, in words." },
|
|
298
|
+
on: {
|
|
299
|
+
type: "string",
|
|
300
|
+
enum: ["params", "query", "headers", "cookie", "body"],
|
|
301
|
+
description: "The part of the request that failed."
|
|
302
|
+
},
|
|
303
|
+
property: { type: "string", description: "Path of the first invalid property." },
|
|
304
|
+
found: { description: "The value received." },
|
|
305
|
+
expected: { description: "A value of the expected shape." },
|
|
306
|
+
errors: {
|
|
307
|
+
type: "array",
|
|
308
|
+
description: "Every validation error.",
|
|
309
|
+
items: { type: "object" }
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
var validationResponse = {
|
|
314
|
+
description: "Unprocessable Content",
|
|
315
|
+
content: {
|
|
316
|
+
"application/problem+json": {
|
|
317
|
+
schema: { $ref: `#/components/schemas/${VALIDATION_PROBLEM}` }
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
var validationComponent = (operations) => operations.some((operation) => operation.responses?.["422"] === validationResponse) ? { [VALIDATION_PROBLEM]: validationProblemSchema } : {};
|
|
287
322
|
var toOperation = (route, models, method) => {
|
|
288
323
|
const { hooks } = route;
|
|
289
324
|
const parameters = new Map;
|
|
@@ -306,6 +341,8 @@ var toOperation = (route, models, method) => {
|
|
|
306
341
|
const allParameters = mergeParameters([...parameters.values()], parameterOverrides);
|
|
307
342
|
const requestBody = mergeRequestBody(toRequestBody(hooks, models, method), requestBodyOverride);
|
|
308
343
|
const responses = mergeResponses(toResponses(route, models), responseOverrides);
|
|
344
|
+
if ((parameters.size > 0 || requestBody) && !responses["422"])
|
|
345
|
+
responses["422"] = validationResponse;
|
|
309
346
|
return {
|
|
310
347
|
...allParameters.length > 0 && { parameters: allParameters },
|
|
311
348
|
...requestBody && { requestBody },
|
|
@@ -474,6 +511,7 @@ var buildDocument = (app, base = {}) => {
|
|
|
474
511
|
...base.components,
|
|
475
512
|
schemas: {
|
|
476
513
|
...base.components?.schemas,
|
|
514
|
+
...validationComponent([...generated.keys()]),
|
|
477
515
|
...Object.fromEntries(Object.entries(models).map(([name, model]) => [
|
|
478
516
|
name,
|
|
479
517
|
toSchema(model)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clov-std/elysia-openapi",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "OpenAPI 3.2 document and Scalar reference for Elysia, generated from your route schemas.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"elysia",
|
|
@@ -36,10 +36,10 @@
|
|
|
36
36
|
"test": "bun test --pass-with-no-tests --coverage"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@types/bun": "^1.4.
|
|
40
|
-
"oxfmt": "0.
|
|
41
|
-
"oxlint": "1.
|
|
42
|
-
"oxlint-tsgolint": "7.0.
|
|
39
|
+
"@types/bun": "^1.4.4",
|
|
40
|
+
"oxfmt": "0.75.0",
|
|
41
|
+
"oxlint": "1.90.0",
|
|
42
|
+
"oxlint-tsgolint": "7.0.2005",
|
|
43
43
|
"typescript": "^7.0.2"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|