From 9d7a1b72ffde53e150db79c73476a204452f4ba4 Mon Sep 17 00:00:00 2001 From: Denis Bessa Date: Wed, 8 Jan 2025 14:09:23 -0300 Subject: [PATCH 1/2] fix: update init function to return output type and handle UUID generation for ZodString --- src/index.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index b09c896..790c531 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import clone from "just-clone"; -import type { ZodTypeAny, input } from "zod"; +import type { ZodTypeAny, input, output } from "zod"; -export function init(schema: T): input { +export function init(schema: T): output { const def = schema._def; switch (def.typeName) { case "ZodObject": { @@ -13,8 +13,16 @@ export function init(schema: T): input { } case "ZodRecord": return {}; - case "ZodString": + case "ZodString": { + if (def.checks) { + for (const check of def.checks) { + if (check.kind === "uuid") { + return crypto.randomUUID(); + } + } + } return ""; + } case "ZodNumber": for (const check of def.checks || []) { if (["min", "max"].includes(check.kind)) { From fdada69528696f7af564d904a49c3cec0827f5e7 Mon Sep 17 00:00:00 2001 From: Denis Bessa Date: Thu, 9 Jan 2025 13:00:41 -0300 Subject: [PATCH 2/2] fix: update init function to handle nullable types and add UUID test case --- src/index.spec.ts | 18 ++++++++++++------ src/index.ts | 8 ++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index d01de0e..71a20a6 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -16,6 +16,12 @@ describe("make empty", () => { expect(empty(schema)).toBeNull(); }); + it("uuid", () => { + const schema = z.string().uuid(); + expect(init(schema).length).toBe(36); + expect(empty(schema)).toBeNull(); + }); + it.each([ ["", z.number(), 0], ["10-", z.number().min(10), 10], @@ -228,17 +234,17 @@ describe("make empty", () => { describe("nullable", () => { it("string", () => { const schema = z.string().nullable(); - expect(init(schema)).toBe(""); + expect(init(schema)).toBeNull(); expect(empty(schema)).toBeNull(); }); it("number", () => { const schema = z.number().nullable(); - expect(init(schema)).toBe(0); + expect(init(schema)).toBeNull(); expect(empty(schema)).toBeNull(); }); it("array", () => { const schema = z.array(z.string()).nullable(); - expect(init(schema)).toStrictEqual([]); + expect(init(schema)).toBeNull(); expect(empty(schema)).toStrictEqual([]); }); }); @@ -246,17 +252,17 @@ describe("make empty", () => { describe("nullish", () => { it("string", () => { const schema = z.string().nullish(); - expect(init(schema)).toBe(""); + expect(init(schema)).toBeNull(); expect(empty(schema)).toBeNull(); }); it("number", () => { const schema = z.number().nullish(); - expect(init(schema)).toBe(0); + expect(init(schema)).toBeNull(); expect(empty(schema)).toBeNull(); }); it("array", () => { const schema = z.array(z.string()).nullish(); - expect(init(schema)).toStrictEqual([]); + expect(init(schema)).toBeNull(); expect(empty(schema)).toStrictEqual([]); }); }); diff --git a/src/index.ts b/src/index.ts index 790c531..7ece4ec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,10 @@ import type { ZodTypeAny, input, output } from "zod"; export function init(schema: T): output { const def = schema._def; + if (schema.isNullable() && def.typeName !== "ZodDefault") { + return null; + } + switch (def.typeName) { case "ZodObject": { const outputObject: Record = {}; @@ -76,7 +80,6 @@ export function init(schema: T): output { case "ZodNull": case "ZodAny": return null; - case "ZodNullable": case "ZodOptional": return init(def.innerType); // case "ZodUndefined": @@ -84,9 +87,6 @@ export function init(schema: T): output { // case "ZodUnknown": // case "ZodNever": default: - if (schema.isNullable()) { - return null; - } return undefined; } }