UNPKG

zod-to-x

Version:

Multi language types generation from Zod schemas.

38 lines (37 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isTranspilerableZodType = isTranspilerableZodType; exports.isTranspiledExtendable = isTranspiledExtendable; exports.cloneTranspiledExtendable = cloneTranspiledExtendable; const zod_1 = require("zod"); function isTranspilerableZodType(zodType) { return (zodType === zod_1.ZodFirstPartyTypeKind.ZodEnum || zodType === zod_1.ZodFirstPartyTypeKind.ZodNativeEnum || zodType === zod_1.ZodFirstPartyTypeKind.ZodObject || zodType === zod_1.ZodFirstPartyTypeKind.ZodUnion || zodType === zod_1.ZodFirstPartyTypeKind.ZodDiscriminatedUnion || zodType === zod_1.ZodFirstPartyTypeKind.ZodIntersection); } function isTranspiledExtendable(zodType) { return (zodType === zod_1.ZodFirstPartyTypeKind.ZodObject || zodType === zod_1.ZodFirstPartyTypeKind.ZodUnion || zodType === zod_1.ZodFirstPartyTypeKind.ZodDiscriminatedUnion || zodType === zod_1.ZodFirstPartyTypeKind.ZodIntersection); } function cloneTranspiledExtendable(zodItem) { if (zodItem instanceof zod_1.ZodObject) { return new zod_1.ZodObject(Object.assign({}, zodItem._def)); } else if (zodItem instanceof zod_1.ZodUnion) { return new zod_1.ZodUnion(Object.assign({}, zodItem._def)); } else if (zodItem instanceof zod_1.ZodDiscriminatedUnion) { return new zod_1.ZodDiscriminatedUnion(Object.assign({}, zodItem._def)); } else if (zodItem instanceof zod_1.ZodIntersection) { return new zod_1.ZodIntersection(Object.assign({}, zodItem._def)); } else { throw new Error(`This type cannot be extended when transpiled: ${zodItem}`); } }