zod-openapi 1.0.0 → 1.0.2
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/lib-commonjs/create/schema/discriminatedUnion.js +14 -11
- package/lib-commonjs/create/schema/discriminatedUnion.js.map +1 -1
- package/lib-es2015/create/schema/discriminatedUnion.js +14 -11
- package/lib-es2015/create/schema/discriminatedUnion.js.map +1 -1
- package/lib-types/create/schema/discriminatedUnion.d.ts +1 -6
- package/package.json +3 -3
|
@@ -2,38 +2,41 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.mapDiscriminator = exports.createDiscriminatedUnionSchema = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
|
-
const components_1 = require("../components");
|
|
6
5
|
const _1 = require(".");
|
|
7
6
|
const createDiscriminatedUnionSchema = (zodDiscriminatedUnion, state) => {
|
|
8
7
|
const options = zodDiscriminatedUnion.options;
|
|
9
8
|
const schemas = options.map((option) => (0, _1.createSchemaOrRef)(option, state));
|
|
10
|
-
const discriminator = (0, exports.mapDiscriminator)(options, zodDiscriminatedUnion.discriminator);
|
|
9
|
+
const discriminator = (0, exports.mapDiscriminator)(schemas, options, zodDiscriminatedUnion.discriminator);
|
|
11
10
|
return {
|
|
12
11
|
oneOf: schemas,
|
|
13
12
|
...(discriminator && { discriminator }),
|
|
14
13
|
};
|
|
15
14
|
};
|
|
16
15
|
exports.createDiscriminatedUnionSchema = createDiscriminatedUnionSchema;
|
|
17
|
-
const mapDiscriminator = (zodObjects, discriminator) => {
|
|
18
|
-
if (
|
|
16
|
+
const mapDiscriminator = (schemas, zodObjects, discriminator) => {
|
|
17
|
+
if (typeof discriminator !== 'string') {
|
|
19
18
|
return undefined;
|
|
20
19
|
}
|
|
21
|
-
const mapping =
|
|
22
|
-
|
|
20
|
+
const mapping = {};
|
|
21
|
+
for (const [index, zodObject] of zodObjects.entries()) {
|
|
22
|
+
const schema = schemas[index];
|
|
23
|
+
const componentSchemaRef = '$ref' in schema ? schema?.$ref : undefined;
|
|
24
|
+
if (!componentSchemaRef) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
23
27
|
const value = zodObject.shape[discriminator];
|
|
24
28
|
if (value instanceof zod_1.ZodEnum) {
|
|
25
29
|
for (const enumValue of value._def.values) {
|
|
26
|
-
|
|
30
|
+
mapping[enumValue] = componentSchemaRef;
|
|
27
31
|
}
|
|
28
|
-
|
|
32
|
+
continue;
|
|
29
33
|
}
|
|
30
34
|
const literalValue = (value?._def).value;
|
|
31
35
|
if (typeof literalValue !== 'string') {
|
|
32
36
|
throw new Error(`Discriminator ${discriminator} could not be found in one of the values of a discriminated union`);
|
|
33
37
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}, {});
|
|
38
|
+
mapping[literalValue] = componentSchemaRef;
|
|
39
|
+
}
|
|
37
40
|
return {
|
|
38
41
|
propertyName: discriminator,
|
|
39
42
|
mapping,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discriminatedUnion.js","sourceRoot":"","sources":["../../../src/create/schema/discriminatedUnion.ts"],"names":[],"mappings":";;;AACA,6BAMa;AAEb,
|
|
1
|
+
{"version":3,"file":"discriminatedUnion.js","sourceRoot":"","sources":["../../../src/create/schema/discriminatedUnion.ts"],"names":[],"mappings":";;;AACA,6BAMa;AAEb,wBAAmD;AAE5C,MAAM,8BAA8B,GAAG,CAC5C,qBAAsD,EACtD,KAAkB,EACE,EAAE;IACtB,MAAM,OAAO,GAAG,qBAAqB,CAAC,OAAyB,CAAC;IAChE,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAA,oBAAiB,EAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;IAC1E,MAAM,aAAa,GAAG,IAAA,wBAAgB,EACpC,OAAO,EACP,OAAO,EACP,qBAAqB,CAAC,aAAa,CACpC,CAAC;IACF,OAAO;Q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
|
@@ -1,35 +1,38 @@
|
|
|
1
1
|
import { ZodEnum, } from 'zod';
|
|
2
|
-
import { createComponentSchemaRef } from '../components';
|
|
3
2
|
import { createSchemaOrRef } from '.';
|
|
4
3
|
export const createDiscriminatedUnionSchema = (zodDiscriminatedUnion, state) => {
|
|
5
4
|
const options = zodDiscriminatedUnion.options;
|
|
6
5
|
const schemas = options.map((option) => createSchemaOrRef(option, state));
|
|
7
|
-
const discriminator = mapDiscriminator(options, zodDiscriminatedUnion.discriminator);
|
|
6
|
+
const discriminator = mapDiscriminator(schemas, options, zodDiscriminatedUnion.discriminator);
|
|
8
7
|
return {
|
|
9
8
|
oneOf: schemas,
|
|
10
9
|
...(discriminator && { discriminator }),
|
|
11
10
|
};
|
|
12
11
|
};
|
|
13
|
-
export const mapDiscriminator = (zodObjects, discriminator) => {
|
|
14
|
-
if (
|
|
12
|
+
export const mapDiscriminator = (schemas, zodObjects, discriminator) => {
|
|
13
|
+
if (typeof discriminator !== 'string') {
|
|
15
14
|
return undefined;
|
|
16
15
|
}
|
|
17
|
-
const mapping =
|
|
18
|
-
|
|
16
|
+
const mapping = {};
|
|
17
|
+
for (const [index, zodObject] of zodObjects.entries()) {
|
|
18
|
+
const schema = schemas[index];
|
|
19
|
+
const componentSchemaRef = '$ref' in schema ? schema?.$ref : undefined;
|
|
20
|
+
if (!componentSchemaRef) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
19
23
|
const value = zodObject.shape[discriminator];
|
|
20
24
|
if (value instanceof ZodEnum) {
|
|
21
25
|
for (const enumValue of value._def.values) {
|
|
22
|
-
|
|
26
|
+
mapping[enumValue] = componentSchemaRef;
|
|
23
27
|
}
|
|
24
|
-
|
|
28
|
+
continue;
|
|
25
29
|
}
|
|
26
30
|
const literalValue = (value?._def).value;
|
|
27
31
|
if (typeof literalValue !== 'string') {
|
|
28
32
|
throw new Error(`Discriminator ${discriminator} could not be found in one of the values of a discriminated union`);
|
|
29
33
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}, {});
|
|
34
|
+
mapping[literalValue] = componentSchemaRef;
|
|
35
|
+
}
|
|
33
36
|
return {
|
|
34
37
|
propertyName: discriminator,
|
|
35
38
|
mapping,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discriminatedUnion.js","sourceRoot":"","sources":["../../../src/create/schema/discriminatedUnion.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,OAAO,GAGR,MAAM,KAAK,CAAC;AAEb,OAAO,
|
|
1
|
+
{"version":3,"file":"discriminatedUnion.js","sourceRoot":"","sources":["../../../src/create/schema/discriminatedUnion.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,OAAO,GAGR,MAAM,KAAK,CAAC;AAEb,OAAO,EAAe,iBAAiB,EAAE,MAAM,GAAG,CAAC;AAEnD,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAC5C,qBAAsD,EACtD,KAAkB,EACE,EAAE;IACtB,MAAM,OAAO,GAAG,qBAAqB,CAAC,OAAyB,CAAC;IAChE,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,iBAAiB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;IAC1E,MAAM,aAAa,GAAG,gBAAgB,CACpC,OAAO,EACP,OAAO,EACP,qBAAqB,CAAC,aAAa,CACpC,CAAC;IACF,OAAO;QACL,KAAK,EAAE,OAAO;QACd,GAAG,CAAC,aAAa,IAAI,EAAE,aAAa,EAAE,CAAC;KACxC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,OAAuD,EACvD,UAA0B,EAC1B,aAAsB,EACe,EAAE;IACvC,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;QACrC,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,OAAO,GAAsD,EAAE,CAAC;IACtE,KAAK,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,UAAU,CAAC,OAAO,EAAE,EAAE;QACrD,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;QAC9B,MAAM,kBAAkB,GAAG,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;QACvE,IAAI,CAAC,kBAAkB,EAAE;YACvB,OAAO,SAAS,CAAC;SAClB;QAED,MAAM,KAAK,GAAI,SAAS,CAAC,KAAqB,CAAC,aAAa,CAAC,CAAC;QAE9D,IAAI,KAAK,YAAY,OAAO,EAAE;YAC5B,KAAK,MAAM,SAAS,IAAI,KAAK,CAAC,IAAI,CAAC,MAAkB,EAAE;gBACrD,OAAO,CAAC,SAAS,CAAC,GAAG,kBAAkB,CAAC;aACzC;YACD,SAAS;SACV;QAED,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,IAA+B,CAAA,CAAC,KAAK,CAAC;QAEnE,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE;YACpC,MAAM,IAAI,KAAK,CACb,iBAAiB,aAAa,mEAAmE,CAClG,CAAC;SACH;QAED,OAAO,CAAC,YAAY,CAAC,GAAG,kBAAkB,CAAC;KAC5C;IAED,OAAO;QACL,YAAY,EAAE,aAAa;QAC3B,OAAO;KACR,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -2,9 +2,4 @@ import { oas31 } from 'openapi3-ts';
|
|
|
2
2
|
import { AnyZodObject, ZodDiscriminatedUnion } from 'zod';
|
|
3
3
|
import { SchemaState } from '.';
|
|
4
4
|
export declare const createDiscriminatedUnionSchema: (zodDiscriminatedUnion: ZodDiscriminatedUnion<any, any>, state: SchemaState) => oas31.SchemaObject;
|
|
5
|
-
export declare const mapDiscriminator: (zodObjects: AnyZodObject[], discriminator:
|
|
6
|
-
propertyName: string;
|
|
7
|
-
mapping: {
|
|
8
|
-
[key: string]: string;
|
|
9
|
-
};
|
|
10
|
-
} | undefined;
|
|
5
|
+
export declare const mapDiscriminator: (schemas: (oas31.SchemaObject | oas31.ReferenceObject)[], zodObjects: AnyZodObject[], discriminator: unknown) => oas31.SchemaObject['discriminator'];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-openapi",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A library to create full OpenAPI documents from your Zod types",
|
|
5
5
|
"homepage": "https://github.com/samchungy/zod-openapi#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"test:watch": "skuba test --watch"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"openapi3-ts": "4.1.2",
|
|
36
|
-
"yaml": "2.2.2"
|
|
35
|
+
"openapi3-ts": "^4.1.2",
|
|
36
|
+
"yaml": "^2.2.2"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@redocly/cli": "1.0.0-beta.125",
|