prisma-zod-generator
Version:
Prisma 2+ generator to emit Zod schemas from your Prisma schema
35 lines • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NullableJsonValue = exports.InputJsonValueSchema = exports.JsonValueSchema = exports.transformJsonNull = void 0;
const zod_1 = require("zod");
const transformJsonNull = (v) => {
if (v == null || v === 'DbNull')
return null; // collapse to null
if (v === 'JsonNull')
return null; // treat both sentinels as null in pure model context
return v;
};
exports.transformJsonNull = transformJsonNull;
// Recursive JSON value schema (runtime JSON shape). Allows null.
// Note: Precise typing of recursive JSON is tricky; we provide broad ZodType<any> then cast for convenience.
exports.JsonValueSchema = zod_1.z.lazy(() => zod_1.z.union([
zod_1.z.string(),
zod_1.z.number(),
zod_1.z.boolean(),
zod_1.z.literal(null),
zod_1.z.record(zod_1.z.string(), zod_1.z.lazy(() => exports.JsonValueSchema.optional())),
zod_1.z.array(zod_1.z.lazy(() => exports.JsonValueSchema)),
]));
// Input JSON value schema (accepts toJSON objects + nullable branches).
exports.InputJsonValueSchema = zod_1.z.lazy(() => zod_1.z.union([
zod_1.z.string(),
zod_1.z.number(),
zod_1.z.boolean(),
zod_1.z.record(zod_1.z.string(), zod_1.z.lazy(() => zod_1.z.union([exports.InputJsonValueSchema, zod_1.z.literal(null)]))),
zod_1.z.array(zod_1.z.lazy(() => zod_1.z.union([exports.InputJsonValueSchema, zod_1.z.literal(null)]))),
]));
// Nullable JSON input with sentinel handling (DbNull / JsonNull strings for convenience)
exports.NullableJsonValue = zod_1.z
.union([exports.JsonValueSchema, zod_1.z.literal('DbNull'), zod_1.z.literal('JsonNull'), zod_1.z.literal(null)])
.transform((v) => (0, exports.transformJsonNull)(v));
//# sourceMappingURL=json-helpers.js.map