zod-openapi
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -5,3 +5,2 @@ "use strict"; | ||
const zod_1 = require("zod"); | ||
const components_1 = require("../components"); | ||
const _1 = require("."); | ||
@@ -11,3 +10,3 @@ const createDiscriminatedUnionSchema = (zodDiscriminatedUnion, state) => { | ||
const schemas = options.map((option) => (0, _1.createSchemaOrRef)(option, state)); | ||
const discriminator = (0, exports.mapDiscriminator)(options, zodDiscriminatedUnion.discriminator); | ||
const discriminator = (0, exports.mapDiscriminator)(schemas, options, zodDiscriminatedUnion.discriminator); | ||
return { | ||
@@ -19,14 +18,19 @@ oneOf: schemas, | ||
exports.createDiscriminatedUnionSchema = createDiscriminatedUnionSchema; | ||
const mapDiscriminator = (zodObjects, discriminator) => { | ||
if (zodObjects.some((obj) => !obj._def.openapi?.ref)) { | ||
const mapDiscriminator = (schemas, zodObjects, discriminator) => { | ||
if (typeof discriminator !== 'string') { | ||
return undefined; | ||
} | ||
const mapping = zodObjects.reduce((acc, zodObject) => { | ||
const schemaRef = zodObject._def.openapi?.ref; | ||
const mapping = {}; | ||
for (const [index, zodObject] of zodObjects.entries()) { | ||
const schema = schemas[index]; | ||
const componentSchemaRef = '$ref' in schema ? schema?.$ref : undefined; | ||
if (!componentSchemaRef) { | ||
return undefined; | ||
} | ||
const value = zodObject.shape[discriminator]; | ||
if (value instanceof zod_1.ZodEnum) { | ||
for (const enumValue of value._def.values) { | ||
acc[enumValue] = (0, components_1.createComponentSchemaRef)(schemaRef); | ||
mapping[enumValue] = componentSchemaRef; | ||
} | ||
return acc; | ||
continue; | ||
} | ||
@@ -37,5 +41,4 @@ const literalValue = (value?._def).value; | ||
} | ||
acc[literalValue] = (0, components_1.createComponentSchemaRef)(schemaRef); | ||
return acc; | ||
}, {}); | ||
mapping[literalValue] = componentSchemaRef; | ||
} | ||
return { | ||
@@ -42,0 +45,0 @@ propertyName: discriminator, |
import { ZodEnum, } from 'zod'; | ||
import { createComponentSchemaRef } from '../components'; | ||
import { createSchemaOrRef } from '.'; | ||
@@ -7,3 +6,3 @@ export const createDiscriminatedUnionSchema = (zodDiscriminatedUnion, state) => { | ||
const schemas = options.map((option) => createSchemaOrRef(option, state)); | ||
const discriminator = mapDiscriminator(options, zodDiscriminatedUnion.discriminator); | ||
const discriminator = mapDiscriminator(schemas, options, zodDiscriminatedUnion.discriminator); | ||
return { | ||
@@ -14,14 +13,19 @@ oneOf: schemas, | ||
}; | ||
export const mapDiscriminator = (zodObjects, discriminator) => { | ||
if (zodObjects.some((obj) => !obj._def.openapi?.ref)) { | ||
export const mapDiscriminator = (schemas, zodObjects, discriminator) => { | ||
if (typeof discriminator !== 'string') { | ||
return undefined; | ||
} | ||
const mapping = zodObjects.reduce((acc, zodObject) => { | ||
const schemaRef = zodObject._def.openapi?.ref; | ||
const mapping = {}; | ||
for (const [index, zodObject] of zodObjects.entries()) { | ||
const schema = schemas[index]; | ||
const componentSchemaRef = '$ref' in schema ? schema?.$ref : undefined; | ||
if (!componentSchemaRef) { | ||
return undefined; | ||
} | ||
const value = zodObject.shape[discriminator]; | ||
if (value instanceof ZodEnum) { | ||
for (const enumValue of value._def.values) { | ||
acc[enumValue] = createComponentSchemaRef(schemaRef); | ||
mapping[enumValue] = componentSchemaRef; | ||
} | ||
return acc; | ||
continue; | ||
} | ||
@@ -32,5 +36,4 @@ const literalValue = (value?._def).value; | ||
} | ||
acc[literalValue] = createComponentSchemaRef(schemaRef); | ||
return acc; | ||
}, {}); | ||
mapping[literalValue] = componentSchemaRef; | ||
} | ||
return { | ||
@@ -37,0 +40,0 @@ propertyName: discriminator, |
@@ -5,7 +5,2 @@ import { oas31 } from 'openapi3-ts'; | ||
export declare const createDiscriminatedUnionSchema: (zodDiscriminatedUnion: ZodDiscriminatedUnion<any, any>, state: SchemaState) => oas31.SchemaObject; | ||
export declare const mapDiscriminator: (zodObjects: AnyZodObject[], discriminator: string) => { | ||
propertyName: string; | ||
mapping: { | ||
[key: string]: string; | ||
}; | ||
} | undefined; | ||
export declare const mapDiscriminator: (schemas: (oas31.SchemaObject | oas31.ReferenceObject)[], zodObjects: AnyZodObject[], discriminator: unknown) => oas31.SchemaObject['discriminator']; |
{ | ||
"name": "zod-openapi", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A library to create full OpenAPI documents from your Zod types", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/samchungy/zod-openapi#readme", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
244109
2947