zod-to-x
Version:
Multi language types generation from Zod schemas.
194 lines (193 loc) • 9.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZodHelpers = exports.ZodFirstPartyTypeKind = void 0;
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 || (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;
return (((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodUnion &&
i.def.discriminator === undefined);
}
static isZodDiscriminatedUnion(i) {
var _a;
return (((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodUnion &&
i.def.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 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() || {})));
}
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;