Skip to content

Commit

Permalink
Merge pull request #17 from toiroakr/feat/lazy
Browse files Browse the repository at this point in the history
feat: support lazy
  • Loading branch information
toiroakr authored Dec 30, 2024
2 parents 1dc9ee9 + e430e1d commit e15d80e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ describe("make empty", () => {
expect(empty(schema)).toBeNull();
});

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

it("tuple", () => {
const schema = z.tuple([z.string(), z.number()]);
expect(init(schema)).toEqual(["", 0]);
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export function init<T extends ZodTypeAny>(schema: T): input<T> {
return Object.assign(init(def.left) as any, init(def.right));
case "ZodFunction":
return (..._: any[]) => init(def.returns);
case "ZodLazy":
return init(def.getter());
case "ZodPipeline":
return init(def.in);
case "ZodDefault":
Expand Down Expand Up @@ -109,6 +111,8 @@ export function empty<T extends ZodTypeAny>(schema: T): input<T> {
return empty(Array.from(def.options.values() as any[])[0]);
case "ZodIntersection":
return Object.assign(empty(def.left) as any, empty(def.right));
case "ZodLazy":
return empty(def.getter());
case "ZodPipeline":
return empty(def.in);
case "ZodNullable":
Expand Down
8 changes: 0 additions & 8 deletions type.ts

This file was deleted.

0 comments on commit e15d80e

Please sign in to comment.