@anatine/zod-openapi
Advanced tools
Comparing version 2.2.5 to 2.2.6
@@ -5,2 +5,4 @@ # Changelog | ||
### [2.2.6](https://github.com/anatine/zod-plugins/compare/zod-openapi-2.2.5...zod-openapi-2.2.6) (2024-06-21) | ||
### [2.2.5](https://github.com/anatine/zod-plugins/compare/zod-openapi-2.2.4...zod-openapi-2.2.5) (2024-03-20) | ||
@@ -7,0 +9,0 @@ |
{ | ||
"name": "@anatine/zod-openapi", | ||
"version": "2.2.5", | ||
"version": "2.2.6", | ||
"description": "Zod to OpenAPI converter", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -9,4 +9,5 @@ import type { SchemaObject } from 'openapi3-ts/oas31'; | ||
} | ||
type OpenAPIVersion = '3.0' | '3.1'; | ||
export declare function extendApi<T extends OpenApiZodAny>(schema: T, schemaObject?: AnatineSchemaObject): T; | ||
export declare function generateSchema(zodRef: OpenApiZodAny, useOutput?: boolean): SchemaObject; | ||
export declare function generateSchema(zodRef: OpenApiZodAny, useOutput?: boolean, openApiVersion?: OpenAPIVersion): SchemaObject; | ||
export {}; |
@@ -13,10 +13,13 @@ "use strict"; | ||
exports.extendApi = extendApi; | ||
function iterateZodObject({ zodRef, useOutput, hideDefinitions, }) { | ||
function iterateZodObject({ zodRef, useOutput, hideDefinitions, openApiVersion, }) { | ||
const reduced = Object.keys(zodRef.shape) | ||
.filter((key) => (hideDefinitions === null || hideDefinitions === void 0 ? void 0 : hideDefinitions.includes(key)) === false) | ||
.reduce((carry, key) => (Object.assign(Object.assign({}, carry), { [key]: generateSchema(zodRef.shape[key], useOutput) })), {}); | ||
.reduce((carry, key) => (Object.assign(Object.assign({}, carry), { [key]: generateSchema(zodRef.shape[key], useOutput, openApiVersion) })), {}); | ||
return reduced; | ||
} | ||
function parseTransformation({ zodRef, schemas, useOutput, }) { | ||
const input = generateSchema(zodRef._def.schema, useOutput); | ||
function typeFormat(type, openApiVersion) { | ||
return openApiVersion === '3.0' ? type : [type]; | ||
} | ||
function parseTransformation({ zodRef, schemas, useOutput, openApiVersion, }) { | ||
const input = generateSchema(zodRef._def.schema, useOutput, openApiVersion); | ||
let output = 'undefined'; | ||
@@ -27,5 +30,3 @@ if (useOutput && zodRef._def.effect) { | ||
try { | ||
// todo: this doesn't deal with nullable types very well | ||
// @ts-expect-error because we try/catch for a missing type | ||
const type = input.type[0]; | ||
const type = Array.isArray(input.type) ? input.type[0] : input.type; | ||
output = typeof effect.transform(['integer', 'number'].includes(`${type}`) | ||
@@ -51,11 +52,12 @@ ? 0 | ||
} | ||
const outputType = output; | ||
return (0, ts_deepmerge_1.default)(Object.assign(Object.assign(Object.assign({}, (zodRef.description ? { description: zodRef.description } : {})), input), (['number', 'string', 'boolean', 'null'].includes(output) | ||
? { | ||
type: [output], | ||
type: typeFormat(outputType, openApiVersion), | ||
} | ||
: {})), ...schemas); | ||
} | ||
function parseString({ zodRef, schemas, }) { | ||
function parseString({ zodRef, schemas, openApiVersion, }) { | ||
const baseSchema = { | ||
type: ['string'], | ||
type: typeFormat('string', openApiVersion), | ||
}; | ||
@@ -97,5 +99,5 @@ const { checks = [] } = zodRef._def; | ||
} | ||
function parseNumber({ zodRef, schemas, }) { | ||
function parseNumber({ zodRef, schemas, openApiVersion, }) { | ||
const baseSchema = { | ||
type: ['number'], | ||
type: typeFormat('number', openApiVersion), | ||
}; | ||
@@ -118,3 +120,3 @@ const { checks = [] } = zodRef._def; | ||
case 'int': | ||
baseSchema.type = ['integer']; | ||
baseSchema.type = typeFormat('integer', openApiVersion); | ||
break; | ||
@@ -137,3 +139,3 @@ case 'multipleOf': | ||
} | ||
function parseObject({ zodRef, schemas, useOutput, hideDefinitions, }) { | ||
function parseObject({ zodRef, schemas, useOutput, hideDefinitions, openApiVersion, }) { | ||
var _a; | ||
@@ -144,3 +146,3 @@ let additionalProperties; | ||
((_a = zodRef._def.catchall) === null || _a === void 0 ? void 0 : _a._def.typeName) === 'ZodNever')) | ||
additionalProperties = generateSchema(zodRef._def.catchall, useOutput); | ||
additionalProperties = generateSchema(zodRef._def.catchall, useOutput, openApiVersion); | ||
else if (zodRef._def.unknownKeys === 'passthrough') | ||
@@ -160,3 +162,3 @@ additionalProperties = true; | ||
const required = requiredProperties.length > 0 ? { required: requiredProperties } : {}; | ||
return (0, ts_deepmerge_1.default)(Object.assign(Object.assign(Object.assign({ type: ['object'], properties: iterateZodObject({ | ||
return (0, ts_deepmerge_1.default)(Object.assign(Object.assign(Object.assign({ type: typeFormat('object', openApiVersion), properties: iterateZodObject({ | ||
zodRef: zodRef, | ||
@@ -166,23 +168,30 @@ schemas, | ||
hideDefinitions: getExcludedDefinitionsFromSchema(schemas), | ||
openApiVersion, | ||
}) }, required), additionalProperties), hideDefinitions), zodRef.description ? { description: zodRef.description, hideDefinitions } : {}, ...schemas); | ||
} | ||
function parseRecord({ zodRef, schemas, useOutput, }) { | ||
function parseRecord({ zodRef, schemas, useOutput, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)({ | ||
type: ['object'], | ||
type: typeFormat('object', openApiVersion), | ||
additionalProperties: zodRef._def.valueType instanceof zod_1.z.ZodUnknown | ||
? {} | ||
: generateSchema(zodRef._def.valueType, useOutput), | ||
: generateSchema(zodRef._def.valueType, useOutput, openApiVersion), | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseBigInt({ zodRef, schemas, }) { | ||
return (0, ts_deepmerge_1.default)({ type: ['integer'], format: 'int64' }, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
function parseBigInt({ zodRef, schemas, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)({ | ||
type: typeFormat('integer', openApiVersion), | ||
format: 'int64' | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseBoolean({ zodRef, schemas, }) { | ||
return (0, ts_deepmerge_1.default)({ type: ['boolean'] }, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
function parseBoolean({ zodRef, schemas, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)({ type: typeFormat('boolean', openApiVersion) }, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseDate({ zodRef, schemas }) { | ||
return (0, ts_deepmerge_1.default)({ type: ['string'], format: 'date-time' }, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
function parseDate({ zodRef, schemas, openApiVersion }) { | ||
return (0, ts_deepmerge_1.default)({ | ||
type: typeFormat('string', openApiVersion), | ||
format: 'date-time' | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseNull({ zodRef, schemas }) { | ||
return (0, ts_deepmerge_1.default)({ | ||
function parseNull({ zodRef, schemas, openApiVersion }) { | ||
return (0, ts_deepmerge_1.default)(openApiVersion === '3.0' ? { type: 'null' } : { | ||
type: ['string', 'null'], | ||
@@ -192,13 +201,13 @@ enum: ['null'], | ||
} | ||
function parseOptional({ schemas, zodRef, useOutput, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(zodRef.unwrap(), useOutput), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
function parseOptional({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(zodRef.unwrap(), useOutput, openApiVersion), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseNullable({ schemas, zodRef, useOutput, }) { | ||
const schema = generateSchema(zodRef.unwrap(), useOutput); | ||
return (0, ts_deepmerge_1.default)(schema, { type: ['null'] }, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
function parseNullable({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
const schema = generateSchema(zodRef.unwrap(), useOutput, openApiVersion); | ||
return (0, ts_deepmerge_1.default)(schema, { type: typeFormat('null', openApiVersion) }, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseDefault({ schemas, zodRef, useOutput, }) { | ||
return (0, ts_deepmerge_1.default)(Object.assign({ default: zodRef._def.defaultValue() }, generateSchema(zodRef._def.innerType, useOutput)), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
function parseDefault({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)(Object.assign({ default: zodRef._def.defaultValue() }, generateSchema(zodRef._def.innerType, useOutput, openApiVersion)), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseArray({ schemas, zodRef, useOutput, }) { | ||
function parseArray({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
const constraints = {}; | ||
@@ -213,25 +222,27 @@ if (zodRef._def.exactLength != null) { | ||
constraints.maxItems = zodRef._def.maxLength.value; | ||
return (0, ts_deepmerge_1.default)(Object.assign({ type: ['array'], items: generateSchema(zodRef.element, useOutput) }, constraints), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
return (0, ts_deepmerge_1.default)(Object.assign({ type: typeFormat('array', openApiVersion), items: generateSchema(zodRef.element, useOutput, openApiVersion) }, constraints), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseLiteral({ schemas, zodRef, }) { | ||
function parseLiteral({ schemas, zodRef, openApiVersion, }) { | ||
const type = typeof zodRef._def.value; | ||
return (0, ts_deepmerge_1.default)({ | ||
type: [typeof zodRef._def.value], | ||
type: typeFormat(type, openApiVersion), | ||
enum: [zodRef._def.value], | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseEnum({ schemas, zodRef, }) { | ||
function parseEnum({ schemas, zodRef, openApiVersion, }) { | ||
const type = typeof Object.values(zodRef._def.values)[0]; | ||
return (0, ts_deepmerge_1.default)({ | ||
type: [typeof Object.values(zodRef._def.values)[0]], | ||
type: typeFormat(type, openApiVersion), | ||
enum: Object.values(zodRef._def.values), | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseIntersection({ schemas, zodRef, useOutput, }) { | ||
function parseIntersection({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)({ | ||
allOf: [ | ||
generateSchema(zodRef._def.left, useOutput), | ||
generateSchema(zodRef._def.right, useOutput), | ||
generateSchema(zodRef._def.left, useOutput, openApiVersion), | ||
generateSchema(zodRef._def.right, useOutput, openApiVersion), | ||
], | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseUnion({ schemas, zodRef, useOutput, }) { | ||
function parseUnion({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
const contents = zodRef._def.options; | ||
@@ -246,3 +257,3 @@ if (contents.reduce((prev, content) => prev && content._def.typeName === 'ZodLiteral', true)) { | ||
return (0, ts_deepmerge_1.default)({ | ||
type: [type], | ||
type: typeFormat(type, openApiVersion), | ||
enum: literals.map((literal) => literal._def.value), | ||
@@ -253,6 +264,6 @@ }, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
return (0, ts_deepmerge_1.default)({ | ||
oneOf: contents.map((schema) => generateSchema(schema, useOutput)), | ||
oneOf: contents.map((schema) => generateSchema(schema, useOutput, openApiVersion)), | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
function parseDiscriminatedUnion({ schemas, zodRef, useOutput, }) { | ||
function parseDiscriminatedUnion({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)({ | ||
@@ -262,3 +273,3 @@ discriminator: { | ||
}, | ||
oneOf: Array.from(zodRef._def.options.values()).map((schema) => generateSchema(schema, useOutput)), | ||
oneOf: Array.from(zodRef._def.options.values()).map((schema) => generateSchema(schema, useOutput, openApiVersion)), | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
@@ -269,4 +280,4 @@ } | ||
} | ||
function parseBranded({ schemas, zodRef, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(zodRef._def.type), ...schemas); | ||
function parseBranded({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(zodRef._def.type, useOutput, openApiVersion), ...schemas); | ||
} | ||
@@ -276,7 +287,7 @@ function catchAllParser({ zodRef, schemas, }) { | ||
} | ||
function parsePipeline({ schemas, zodRef, useOutput, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(useOutput ? zodRef._def.out : zodRef._def.in, useOutput), ...schemas); | ||
function parsePipeline({ schemas, zodRef, useOutput, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(useOutput ? zodRef._def.out : zodRef._def.in, useOutput, openApiVersion), ...schemas); | ||
} | ||
function parseReadonly({ zodRef, useOutput, schemas, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(zodRef._def.innerType, useOutput), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
function parseReadonly({ zodRef, useOutput, schemas, openApiVersion, }) { | ||
return (0, ts_deepmerge_1.default)(generateSchema(zodRef._def.innerType, useOutput, openApiVersion), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
} | ||
@@ -320,3 +331,3 @@ const workerMap = { | ||
}; | ||
function generateSchema(zodRef, useOutput) { | ||
function generateSchema(zodRef, useOutput = false, openApiVersion = '3.1') { | ||
const { metaOpenApi = {} } = zodRef; | ||
@@ -333,9 +344,10 @@ const schemas = [ | ||
useOutput, | ||
openApiVersion, | ||
}); | ||
} | ||
return catchAllParser({ zodRef, schemas }); | ||
return catchAllParser({ zodRef, schemas, openApiVersion }); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
return catchAllParser({ zodRef, schemas }); | ||
return catchAllParser({ zodRef, schemas, openApiVersion }); | ||
} | ||
@@ -342,0 +354,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47667
397