zod-openapi
Advanced tools
Comparing version
@@ -185,13 +185,7 @@ "use strict"; | ||
if (!zodSchema._def.openapi?.type) { | ||
const zodType = zodSchema.constructor.name; | ||
if (isZodType(zodSchema, "ZodEffects")) { | ||
const schemaName = `${zodType} - ${zodSchema._def.effect.type}`; | ||
throw new Error( | ||
`Unknown schema ${schemaName} at ${state.path.join( | ||
" > " | ||
)}. Please assign it a manual 'type', wrap it in a ZodPipeline or change the 'effectType'.` | ||
); | ||
} | ||
const schemaName = zodSchema.constructor.name; | ||
throw new Error( | ||
`Unknown schema ${zodType}. Please assign it a manual 'type'.` | ||
`Unknown schema ${schemaName} at ${state.path.join( | ||
" > " | ||
)}. Please assign it a manual 'type'.` | ||
); | ||
@@ -361,3 +355,3 @@ } | ||
var isOptionalSchema = (zodSchema, state) => { | ||
if (isZodType(zodSchema, "ZodOptional")) { | ||
if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) { | ||
return true; | ||
@@ -502,2 +496,5 @@ } | ||
(acc, [key, zodSchema]) => { | ||
if (isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) { | ||
return acc; | ||
} | ||
acc[key] = createSchemaObject(zodSchema, state, [`property: ${key}`]); | ||
@@ -560,3 +557,2 @@ return acc; | ||
type: "object", | ||
// @ts-expect-error FIXME: https://github.com/metadevpro/openapi3-ts/pull/120 | ||
propertyNames: keySchema, | ||
@@ -689,3 +685,3 @@ additionalProperties | ||
if (zodTransform._def.openapi?.effectType === "output") { | ||
return createManualTypeSchema(zodTransform, state); | ||
return createManualOutputTransformSchema(zodTransform, state); | ||
} | ||
@@ -698,3 +694,3 @@ if (zodTransform._def.openapi?.effectType === "input") { | ||
if (state.type === "output") { | ||
return createManualTypeSchema(zodTransform, state); | ||
return createManualOutputTransformSchema(zodTransform, state); | ||
} | ||
@@ -706,2 +702,16 @@ state.effectType = "input"; | ||
}; | ||
var createManualOutputTransformSchema = (zodTransform, state) => { | ||
if (!zodTransform._def.openapi?.type) { | ||
const zodType = zodTransform.constructor.name; | ||
const schemaName = `${zodType} - ${zodTransform._def.effect.type}`; | ||
throw new Error( | ||
`Failed to determine a type for ${schemaName} at ${state.path.join( | ||
" > " | ||
)}. Please change the 'effectType' to 'input', wrap it in a ZodPipeline or assign it a manual 'type'.` | ||
); | ||
} | ||
return { | ||
type: zodTransform._def.openapi.type | ||
}; | ||
}; | ||
var throwTransformError = (zodType, state) => { | ||
@@ -998,3 +1008,5 @@ throw new Error( | ||
const required = !isOptionalSchema(schema, state); | ||
const description = schema._def.openapi?.description ?? schema._def.description; | ||
return { | ||
...description && { description }, | ||
...rest, | ||
@@ -1001,0 +1013,0 @@ ...schema && { schema: schemaObject }, |
@@ -161,13 +161,7 @@ var __defProp = Object.defineProperty; | ||
if (!zodSchema._def.openapi?.type) { | ||
const zodType = zodSchema.constructor.name; | ||
if (isZodType(zodSchema, "ZodEffects")) { | ||
const schemaName = `${zodType} - ${zodSchema._def.effect.type}`; | ||
throw new Error( | ||
`Unknown schema ${schemaName} at ${state.path.join( | ||
" > " | ||
)}. Please assign it a manual 'type', wrap it in a ZodPipeline or change the 'effectType'.` | ||
); | ||
} | ||
const schemaName = zodSchema.constructor.name; | ||
throw new Error( | ||
`Unknown schema ${zodType}. Please assign it a manual 'type'.` | ||
`Unknown schema ${schemaName} at ${state.path.join( | ||
" > " | ||
)}. Please assign it a manual 'type'.` | ||
); | ||
@@ -337,3 +331,3 @@ } | ||
var isOptionalSchema = (zodSchema, state) => { | ||
if (isZodType(zodSchema, "ZodOptional")) { | ||
if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) { | ||
return true; | ||
@@ -478,2 +472,5 @@ } | ||
(acc, [key, zodSchema]) => { | ||
if (isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) { | ||
return acc; | ||
} | ||
acc[key] = createSchemaObject(zodSchema, state, [`property: ${key}`]); | ||
@@ -536,3 +533,2 @@ return acc; | ||
type: "object", | ||
// @ts-expect-error FIXME: https://github.com/metadevpro/openapi3-ts/pull/120 | ||
propertyNames: keySchema, | ||
@@ -665,3 +661,3 @@ additionalProperties | ||
if (zodTransform._def.openapi?.effectType === "output") { | ||
return createManualTypeSchema(zodTransform, state); | ||
return createManualOutputTransformSchema(zodTransform, state); | ||
} | ||
@@ -674,3 +670,3 @@ if (zodTransform._def.openapi?.effectType === "input") { | ||
if (state.type === "output") { | ||
return createManualTypeSchema(zodTransform, state); | ||
return createManualOutputTransformSchema(zodTransform, state); | ||
} | ||
@@ -682,2 +678,16 @@ state.effectType = "input"; | ||
}; | ||
var createManualOutputTransformSchema = (zodTransform, state) => { | ||
if (!zodTransform._def.openapi?.type) { | ||
const zodType = zodTransform.constructor.name; | ||
const schemaName = `${zodType} - ${zodTransform._def.effect.type}`; | ||
throw new Error( | ||
`Failed to determine a type for ${schemaName} at ${state.path.join( | ||
" > " | ||
)}. Please change the 'effectType' to 'input', wrap it in a ZodPipeline or assign it a manual 'type'.` | ||
); | ||
} | ||
return { | ||
type: zodTransform._def.openapi.type | ||
}; | ||
}; | ||
var throwTransformError = (zodType, state) => { | ||
@@ -974,3 +984,5 @@ throw new Error( | ||
const required = !isOptionalSchema(schema, state); | ||
const description = schema._def.openapi?.description ?? schema._def.description; | ||
return { | ||
...description && { description }, | ||
...rest, | ||
@@ -977,0 +989,0 @@ ...schema && { schema: schemaObject }, |
@@ -5,2 +5,3 @@ import type { ZodEffects, ZodType, ZodTypeAny, input, output } from 'zod'; | ||
export declare const createTransformSchema: <T extends ZodTypeAny, Output = output<T>, Input = input<T>>(zodTransform: ZodEffects<T, Output, Input>, state: SchemaState) => oas31.SchemaObject | oas31.ReferenceObject; | ||
export declare const createManualOutputTransformSchema: <T extends ZodTypeAny, Output = output<T>, Input = input<T>>(zodTransform: ZodEffects<T, Output, Input>, state: SchemaState) => oas31.SchemaObject; | ||
export declare const throwTransformError: (zodType: ZodType, state: SchemaState) => never; |
@@ -31,2 +31,3 @@ import { ServerObject } from './oas-common'; | ||
name: string; | ||
identifier?: string; | ||
url?: string; | ||
@@ -229,2 +230,3 @@ } | ||
additionalProperties?: SchemaObject | ReferenceObject | boolean; | ||
propertyNames?: SchemaObject | ReferenceObject; | ||
description?: string; | ||
@@ -249,2 +251,4 @@ default?: any; | ||
prefixItems?: (SchemaObject | ReferenceObject)[]; | ||
contentMediaType?: string; | ||
contentEncoding?: string; | ||
} | ||
@@ -251,0 +255,0 @@ export declare function isSchemaObject(schema: SchemaObject | ReferenceObject): schema is SchemaObject; |
{ | ||
"name": "zod-openapi", | ||
"version": "2.11.0", | ||
"version": "2.12.0", | ||
"description": "Convert Zod Schemas to OpenAPI v3.x documentation", | ||
@@ -46,8 +46,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"@redocly/cli": "1.4.0", | ||
"@redocly/cli": "1.6.0", | ||
"@types/node": "^20.3.0", | ||
"eslint-plugin-zod-openapi": "^0.1.0", | ||
"openapi3-ts": "4.1.2", | ||
"skuba": "7.2.0", | ||
"yaml": "2.3.3", | ||
"openapi3-ts": "4.2.1", | ||
"skuba": "7.3.1", | ||
"yaml": "2.3.4", | ||
"zod": "3.22.4" | ||
@@ -54,0 +54,0 @@ }, |
@@ -185,3 +185,3 @@ <p align="center"> | ||
paths: { | ||
'/jobs/:a': { | ||
'/jobs/{a}': { | ||
put: { | ||
@@ -205,3 +205,3 @@ requestParams: { | ||
paths: { | ||
'/jobs/:a': { | ||
'/jobs/{a}': { | ||
put: { | ||
@@ -526,2 +526,3 @@ parameters: [ | ||
- supporting `string`, `number` and combined enums. | ||
- ZodNever | ||
- ZodNull | ||
@@ -549,2 +550,3 @@ - ZodNullable | ||
- `prefixItems` mapping for OpenAPI 3.1.0+ | ||
- ZodUndefined | ||
- ZodUnion | ||
@@ -551,0 +553,0 @@ - By default it outputs an `allOf` schema. Use `unionOneOf` to change this to output `oneOf` instead. |
181330
0.74%4481
0.65%621
0.32%