Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty string to null #19

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const schema = z.object({
});
// create default values with zod-empty
const defaultValues = init(schema); // => { name: "", age: 10, hobbies: [] }
// or const defaultValues = empty(schema); // => { name: "", age: null, hobbies: [] }
// or const defaultValues = empty(schema); // => { name: null, age: null, hobbies: [] }

const App = () => {
const {
Expand Down
26 changes: 13 additions & 13 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("make empty", () => {
it("string", () => {
const schema = z.string();
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();
});

it.each([
Expand Down Expand Up @@ -75,7 +75,7 @@ describe("make empty", () => {
buz: [],
});
expect(empty(schema)).toEqual({
foo: "",
foo: null,
bar: null,
buz: [],
});
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("make empty", () => {
it("union", () => {
let schema: z.Schema = z.union([z.string(), z.number()]);
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();

schema = z.union([z.number(), z.string()]);
expect(init(schema)).toBe(0);
Expand All @@ -120,7 +120,7 @@ describe("make empty", () => {
z.object({ type: z.literal("b"), b: z.string() }),
]);
expect(init(schema)).toEqual({ type: "a", a: "", c: 0 });
expect(empty(schema)).toEqual({ type: "a", a: "", c: null });
expect(empty(schema)).toEqual({ type: "a", a: null, c: null });
});

it("intersection", () => {
Expand All @@ -141,9 +141,9 @@ describe("make empty", () => {
salary: 0,
});
expect(empty(schema)).toEqual({
name: "",
name: null,
age: null,
role: "",
role: null,
salary: null,
});
});
Expand All @@ -161,13 +161,13 @@ describe("make empty", () => {
it("lazy", () => {
const schema = z.lazy(() => z.string());
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();
});

it("tuple", () => {
const schema = z.tuple([z.string(), z.number()]);
expect(init(schema)).toEqual(["", 0]);
expect(empty(schema)).toEqual(["", null]);
expect(empty(schema)).toEqual([null, null]);
});

it("set", () => {
Expand Down Expand Up @@ -229,7 +229,7 @@ describe("make empty", () => {
it("string", () => {
const schema = z.string().nullable();
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();
});
it("number", () => {
const schema = z.number().nullable();
Expand All @@ -247,7 +247,7 @@ describe("make empty", () => {
it("string", () => {
const schema = z.string().nullish();
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();
});
it("number", () => {
const schema = z.number().nullish();
Expand All @@ -270,7 +270,7 @@ describe("make empty", () => {
it("optional", () => {
const schema = z.string().optional();
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();
});

it("undefined", () => {
Expand Down Expand Up @@ -300,14 +300,14 @@ describe("make empty", () => {
it("pipe", () => {
let schema: any = z.string().pipe(z.number());
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();

schema = z.number().pipe(z.string());
expect(init(schema)).toBe(0);
expect(empty(schema)).toBeNull();

schema = z.string().pipe(z.number()).pipe(z.boolean());
expect(init(schema)).toBe("");
expect(empty(schema)).toBe("");
expect(empty(schema)).toBeNull();
});
});
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ export function empty<T extends ZodTypeAny>(schema: T): input<T> {
}
return outputObject;
}
case "ZodString":
return "";
case "ZodRecord":
return {};
case "ZodArray":
Expand Down
Loading