zod-to-x
Version:
Multi language types generation from Zod schemas.
242 lines (241 loc) • 11.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZodHelpers = exports.ZodFirstPartyTypeKind = void 0;
exports.createGenericType = createGenericType;
exports.useGenericType = useGenericType;
const v4_1 = require("zod/v4");
const zod_ext_1 = require("./zod_ext");
var ZodFirstPartyTypeKind;
(function (ZodFirstPartyTypeKind) {
ZodFirstPartyTypeKind["ZodAny"] = "any";
ZodFirstPartyTypeKind["ZodString"] = "string";
ZodFirstPartyTypeKind["ZodNumber"] = "number";
ZodFirstPartyTypeKind["ZodBigInt"] = "bigint";
ZodFirstPartyTypeKind["ZodLiteral"] = "literal";
ZodFirstPartyTypeKind["ZodBoolean"] = "boolean";
ZodFirstPartyTypeKind["ZodDate"] = "date";
ZodFirstPartyTypeKind["ZodEnum"] = "enum";
ZodFirstPartyTypeKind["ZodUnion"] = "union";
ZodFirstPartyTypeKind["ZodIntersection"] = "intersection";
ZodFirstPartyTypeKind["ZodObject"] = "object";
ZodFirstPartyTypeKind["ZodLazy"] = "lazy";
ZodFirstPartyTypeKind["ZodRecord"] = "record";
ZodFirstPartyTypeKind["ZodMap"] = "map";
ZodFirstPartyTypeKind["ZodSet"] = "set";
ZodFirstPartyTypeKind["ZodArray"] = "array";
ZodFirstPartyTypeKind["ZodTuple"] = "tuple";
ZodFirstPartyTypeKind["ZodOptional"] = "optional";
ZodFirstPartyTypeKind["ZodNullable"] = "nullable";
ZodFirstPartyTypeKind["ZodDefault"] = "default";
ZodFirstPartyTypeKind["ZodPromise"] = "promise";
})(ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = ZodFirstPartyTypeKind = {}));
/**
* Zod's type is checked insted of instanceof to resolve Bun incomatibilities.
*/
class ZodHelpers {
static isZodType(i) {
var _a;
return Object.values(ZodFirstPartyTypeKind).includes((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type);
}
static isZodAny(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodAny;
}
static isZodString(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodString;
}
static isZodNumber(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodNumber;
}
static isZodBigInt(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodBigInt;
}
static isZodLiteral(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodLiteral;
}
static isZodBoolean(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodBoolean;
}
static isZodDate(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodDate;
}
static isZodEnum(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodEnum;
}
static isZodUnion(i) {
var _a, _b, _c;
return (((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodUnion &&
((_c = (_b = i._zod) === null || _b === void 0 ? void 0 : _b.def) === null || _c === void 0 ? void 0 : _c.discriminator) === undefined);
}
static isZodDiscriminatedUnion(i) {
var _a, _b, _c;
return (((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodUnion &&
((_c = (_b = i._zod) === null || _b === void 0 ? void 0 : _b.def) === null || _c === void 0 ? void 0 : _c.discriminator) !== undefined);
}
static isZodIntersection(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodIntersection;
}
static isZodObject(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodObject;
}
static isZodLazy(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodLazy;
}
static isZodRecord(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodRecord;
}
static isZodMap(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodMap;
}
static isZodArray(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodArray;
}
static isZodSet(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodSet;
}
static isZodTuple(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodTuple;
}
static isZodOptional(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodOptional;
}
static isZodNullable(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodNullable;
}
static isZodDefault(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodDefault;
}
static isZodPromise(i) {
var _a;
return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodPromise;
}
static isZodAnyUnionType(i) {
return this.isZodUnion(i) || this.isZodDiscriminatedUnion(i);
}
static isZodAnyNumberType(i) {
return this.isZodNumber(i) || this.isZodBigInt(i);
}
static isZodAnyMapType(i) {
return this.isZodMap(i) || this.isZodRecord(i);
}
/**
* Complex types that shall always be transpiled, which output would be a type, or alias if
* redefined using layered modeling.
* @param zodType
* @returns
*/
static isTranspilerableZodType(zodType) {
var _a;
const type = typeof zodType === "string" ? zodType : (_a = zodType === null || zodType === void 0 ? void 0 : zodType.def) === null || _a === void 0 ? void 0 : _a.type;
return (type === ZodFirstPartyTypeKind.ZodEnum ||
type === ZodFirstPartyTypeKind.ZodObject ||
type === ZodFirstPartyTypeKind.ZodUnion ||
type === ZodFirstPartyTypeKind.ZodIntersection);
}
/**
* Primitive types that can only be transpiled if defined using layered modeling, which output
* would be a type alias.
* @param zodType
* @param onlyArray Array types are always transpiled as alias in layered modeling.
* @returns
*/
static isTranspilerableAliasedZodType(zodType, onlyArray = false) {
var _a;
const type = typeof zodType === "string" ? zodType : (_a = zodType === null || zodType === void 0 ? void 0 : zodType.def) === null || _a === void 0 ? void 0 : _a.type;
if (onlyArray === true) {
return type === ZodFirstPartyTypeKind.ZodArray;
}
return (type === ZodFirstPartyTypeKind.ZodString ||
type === ZodFirstPartyTypeKind.ZodNumber ||
type === ZodFirstPartyTypeKind.ZodBigInt ||
type === ZodFirstPartyTypeKind.ZodBoolean ||
type === ZodFirstPartyTypeKind.ZodDate ||
type === ZodFirstPartyTypeKind.ZodAny ||
type === ZodFirstPartyTypeKind.ZodMap ||
type === ZodFirstPartyTypeKind.ZodSet ||
type === ZodFirstPartyTypeKind.ZodRecord ||
type === ZodFirstPartyTypeKind.ZodTuple ||
type === ZodFirstPartyTypeKind.ZodArray);
}
static cloneZod(i) {
return i.meta(Object.assign({}, (i.meta() || {})));
}
/**
* Zod2X generics are represented as Promise<"TypeName">.
* Ex: z.promise(z.literal("K")) will be transpiled to Template<K>
*/
static isZod2XGeneric(i) {
return (this.isZodPromise(i) &&
this.isZodLiteral(i.unwrap()) &&
typeof i.unwrap().def.values[0] === "string");
}
static createZodObject(properties) {
return zod_ext_1.Extended.getZ().object(Object.fromEntries(properties));
}
static getZodNumberConstraints(i) {
const constraints = {
isInt: this.isZodBigInt(i) || i.format === "safeint",
min: typeof i.minValue === "number" &&
isFinite(i.minValue) &&
Math.abs(i.minValue) !== Number.MAX_SAFE_INTEGER
? i.minValue
: undefined,
max: typeof i.maxValue === "number" &&
isFinite(i.maxValue) &&
Math.abs(i.maxValue) !== Number.MAX_SAFE_INTEGER
? i.maxValue
: undefined,
};
return constraints;
}
}
exports.ZodHelpers = ZodHelpers;
function createGenericType(name) {
return zod_ext_1.Extended.getZ().promise(zod_ext_1.Extended.getZ().literal(name));
}
function useGenericType(genObj, childrens, skipLazy) {
const builder = () => {
let extended = genObj;
for (const [key, property] of Object.entries(genObj.shape)) {
if (ZodHelpers.isZod2XGeneric(property)) {
const childType = childrens[key];
if (!childType) {
throw new Error(`Missing child type for generic property ${key} in ${genObj._zod2x.typeName}.`);
}
const zod2xMeta = structuredClone(extended._zod2x);
extended = extended.extend({ [key]: childType });
extended._zod2x = zod2xMeta;
extended._zod2x.isGenericChild = false;
if (!Array.isArray(extended._zod2x.genericTypes)) {
extended._zod2x.genericTypes = [];
}
if (childType._zod2x !== undefined) {
extended._zod2x.genericTypes.push({
typeName: childType._zod2x.typeName,
layer: childType._zod2x.layer,
});
}
}
}
return extended;
};
return skipLazy ? builder() : v4_1.z.lazy(builder);
}