zod-openapi 0.3.0 → 0.4.1
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/LICENSE +1 -1
- package/README.md +52 -8
- package/lib-commonjs/create/components.js +11 -5
- package/lib-commonjs/create/components.js.map +1 -1
- package/lib-commonjs/create/document.js +1 -1
- package/lib-commonjs/create/document.js.map +1 -1
- package/lib-commonjs/create/schema/index.js +115 -132
- package/lib-commonjs/create/schema/index.js.map +1 -1
- package/lib-commonjs/create/schema/nativeEnum.js +11 -3
- package/lib-commonjs/create/schema/nativeEnum.js.map +1 -1
- package/lib-commonjs/create/schema/nullable.js +20 -16
- package/lib-commonjs/create/schema/nullable.js.map +1 -1
- package/lib-commonjs/create/schema/number.js +12 -5
- package/lib-commonjs/create/schema/number.js.map +1 -1
- package/lib-commonjs/create/schema/tuple.js +25 -5
- package/lib-commonjs/create/schema/tuple.js.map +1 -1
- package/lib-commonjs/extendZod.js.map +1 -1
- package/lib-commonjs/openapi.js +13 -0
- package/lib-commonjs/openapi.js.map +1 -0
- package/lib-es2015/create/components.js +11 -5
- package/lib-es2015/create/components.js.map +1 -1
- package/lib-es2015/create/document.js +1 -1
- package/lib-es2015/create/document.js.map +1 -1
- package/lib-es2015/create/schema/index.js +3 -3
- package/lib-es2015/create/schema/index.js.map +1 -1
- package/lib-es2015/create/schema/nativeEnum.js +11 -3
- package/lib-es2015/create/schema/nativeEnum.js.map +1 -1
- package/lib-es2015/create/schema/nullable.js +20 -15
- package/lib-es2015/create/schema/nullable.js.map +1 -1
- package/lib-es2015/create/schema/number.js +12 -5
- package/lib-es2015/create/schema/number.js.map +1 -1
- package/lib-es2015/create/schema/tuple.js +25 -5
- package/lib-es2015/create/schema/tuple.js.map +1 -1
- package/lib-es2015/extendZod.js.map +1 -1
- package/lib-es2015/openapi.js +9 -0
- package/lib-es2015/openapi.js.map +1 -0
- package/lib-types/create/components.d.ts +7 -6
- package/lib-types/create/document.d.ts +13 -11
- package/lib-types/create/schema/nativeEnum.d.ts +2 -1
- package/lib-types/create/schema/nullable.d.ts +0 -1
- package/lib-types/create/schema/number.d.ts +2 -1
- package/lib-types/extendZod.d.ts +5 -3
- package/lib-types/openapi.d.ts +3 -0
- package/package.json +9 -6
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
# zod-openapi
|
|
2
2
|
|
|
3
|
+
A Typescript library to create full OpenAPI 3 documentation from [Zod](https://github.com/colinhacks/zod) Types.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/zod-openapi)
|
|
6
|
+
[](https://www.npmjs.com/package/zod-openapi)
|
|
3
7
|
[](https://github.com/seek-oss/skuba)
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
Install via npm:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install zod zod-openapi
|
|
15
|
+
```
|
|
6
16
|
|
|
7
17
|
## API
|
|
8
18
|
|
|
@@ -288,6 +298,20 @@ Wherever `title` is used in schemas across the document, it will instead be crea
|
|
|
288
298
|
|
|
289
299
|
This can be an extremely powerful way to generate better Open API documentation. There are some Open API features like [discriminator mapping](https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/) which require all schemas in the union to contain a ref.
|
|
290
300
|
|
|
301
|
+
To display components which are not referenced by simply add the Zod Schema to the schema components directly.
|
|
302
|
+
|
|
303
|
+
eg.
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
{
|
|
307
|
+
"components": {
|
|
308
|
+
"schemas": {
|
|
309
|
+
MyJobSchema // note: this will register this Zod Schema as MyJobSchema unless `ref` is specified on the type
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
291
315
|
#### Parameters
|
|
292
316
|
|
|
293
317
|
Query, Path, Header & Cookie parameters can be similarly registered:
|
|
@@ -312,6 +336,14 @@ const header = z.string().openapi({
|
|
|
312
336
|
});
|
|
313
337
|
```
|
|
314
338
|
|
|
339
|
+
## Supported OpenAPI Versions
|
|
340
|
+
|
|
341
|
+
- '3.0.0'
|
|
342
|
+
- '3.0.1'
|
|
343
|
+
- '3.0.2'
|
|
344
|
+
- '3.0.3'
|
|
345
|
+
- '3.1.0'
|
|
346
|
+
|
|
315
347
|
## Supported Zod Schema
|
|
316
348
|
|
|
317
349
|
- ZodArray
|
|
@@ -352,6 +384,10 @@ eg.
|
|
|
352
384
|
z.custom().openapi({ type: 'string' });
|
|
353
385
|
```
|
|
354
386
|
|
|
387
|
+
## Ecosystem
|
|
388
|
+
|
|
389
|
+
- [eslint-plugin-zod-openapi](https://github.com/samchungy/eslint-plugin-zod-openapi) - Eslint rules for zod-openapi. This includes features which can autogenerate Typescript comments for your Zod types based on your `description`, `example` and `deprecated` fields.
|
|
390
|
+
|
|
355
391
|
## Development
|
|
356
392
|
|
|
357
393
|
### Prerequisites
|
|
@@ -379,15 +415,23 @@ yarn format
|
|
|
379
415
|
yarn lint
|
|
380
416
|
```
|
|
381
417
|
|
|
382
|
-
###
|
|
418
|
+
### Release
|
|
383
419
|
|
|
384
|
-
|
|
385
|
-
# Compile source
|
|
386
|
-
yarn build
|
|
420
|
+
To release a new version
|
|
387
421
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
422
|
+
1. Create a [new GitHub Release](https://github.com/samchungy/zod-openapi/releases/new)
|
|
423
|
+
2. Select `🏷️ Choose a tag`, enter a version number. eg. `v1.2.0` and click `+ Create new tag: vX.X.X on publish`.
|
|
424
|
+
3. Click the `Generate release notes` button and adjust the description.
|
|
425
|
+
4. Tick the `Set as the latest release` box and click `Publish release`. This will trigger the `Release` workflow.
|
|
426
|
+
5. Check the `Pull Requests` tab for a PR labelled `Release vX.X.X`.
|
|
427
|
+
6. Click `Merge Pull Request` on that Pull Request to update master with the new package version.
|
|
428
|
+
|
|
429
|
+
To release a new beta version
|
|
430
|
+
|
|
431
|
+
1. Create a [new GitHub Release](https://github.com/samchungy/zod-openapi/releases/new)
|
|
432
|
+
2. Select `🏷️ Choose a tag`, enter a version number with a `-beta.X` suffix eg. `v1.2.0-beta.1` and click `+ Create new tag: vX.X.X-beta.X on publish`.
|
|
433
|
+
3. Click the `Generate release notes` button and adjust the description.
|
|
434
|
+
4. Tick the `Set as a pre-release` box and click `Publish release`. This will trigger the `Prerelease` workflow.
|
|
391
435
|
|
|
392
436
|
## Credits
|
|
393
437
|
|
|
@@ -3,8 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createComponents = exports.createComponentSchemaRef = exports.getDefaultComponents = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const metadata_1 = require("./schema/metadata");
|
|
6
|
-
const getDefaultComponents = (componentsObject) => {
|
|
7
|
-
const defaultComponents = {
|
|
6
|
+
const getDefaultComponents = (componentsObject, openapi = '3.1.0') => {
|
|
7
|
+
const defaultComponents = {
|
|
8
|
+
schemas: {},
|
|
9
|
+
parameters: {},
|
|
10
|
+
headers: {},
|
|
11
|
+
openapi,
|
|
12
|
+
};
|
|
8
13
|
if (!componentsObject) {
|
|
9
14
|
return defaultComponents;
|
|
10
15
|
}
|
|
@@ -19,18 +24,19 @@ const createSchemas = (schemas, components) => {
|
|
|
19
24
|
return;
|
|
20
25
|
}
|
|
21
26
|
return Object.entries(schemas).forEach(([key, schema]) => {
|
|
27
|
+
const ref = schema instanceof zod_1.ZodType ? schema._def.openapi?.ref ?? key : key;
|
|
22
28
|
const component = components.schemas[key];
|
|
23
29
|
if (component) {
|
|
24
|
-
throw new Error(`schemaRef "${
|
|
30
|
+
throw new Error(`schemaRef "${ref}" is already registered`);
|
|
25
31
|
}
|
|
26
32
|
if (schema instanceof zod_1.ZodType) {
|
|
27
|
-
components.schemas[
|
|
33
|
+
components.schemas[ref] = {
|
|
28
34
|
schemaObject: (0, metadata_1.createSchemaWithMetadata)(schema, components),
|
|
29
35
|
zodSchema: schema,
|
|
30
36
|
};
|
|
31
37
|
return;
|
|
32
38
|
}
|
|
33
|
-
components.schemas[
|
|
39
|
+
components.schemas[ref] = {
|
|
34
40
|
schemaObject: schema,
|
|
35
41
|
};
|
|
36
42
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"components.js","sourceRoot":"","sources":["../../src/create/components.ts"],"names":[],"mappings":";;;AACA,6BAA8B;AAG9B,gDAA6D;
|
|
1
|
+
{"version":3,"file":"components.js","sourceRoot":"","sources":["../../src/create/components.ts"],"names":[],"mappings":";;;AACA,6BAA8B;AAG9B,gDAA6D;AAgDtD,MAAM,oBAAoB,GAAG,CAClC,gBAGC,EACD,UAA6B,OAAO,EAClB,EAAE;IACpB,MAAM,iBAAiB,GAAG;QACxB,OAAO,EAAE,EAAE;QACX,UAAU,EAAE,EAAE;QACd,OAAO,EAAE,EAAE;QACX,OAAO;KACR,CAAC;IACF,IAAI,CAAC,gBAAgB,EAAE;QACrB,OAAO,iBAAiB,CAAC;KAC1B;IAED,aAAa,CAAC,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IAC3D,gBAAgB,CAAC,gBAAgB,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAC;IACjE,aAAa,CAAC,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IAE3D,OAAO,iBAAiB,CAAC;AAC3B,CAAC,CAAC;AAtBW,QAAA,oBAAoB,wBAsB/B;AAEF,MAAM,aAAa,GAAG,CACpB,OAA8C,EAC9C,UAA4B,EACtB,EAAE;IACR,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO;KACR;IACD,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE;QACvD,MAAM,GAAG,GACP,MAAM,YAAY,aAAO,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,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CAAC,SAAS,CAAC;AACjE,CAAC,CAAC"}
|
|
@@ -6,7 +6,7 @@ const components_1 = require("./components");
|
|
|
6
6
|
const paths_1 = require("./paths");
|
|
7
7
|
const createDocument = (zodOpenApiObject) => {
|
|
8
8
|
const { schemas, parameters, headers, ...rest } = zodOpenApiObject.components ?? {};
|
|
9
|
-
const components = (0, components_1.getDefaultComponents)({ headers, schemas, parameters });
|
|
9
|
+
const components = (0, components_1.getDefaultComponents)({ headers, schemas, parameters }, zodOpenApiObject.openapi);
|
|
10
10
|
return {
|
|
11
11
|
...zodOpenApiObject,
|
|
12
12
|
paths: (0, paths_1.createPaths)(zodOpenApiObject.paths, components),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document.js","sourceRoot":"","sources":["../../src/create/document.ts"],"names":[],"mappings":";;;AACA,+BAAiC;
|
|
1
|
+
{"version":3,"file":"document.js","sourceRoot":"","sources":["../../src/create/document.ts"],"names":[],"mappings":";;;AACA,+BAAiC;AAKjC,6CAAsE;AACtE,mCAAsC;AA4F/B,MAAM,cAAc,GAAG,CAC5B,gBAAkC,EACb,EAAE;IACvB,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAC7C,gBAAgB,CAAC,UAAU,IAAI,EAAE,CAAC;IACpC,MAAM,UAAU,GAAG,IAAA,iCAAoB,EACrC,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,EAChC,gBAAgB,CAAC,OAAO,CACzB,CAAC;IAEF,OAAO;QACL,GAAG,gBAAgB;QACnB,KAAK,EAAE,IAAA,mBAAW,EAAC,gBAAgB,CAAC,KAAK,EAAE,UAAU,CAAC;QACtD,QAAQ,EAAE,IAAA,mBAAW,EAAC,gBAAgB,CAAC,QAAQ,EAAE,UAAU,CAAC;QAC5D,UAAU,EAAE,IAAA,6BAAgB,EAAC,IAAI,EAAE,UAAU,CAAC;KAC/C,CAAC;AACJ,CAAC,CAAC;AAhBW,QAAA,cAAc,kBAgBzB;AAEK,MAAM,kBAAkB,GAAG,CAChC,MAAwB,EACxB,WAGC,EACO,EAAE;IACV,MAAM,QAAQ,GAAG,IAAA,sBAAc,EAAC,MAAM,CAAC,CAAC;IACxC,OAAO,IAAI,CAAC,SAAS,CACnB,QAAQ,EACR,WAAW,EAAE,QAAQ,EACrB,WAAW,EAAE,OAAO,IAAI,CAAC,CAC1B,CAAC;AACJ,CAAC,CAAC;AAbW,QAAA,kBAAkB,sBAa7B;AAEK,MAAM,kBAAkB,GAAG,CAChC,MAAwB,EACxB,cAGI,EAAE,EACE,EAAE;IACV,MAAM,QAAQ,GAAG,IAAA,sBAAc,EAAC,MAAM,CAAC,CAAC;IACxC,OAAO,IAAA,gBAAS,EAAC,QAAQ,EAAE,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;AACxE,CAAC,CAAC;AATW,QAAA,kBAAkB,sBAS7B"}
|
|
@@ -1,144 +1,127 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
exports.createSchemaOrRef =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const string_1 = require('./string');
|
|
27
|
-
const tuple_1 = require('./tuple');
|
|
28
|
-
const union_1 = require('./union');
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSchemaOrRef = exports.createRegisteredSchema = exports.createSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const components_1 = require("../components");
|
|
6
|
+
const array_1 = require("./array");
|
|
7
|
+
const boolean_1 = require("./boolean");
|
|
8
|
+
const date_1 = require("./date");
|
|
9
|
+
const default_1 = require("./default");
|
|
10
|
+
const discriminatedUnion_1 = require("./discriminatedUnion");
|
|
11
|
+
const effects_1 = require("./effects");
|
|
12
|
+
const enum_1 = require("./enum");
|
|
13
|
+
const intersection_1 = require("./intersection");
|
|
14
|
+
const literal_1 = require("./literal");
|
|
15
|
+
const metadata_1 = require("./metadata");
|
|
16
|
+
const nativeEnum_1 = require("./nativeEnum");
|
|
17
|
+
const null_1 = require("./null");
|
|
18
|
+
const nullable_1 = require("./nullable");
|
|
19
|
+
const number_1 = require("./number");
|
|
20
|
+
const object_1 = require("./object");
|
|
21
|
+
const optional_1 = require("./optional");
|
|
22
|
+
const record_1 = require("./record");
|
|
23
|
+
const string_1 = require("./string");
|
|
24
|
+
const tuple_1 = require("./tuple");
|
|
25
|
+
const union_1 = require("./union");
|
|
29
26
|
const createSchema = (zodSchema, components) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
zodSchema.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (!zodSchema._def.openapi?.type) {
|
|
97
|
-
console.log(zodSchema);
|
|
98
|
-
throw new Error(
|
|
99
|
-
`Unknown schema ${zodSchema.toString()}. Please assign it a manual type`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
return {};
|
|
27
|
+
if (zodSchema instanceof zod_1.ZodString) {
|
|
28
|
+
return (0, string_1.createStringSchema)(zodSchema);
|
|
29
|
+
}
|
|
30
|
+
if (zodSchema instanceof zod_1.ZodNumber) {
|
|
31
|
+
return (0, number_1.createNumberSchema)(zodSchema, components);
|
|
32
|
+
}
|
|
33
|
+
if (zodSchema instanceof zod_1.ZodBoolean) {
|
|
34
|
+
return (0, boolean_1.createBooleanSchema)(zodSchema);
|
|
35
|
+
}
|
|
36
|
+
if (zodSchema instanceof zod_1.ZodEnum) {
|
|
37
|
+
return (0, enum_1.createEnumSchema)(zodSchema);
|
|
38
|
+
}
|
|
39
|
+
if (zodSchema instanceof zod_1.ZodLiteral) {
|
|
40
|
+
return (0, literal_1.createLiteralSchema)(zodSchema);
|
|
41
|
+
}
|
|
42
|
+
if (zodSchema instanceof zod_1.ZodNativeEnum) {
|
|
43
|
+
return (0, nativeEnum_1.createNativeEnumSchema)(zodSchema, components);
|
|
44
|
+
}
|
|
45
|
+
if (zodSchema instanceof zod_1.ZodArray) {
|
|
46
|
+
return (0, array_1.createArraySchema)(zodSchema, components);
|
|
47
|
+
}
|
|
48
|
+
if (zodSchema instanceof zod_1.ZodObject) {
|
|
49
|
+
return (0, object_1.createObjectSchema)(zodSchema, components);
|
|
50
|
+
}
|
|
51
|
+
if (zodSchema instanceof zod_1.ZodUnion) {
|
|
52
|
+
return (0, union_1.createUnionSchema)(zodSchema, components);
|
|
53
|
+
}
|
|
54
|
+
if (zodSchema instanceof zod_1.ZodDiscriminatedUnion) {
|
|
55
|
+
return (0, discriminatedUnion_1.createDiscriminatedUnionSchema)(zodSchema, components);
|
|
56
|
+
}
|
|
57
|
+
if (zodSchema instanceof zod_1.ZodNull) {
|
|
58
|
+
return (0, null_1.createNullSchema)(zodSchema);
|
|
59
|
+
}
|
|
60
|
+
if (zodSchema instanceof zod_1.ZodNullable) {
|
|
61
|
+
return (0, nullable_1.createNullableSchema)(zodSchema, components);
|
|
62
|
+
}
|
|
63
|
+
if (zodSchema instanceof zod_1.ZodOptional) {
|
|
64
|
+
return (0, optional_1.createOptionalSchema)(zodSchema, components);
|
|
65
|
+
}
|
|
66
|
+
if (zodSchema instanceof zod_1.ZodDefault) {
|
|
67
|
+
return (0, default_1.createDefaultSchema)(zodSchema, components);
|
|
68
|
+
}
|
|
69
|
+
if (zodSchema instanceof zod_1.ZodRecord) {
|
|
70
|
+
return (0, record_1.createRecordSchema)(zodSchema, components);
|
|
71
|
+
}
|
|
72
|
+
if (zodSchema instanceof zod_1.ZodTuple) {
|
|
73
|
+
return (0, tuple_1.createTupleSchema)(zodSchema, components);
|
|
74
|
+
}
|
|
75
|
+
if (zodSchema instanceof zod_1.ZodDate) {
|
|
76
|
+
return (0, date_1.createDateSchema)(zodSchema);
|
|
77
|
+
}
|
|
78
|
+
if (zodSchema instanceof zod_1.ZodEffects &&
|
|
79
|
+
(zodSchema._def.effect.type === 'refinement' ||
|
|
80
|
+
zodSchema._def.effect.type === 'preprocess')) {
|
|
81
|
+
return (0, effects_1.createEffectsSchema)(zodSchema, components);
|
|
82
|
+
}
|
|
83
|
+
if (zodSchema instanceof zod_1.ZodNativeEnum) {
|
|
84
|
+
return (0, nativeEnum_1.createNativeEnumSchema)(zodSchema, components);
|
|
85
|
+
}
|
|
86
|
+
if (zodSchema instanceof zod_1.ZodIntersection) {
|
|
87
|
+
return (0, intersection_1.createIntersectionSchema)(zodSchema, components);
|
|
88
|
+
}
|
|
89
|
+
if (!zodSchema._def.openapi?.type) {
|
|
90
|
+
throw new Error(`Unknown schema ${zodSchema.toString()}. Please assign it a manual type`);
|
|
91
|
+
}
|
|
92
|
+
return {};
|
|
103
93
|
};
|
|
104
94
|
exports.createSchema = createSchema;
|
|
105
95
|
const createRegisteredSchema = (zodSchema, schemaRef, components) => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
96
|
+
const component = components.schemas[schemaRef];
|
|
97
|
+
if (component) {
|
|
98
|
+
if (component.zodSchema !== zodSchema) {
|
|
99
|
+
throw new Error(`schemaRef "${schemaRef}" is already registered`);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
$ref: (0, components_1.createComponentSchemaRef)(schemaRef),
|
|
103
|
+
};
|
|
110
104
|
}
|
|
105
|
+
// Optional Objects can return a reference object
|
|
106
|
+
const schemaOrRef = (0, metadata_1.createSchemaWithMetadata)(zodSchema, components);
|
|
107
|
+
if ('$ref' in schemaOrRef) {
|
|
108
|
+
throw new Error('Unexpected Error: received a reference object');
|
|
109
|
+
}
|
|
110
|
+
components.schemas[schemaRef] = {
|
|
111
|
+
schemaObject: schemaOrRef,
|
|
112
|
+
zodSchema,
|
|
113
|
+
};
|
|
111
114
|
return {
|
|
112
|
-
|
|
115
|
+
$ref: (0, components_1.createComponentSchemaRef)(schemaRef),
|
|
113
116
|
};
|
|
114
|
-
}
|
|
115
|
-
// Optional Objects can return a reference object
|
|
116
|
-
const schemaOrRef = (0, metadata_1.createSchemaWithMetadata)(
|
|
117
|
-
zodSchema,
|
|
118
|
-
components,
|
|
119
|
-
);
|
|
120
|
-
if ('$ref' in schemaOrRef) {
|
|
121
|
-
throw new Error('Unexpected Error: received a reference object');
|
|
122
|
-
}
|
|
123
|
-
components.schemas[schemaRef] = {
|
|
124
|
-
schemaObject: schemaOrRef,
|
|
125
|
-
zodSchema,
|
|
126
|
-
};
|
|
127
|
-
return {
|
|
128
|
-
$ref: (0, components_1.createComponentSchemaRef)(schemaRef),
|
|
129
|
-
};
|
|
130
117
|
};
|
|
131
118
|
exports.createRegisteredSchema = createRegisteredSchema;
|
|
132
119
|
const createSchemaOrRef = (zodSchema, components) => {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
components,
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
return (0, metadata_1.createSchemaWithMetadata)(zodSchema, components);
|
|
120
|
+
const schemaRef = zodSchema._def.openapi?.ref;
|
|
121
|
+
if (schemaRef) {
|
|
122
|
+
return (0, exports.createRegisteredSchema)(zodSchema, schemaRef, components);
|
|
123
|
+
}
|
|
124
|
+
return (0, metadata_1.createSchemaWithMetadata)(zodSchema, components);
|
|
142
125
|
};
|
|
143
126
|
exports.createSchemaOrRef = createSchemaOrRef;
|
|
144
|
-
//# sourceMappingURL=index.js.map
|
|
127
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/create/schema/index.ts"],"names":[],"mappings":";;;AACA,6BAsBa;AAEb,8CAA2E;AAE3E,mCAA4C;AAC5C,uCAAgD;AAChD,iCAA0C;AAC1C,uCAAgD;AAChD,6DAAsE;AACtE,uCAAgD;AAChD,iCAA0C;AAC1C,iDAA0D;AAC1D,uCAAgD;AAChD,yCAAsD;AACtD,6CAAsD;AACtD,iCAA0C;AAC1C,yCAAkD;AAClD,qCAA8C;AAC9C,qCAA8C;AAC9C,yCAAkD;AAClD,qCAA8C;AAC9C,qCAA8C;AAC9C,mCAA4C;AAC5C,mCAA4C;AAErC,MAAM,YAAY,GAAG,CAK1B,SAAsC,EACtC,UAA4B,EACgB,EAAE;IAC9C,IAAI,SAAS,YAAY,eAAS,EAAE;QAClC,OAAO,IAAA,2BAAkB,EAAC,SAAS,CAAC,CAAC;KACtC;IAED,IA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AAM,iBAAiB,GAAG,CAK/B,SAAsC,EACtC,UAA4B,EACgB,EAAE;IAC9C,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC;IAC9C,IAAI,SAAS,EAAE;QACb,OAAO,IAAA,8BAAsB,EAAC,SAAS,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;KACjE;IAED,OAAO,IAAA,mCAAwB,EAAC,SAAS,EAAE,UAAU,CAAC,CAAC;AACzD,CAAC,CAAC;AAdW,QAAA,iBAAiB,qBAc5B"}
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.sortStringsAndNumbers = exports.getValidEnumValues = exports.createNativeEnumSchema = void 0;
|
|
4
|
-
const
|
|
4
|
+
const openapi_1 = require("../../openapi");
|
|
5
|
+
const createNativeEnumSchema = (zodEnum, components) => {
|
|
5
6
|
const enumValues = (0, exports.getValidEnumValues)(zodEnum._def.values);
|
|
6
7
|
const { numbers, strings } = (0, exports.sortStringsAndNumbers)(enumValues);
|
|
7
8
|
if (strings.length && numbers.length) {
|
|
9
|
+
if ((0, openapi_1.satisfiesVersion)(components.openapi, '3.1.0'))
|
|
10
|
+
return {
|
|
11
|
+
type: ['string', 'number'],
|
|
12
|
+
enum: [...strings, ...numbers],
|
|
13
|
+
};
|
|
8
14
|
return {
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
oneOf: [
|
|
16
|
+
{ type: 'string', enum: strings },
|
|
17
|
+
{ type: 'number', enum: numbers },
|
|
18
|
+
],
|
|
11
19
|
};
|
|
12
20
|
}
|
|
13
21
|
if (strings.length) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nativeEnum.js","sourceRoot":"","sources":["../../../src/create/schema/nativeEnum.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"nativeEnum.js","sourceRoot":"","sources":["../../../src/create/schema/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AAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5C,CAAC,CAAC;AALW,QAAA,kBAAkB,sBAK7B;AAEK,MAAM,qBAAqB,GAAG,CACnC,MAA2B,EACR,EAAE,CAAC,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC;IAC7E,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC;CAC9E,CAAC,CAAC;AALU,QAAA,qBAAqB,yBAK/B"}
|
|
@@ -1,36 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createNullableSchema = void 0;
|
|
4
|
+
const openapi_1 = require("../../openapi");
|
|
4
5
|
const _1 = require(".");
|
|
5
6
|
const createNullableSchema = (zodNullable, components) => {
|
|
6
7
|
const schemaOrReference = (0, _1.createSchemaOrRef)(zodNullable.unwrap(), components);
|
|
7
|
-
if ('$ref' in schemaOrReference) {
|
|
8
|
+
if ('$ref' in schemaOrReference || schemaOrReference.allOf) {
|
|
8
9
|
return {
|
|
9
|
-
oneOf: mapNullOf([schemaOrReference]),
|
|
10
|
+
oneOf: mapNullOf([schemaOrReference], components.openapi),
|
|
10
11
|
};
|
|
11
12
|
}
|
|
12
13
|
if (schemaOrReference.oneOf) {
|
|
13
14
|
const { oneOf, ...schema } = schemaOrReference;
|
|
14
15
|
return {
|
|
15
|
-
oneOf: mapNullOf(oneOf),
|
|
16
|
+
oneOf: mapNullOf(oneOf, components.openapi),
|
|
16
17
|
...schema,
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
|
-
if (schemaOrReference.allOf) {
|
|
20
|
-
return {
|
|
21
|
-
oneOf: [schemaOrReference, { type: 'null' }],
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
20
|
if (schemaOrReference.anyOf) {
|
|
25
21
|
const { anyOf, ...schema } = schemaOrReference;
|
|
26
22
|
return {
|
|
27
|
-
anyOf: mapNullOf(anyOf),
|
|
23
|
+
anyOf: mapNullOf(anyOf, components.openapi),
|
|
28
24
|
...schema,
|
|
29
25
|
};
|
|
30
26
|
}
|
|
31
27
|
const { type, ...schema } = schemaOrReference;
|
|
28
|
+
if ((0, openapi_1.satisfiesVersion)(components.openapi, '3.1.0')) {
|
|
29
|
+
return {
|
|
30
|
+
type: mapNullType(type),
|
|
31
|
+
...schema,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
32
34
|
return {
|
|
33
|
-
type
|
|
35
|
+
type,
|
|
36
|
+
nullable: true,
|
|
34
37
|
...schema,
|
|
35
38
|
};
|
|
36
39
|
};
|
|
@@ -44,9 +47,10 @@ const mapNullType = (type) => {
|
|
|
44
47
|
}
|
|
45
48
|
return [type, 'null'];
|
|
46
49
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
];
|
|
50
|
+
const mapNullOf = (ofSchema, openapi) => {
|
|
51
|
+
if ((0, openapi_1.satisfiesVersion)(openapi, '3.1.0')) {
|
|
52
|
+
return [...ofSchema, { type: 'null' }];
|
|
53
|
+
}
|
|
54
|
+
return [...ofSchema, { nullable: true }];
|
|
55
|
+
};
|
|
52
56
|
//# sourceMappingURL=nullable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nullable.js","sourceRoot":"","sources":["../../../src/create/schema/nullable.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"nullable.js","sourceRoot":"","sources":["../../../src/create/schema/nullable.ts"],"names":[],"mappings":";;;AAGA,2CAAiD;AAIjD,wBAAsC;AAE/B,MAAM,oBAAoB,GAAG,CAClC,WAA6B,EAC7B,UAA4B,EACR,EAAE;IACtB,MAAM,iBAAiB,GAAG,IAAA,oBAAiB,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,CAAC;KAC1B;IAED,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,CAChB,QAAwD,EACxD,OAA0B,EACsB,EAAE;IAClD,IAAI,IAAA,0BAAgB,EAAC,OAAO,EAAE,OAAO,CAAC,EAAE;QACtC,OAAO,CAAC,GAAG,QAAQ,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;KACxC;IACD,OAAO,CAAC,GAAG,QAAQ,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AAC3C,CAAC,CAAC"}
|
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createNumberSchema = void 0;
|
|
4
|
-
const
|
|
4
|
+
const openapi_1 = require("../../openapi");
|
|
5
|
+
const createNumberSchema = (zodNumber, components) => {
|
|
5
6
|
const zodNumberChecks = getZodNumberChecks(zodNumber);
|
|
6
7
|
return {
|
|
7
8
|
type: mapNumberType(zodNumberChecks),
|
|
8
|
-
...mapRanges(zodNumberChecks),
|
|
9
|
+
...mapRanges(zodNumberChecks, components.openapi), // union types are too pesky to drill through
|
|
9
10
|
};
|
|
10
11
|
};
|
|
11
12
|
exports.createNumberSchema = createNumberSchema;
|
|
12
|
-
const mapRanges = (zodNumberChecks) => {
|
|
13
|
+
const mapRanges = (zodNumberChecks, openapi) => {
|
|
13
14
|
const minimum = zodNumberChecks.min?.value;
|
|
14
15
|
const maximum = zodNumberChecks.max?.value;
|
|
16
|
+
const exclusiveMinimum = zodNumberChecks.min?.inclusive;
|
|
17
|
+
const exclusiveMaximum = zodNumberChecks.max?.inclusive;
|
|
15
18
|
return {
|
|
16
19
|
...(minimum !== undefined && { minimum }),
|
|
17
20
|
...(maximum !== undefined && { maximum }),
|
|
18
|
-
...(
|
|
19
|
-
|
|
21
|
+
...(exclusiveMinimum && {
|
|
22
|
+
exclusiveMinimum: (0, openapi_1.satisfiesVersion)(openapi, '3.1.0') ? minimum : true,
|
|
23
|
+
}),
|
|
24
|
+
...(exclusiveMaximum && {
|
|
25
|
+
exclusiveMaximum: (0, openapi_1.satisfiesVersion)(openapi, '3.1.0') ? maximum : true,
|
|
26
|
+
}),
|
|
20
27
|
};
|
|
21
28
|
};
|
|
22
29
|
const getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, check) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.js","sourceRoot":"","sources":["../../../src/create/schema/number.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"number.js","sourceRoot":"","sources":["../../../src/create/schema/number.ts"],"names":[],"mappings":";;;AAGA,2CAAiD;AAI1C,MAAM,kBAAkB,GAAG,CAChC,SAAoB,EACpB,UAA4B,EACR,EAAE;IACtB,MAAM,eAAe,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAEtD,OAAO;QACL,IAAI,EAAE,aAAa,CAAC,eAAe,CAAC;QACpC,GAAI,SAAS,CAAC,eAAe,EAAE,UAAU,CAAC,OAAO,CAAwB,EAAE,6CAA6C;KACzH,CAAC;AACJ,CAAC,CAAC;AAVW,QAAA,kBAAkB,sBAU7B;AAEF,MAAM,SAAS,GAAG,CAChB,eAAkC,EAClC,OAA0B,EAI1B,EAAE;IACF,MAAM,OAAO,GAAG,eAAe,CAAC,GAAG,EAAE,KAAK,CAAC;IAC3C,MAAM,OAAO,GAAG,eAAe,CAAC,GAAG,EAAE,KAAK,CAAC;IAE3C,MAAM,gBAAgB,GAAG,eAAe,CAAC,GAAG,EAAE,SAAS,CAAC;IACxD,MAAM,gBAAgB,GAAG,eAAe,CAAC,GAAG,EAAE,SAAS,CAAC;IAExD,OAAO;QACL,GAAG,CAAC,OAAO,KAAK,SAAS,IAAI,EAAE,OAAO,EAAE,CAAC;QACzC,GAAG,CAAC,OAAO,KAAK,SAAS,IAAI,EAAE,OAAO,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