Skip to content

Commit

Permalink
Merge pull request #15 from toiroakr/fix/nullish
Browse files Browse the repository at this point in the history
fix: return value for nullish schema
  • Loading branch information
toiroakr authored Dec 29, 2024
2 parents c63178e + 30de68e commit 7f13b78
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"zod": "^3.24.1"
},
"peerDependencies": {
"zod": "3.x",
"typescript": "5.x"
"typescript": "5.x",
"zod": "3.x"
},
"packemon": [
{
Expand Down
32 changes: 23 additions & 9 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,30 @@ describe("make empty", () => {
expect(empty(schema)).toBeNull();
});

it("nullable", () => {
const schema = z.string().nullable();
expect(init(schema)).toBeNull();
expect(empty(schema)).toBeNull();
describe("nullable", () => {
it("string", () => {
const schema = z.string().nullable();
expect(init(schema)).toBe("");
expect(empty(schema)).toBeNull();
});
it("array", () => {
const schema = z.array(z.string()).nullable();
expect(init(schema)).toStrictEqual([]);
expect(empty(schema)).toStrictEqual([]);
});
});

it("nullish", () => {
const schema = z.string().nullish();
expect(init(schema)).toBeNull();
expect(empty(schema)).toBeNull();
describe("nullish", () => {
it("string", () => {
const schema = z.string().nullish();
expect(init(schema)).toBe("");
expect(empty(schema)).toBeNull();
});
it("array", () => {
const schema = z.array(z.string()).nullish();
expect(init(schema)).toStrictEqual([]);
expect(empty(schema)).toStrictEqual([]);
});
});

it("any", () => {
Expand All @@ -235,7 +249,7 @@ describe("make empty", () => {

it("optional", () => {
const schema = z.string().optional();
expect(init(schema)).toBeUndefined();
expect(init(schema)).toBe("");
expect(empty(schema)).toBeNull();
});

Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ export function init<T extends ZodTypeAny>(schema: T): input<T> {
case "ZodNaN":
return Number.NaN;
case "ZodNull":
case "ZodNullable":
case "ZodAny":
return null;
// case "ZodOptional":
case "ZodNullable":
case "ZodOptional":
return init(def.innerType);
// case "ZodUndefined":
// case "ZodVoid":
// case "ZodUnknown":
Expand All @@ -81,9 +82,6 @@ export function init<T extends ZodTypeAny>(schema: T): input<T> {
}

export function empty<T extends ZodTypeAny>(schema: T): input<T> {
if (schema.isNullable()) {
return null;
}
const def = schema._def;
switch (def.typeName) {
case "ZodObject": {
Expand Down Expand Up @@ -111,6 +109,7 @@ export function empty<T extends ZodTypeAny>(schema: T): input<T> {
return Object.assign(empty(def.left) as any, empty(def.right));
case "ZodPipeline":
return empty(def.in);
case "ZodNullable":
case "ZodOptional":
return empty(def.innerType);
case "ZodLiteral":
Expand Down

0 comments on commit 7f13b78

Please sign in to comment.