slonik-trpc
Version:
Slonik tRPC loader
79 lines (78 loc) • 3.62 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mockZod = exports.notEmpty = void 0;
const zod = __importStar(require("zod"));
function notEmpty(value) {
return value !== null && value !== undefined;
}
exports.notEmpty = notEmpty;
function mockZod(field, options) {
var _a, _b, _c, _d, _e;
const fieldName = (options === null || options === void 0 ? void 0 : options.fieldName) || '';
if ((options === null || options === void 0 ? void 0 : options.zodMappers) && ((_a = field === null || field === void 0 ? void 0 : field._def) === null || _a === void 0 ? void 0 : _a.typeName) in options.zodMappers) {
return options.zodMappers[field._def.typeName](fieldName, field);
}
if ((options === null || options === void 0 ? void 0 : options.mappers) && fieldName in options.mappers) {
return options.mappers[fieldName](fieldName, field);
}
if (field instanceof zod.ZodNumber) {
return 1;
}
else if (field instanceof zod.ZodString) {
return '2023-01-01';
}
else if (field instanceof zod.ZodArray) {
return Math.random() > 0.5 ? [mockZod(field._def.type, options)] : [];
}
else if (field instanceof zod.ZodBoolean) {
return Math.random() > 0.5;
}
else if (field instanceof zod.ZodUnion) {
return mockZod(field._def.options[0], options);
}
else if (field instanceof zod.ZodEnum) {
return (_c = (_b = field._def) === null || _b === void 0 ? void 0 : _b.values) === null || _c === void 0 ? void 0 : _c[0];
}
else if (field instanceof zod.ZodNullable || field instanceof zod.ZodOptional) {
return mockZod(field._def.innerType, options);
}
else if (field instanceof zod.ZodEffects) {
return mockZod(field._def.schema, options);
}
else if (field instanceof zod.ZodNever) {
return undefined;
}
else if (field instanceof zod.ZodNull) {
return null;
}
else if (field instanceof zod.ZodDefault) {
return (_d = field._def) === null || _d === void 0 ? void 0 : _d.defaultValue();
}
else if (field instanceof zod.ZodLazy && ((options === null || options === void 0 ? void 0 : options.counter) || 0) < 100) {
return mockZod(field._def.getter(), Object.assign(Object.assign({}, options), { counter: ((options === null || options === void 0 ? void 0 : options.counter) || 0) + 1 }));
}
if (field instanceof zod.ZodObject) {
return Object.fromEntries(Object.entries(field.shape).map(([key, value]) => [key, mockZod(value, Object.assign(Object.assign({}, options), { fieldName: key }))]));
}
return (_e = field._def) === null || _e === void 0 ? void 0 : _e.typeName;
}
exports.mockZod = mockZod;