Skip to content

Commit

Permalink
Merge branch 'main' of github.com:toiroakr/zod-empty
Browse files Browse the repository at this point in the history
  • Loading branch information
toiroakr committed Jan 10, 2025
2 parents 4465ca7 + 6be822d commit 88200de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
18 changes: 12 additions & 6 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -229,35 +235,35 @@ 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([]);
});
});

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([]);
});
});
Expand Down
22 changes: 15 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import clone from "just-clone";
import type { ZodTypeAny, input } from "zod";
import type { ZodTypeAny, input, output } from "zod";

export function init<T extends ZodTypeAny>(schema: T): input<T> {
export function init<T extends ZodTypeAny>(schema: T): output<T> {
const def = schema._def;
if (schema.isNullable() && def.typeName !== "ZodDefault") {
return null;
}

switch (def.typeName) {
case "ZodObject": {
const outputObject: Record<string, unknown> = {};
Expand All @@ -13,8 +17,16 @@ export function init<T extends ZodTypeAny>(schema: T): input<T> {
}
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)) {
Expand Down Expand Up @@ -68,17 +80,13 @@ export function init<T extends ZodTypeAny>(schema: T): input<T> {
case "ZodNull":
case "ZodAny":
return null;
case "ZodNullable":
case "ZodOptional":
return init(def.innerType);
// case "ZodUndefined":
// case "ZodVoid":
// case "ZodUnknown":
// case "ZodNever":
default:
if (schema.isNullable()) {
return null;
}
return undefined;
}
}
Expand Down

0 comments on commit 88200de

Please sign in to comment.