diff --git a/README.md b/README.md index 009993c..9f5447f 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ const schema = z.object({ age: z.number().min(10), }); // create default values with zod-empty -const defaultValues = init>(schema); +const defaultValues = init(schema); const App = () => { const { diff --git a/samples/react-hook-form/src/App.tsx b/samples/react-hook-form/src/App.tsx index 636704a..be9b31f 100644 --- a/samples/react-hook-form/src/App.tsx +++ b/samples/react-hook-form/src/App.tsx @@ -10,7 +10,7 @@ const schema = z.object({ age: z.number().min(10).default(100), }); // create default values with zod-empty -const defaultValues = init>(schema); +const defaultValues = init(schema); const App = () => { const { diff --git a/src/index.spec.ts b/src/index.spec.ts index b3a21c7..72c8016 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -110,7 +110,7 @@ describe("make empty", () => { .args(z.number(), z.string()) .returns(z.boolean()); type SchemaType = z.infer; - expect(init(schema)(0, "")).toBe(false); + expect(init(schema)(0, "")).toBe(false); }); it("tuple", () => { diff --git a/src/index.ts b/src/index.ts index 98e1141..27563a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,15 @@ /* eslint @typescript-eslint/no-explicit-any: off */ import clone from "just-clone"; -import type { ZodTypeAny } from "zod"; +import type { input, ZodTypeAny } from "zod"; -function make(schema: T): unknown { +function init(schema: T): input { const def = schema._def; switch (def.typeName) { case "ZodObject": const outputObject: Record = {}; Object.entries(def.shape()).forEach( - ([key, value]) => (outputObject[key] = make(value as ZodTypeAny)) + ([key, value]) => (outputObject[key] = init(value as ZodTypeAny)) ); return outputObject; case "ZodRecord": @@ -32,11 +32,11 @@ function make(schema: T): unknown { case "ZodLiteral": return def.value; case "ZodEffects": - return make(def.schema); + return init(def.schema); case "ZodArray": return []; case "ZodTuple": - return def.items.map((item: ZodTypeAny) => make(item)); + return def.items.map((item: ZodTypeAny) => init(item)); case "ZodSet": return new Set(); case "ZodMap": @@ -49,14 +49,14 @@ function make(schema: T): unknown { (value) => typeof def.values[value as any] !== "number" )[0]; case "ZodUnion": - return make(def.options[0]); + return init(def.options[0]); case "ZodDiscriminatedUnion": - return make(Array.from(def.options.values() as any[])[0]); + return init(Array.from(def.options.values() as any[])[0]); case "ZodIntersection": - return Object.assign(make(def.left) as any, make(def.right)); + return Object.assign(init(def.left) as any, init(def.right)); case "ZodFunction": // eslint-disable-next-line @typescript-eslint/no-unused-vars - return (..._: any[]) => make(def.returns); + return (..._: any[]) => init(def.returns); case "ZodDefault": return def.innerType._def.typeName === "ZodFunction" ? def.defaultValue() @@ -76,7 +76,4 @@ function make(schema: T): unknown { return undefined; } } -function init(schema: any): R { - return make(schema) as R; -} export default init;