UNPKG

openapi-tsk

Version:
127 lines (126 loc) 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZodToOpenAPI = void 0; const types_1 = require("./types"); const zod_1 = require("zod"); class ZodToOpenAPI { static convertString(schema) { const stringSchema = { type: types_1.PropTypeEnum.STRING }; if (schema._def.checks) { for (const check of schema._def.checks) { if (check.kind === "min") { stringSchema.minimum = check.value; } else if (check.kind === "max") { stringSchema.maximum = check.value; } else if (check.kind === "email") { stringSchema.format = types_1.PropFormatEnum.EMAIL; } else if (check.kind === "url") { stringSchema.format = types_1.PropFormatEnum.URI; } else if (check.kind === "uuid") { stringSchema.format = types_1.PropFormatEnum.UUID; } } } return stringSchema; } static convertNumber(schema) { const numberSchema = { type: types_1.PropTypeEnum.NUMBER }; if (schema._def.checks) { for (const check of schema._def.checks) { if (check.kind === "min") { numberSchema.minimum = check.value; } else if (check.kind === "max") { numberSchema.maximum = check.value; } } } return numberSchema; } static convertArray(schema) { return { type: types_1.PropTypeEnum.ARRAY, items: this.convert(schema._def.type), }; } static convertEnum(schema) { return { type: types_1.PropTypeEnum.STRING, enum: schema._def.values, }; } static convertLiteral(schema) { return { type: typeof schema._def.value, enum: [schema._def.value], }; } static convert(schema) { if (!(schema === null || schema === void 0 ? void 0 : schema._def)) { throw new Error("Zod Schema is undefined or null"); } if (schema instanceof zod_1.ZodString || schema._def.typeName === "ZodString") { return this.convertString(schema); } if (schema instanceof zod_1.ZodNumber || schema._def.typeName === "ZodNumber") { return this.convertNumber(schema); } if (schema instanceof zod_1.ZodBoolean || schema._def.typeName === "ZodBoolean") { return { type: types_1.PropTypeEnum.BOOLEAN }; } if (schema instanceof zod_1.ZodArray || schema._def.typeName === "ZodArray") { return this.convertArray(schema); } if (schema instanceof zod_1.ZodEnum || schema._def.typeName === "ZodEnum") { return this.convertEnum(schema); } if (schema instanceof zod_1.ZodLiteral || schema._def.typeName === "ZodLiteral") { return this.convertLiteral(schema); } if (schema instanceof zod_1.ZodOptional || schema._def.typeName === "ZodOptional") { return this.convert(schema._def.innerType); } if (schema instanceof zod_1.ZodNullable || schema._def.typeName === "ZodNullable") { const nullableSchema = this.convert(schema._def.innerType); return { ...nullableSchema, nullable: true }; } if (schema instanceof zod_1.ZodDate || schema._def.typeName === "ZodDate") { return { type: types_1.PropTypeEnum.STRING, format: types_1.PropFormatEnum.DATE }; } if (schema instanceof zod_1.ZodDefault || schema._def.typeName === "ZodDefault") { return this.convert(schema._def.innerType); } throw new Error(`Unsupported Zod type: ${schema.constructor.name}`); } static transform(zodSchema) { if (!(zodSchema === null || zodSchema === void 0 ? void 0 : zodSchema.shape)) { throw new Error("ZodToOpenAPI only accepts ZodObject as input. Please provide a valid ZodObject."); } const properties = {}; const required = []; for (const [key, value] of Object.entries(zodSchema.shape)) { const propertySchema = this.convert(value); if (!(value instanceof zod_1.ZodOptional || value._def.typeName === "ZodOptional" || value instanceof zod_1.ZodNullable || value._def.typeName === "ZodNullable")) { propertySchema.required = true; required.push(key); } else { propertySchema.required = false; } properties[key] = propertySchema; } return { type: types_1.PropTypeEnum.OBJECT, properties, ...(required.length > 0 && { required }), }; } } exports.ZodToOpenAPI = ZodToOpenAPI;