diff --git a/README.md b/README.md index cf82ca7..e2e13a9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # typedoc-plugin-zod -Improves display of types created with [Zod](https://github.com/colinhacks/zod)'s `z.infer` type. +Improves display of types created with [Zod](https://github.com/colinhacks/zod)'s `z.infer` and `z.input` types. ## Usage diff --git a/src/plugin.ts b/src/plugin.ts index 61550d9..e2a1912 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -40,7 +40,8 @@ export function load(app: Application) { !refl.kindOf(ReflectionKind.TypeAlias) || refl.type?.type !== "reference" || refl.type.package !== "zod" || - refl.type.qualifiedName !== "TypeOf" + (refl.type.qualifiedName !== "TypeOf" && + refl.type.qualifiedName !== "input") ) { return; } diff --git a/src/test/plugin.test.ts b/src/test/plugin.test.ts index 0c258e7..f3d4567 100644 --- a/src/test/plugin.test.ts +++ b/src/test/plugin.test.ts @@ -2,9 +2,9 @@ import outdent from "outdent"; import { Application, DeclarationReflection, - ProjectReflection, TSConfigReader, TypeScript as ts, + ReflectionType, } from "typedoc"; import { test, expect, beforeAll } from "vitest"; import { load } from "../plugin"; @@ -48,6 +48,7 @@ test("infer.ts", () => { expect(typeDeclaration.toStringHierarchy()).toBe(outdent` TypeAlias Abc:Object TypeLiteral __type + Property def:string Property opt:string Property other:{ arr: number[]; } Property prop:string @@ -59,6 +60,40 @@ test("infer.ts", () => { expect(schemaDeclaration.toStringHierarchy()).toBe( "Variable abc:ZodObject", ); + + expect( + (typeDeclaration.type as ReflectionType)!.declaration!.getChildByName( + "def", + )!.flags.isOptional, + ).toBe(false); +}); + +test("input.ts", () => { + const project = convert("input.ts"); + const typeDeclaration = project.getChildByName( + "Abc", + ) as DeclarationReflection; + expect(typeDeclaration.toStringHierarchy()).toBe(outdent` + TypeAlias Abc:Object + TypeLiteral __type + Property def:string + Property opt:string + Property other:{ arr: number[]; } + Property prop:string + `); + + const schemaDeclaration = project.getChildByName( + "abc", + ) as DeclarationReflection; + expect(schemaDeclaration.toStringHierarchy()).toBe( + "Variable abc:ZodObject", + ); + + expect( + (typeDeclaration.type as ReflectionType)!.declaration!.getChildByName( + "def", + )!.flags.isOptional, + ).toBe(true); }); test("Schemas which have multiple declarations, #2", () => { diff --git a/src/testdata/infer.ts b/src/testdata/infer.ts index 0d85320..a5d9823 100644 --- a/src/testdata/infer.ts +++ b/src/testdata/infer.ts @@ -10,6 +10,7 @@ import z from "zod"; * arr: zod.array(zod.number()), * }), * opt: z.string().optional(), + * def: z.string().default("abc"), * }); * ``` */ @@ -19,6 +20,7 @@ export const abc = z.object({ arr: z.array(z.number()), }), opt: z.string().optional(), + def: z.string().default("abc"), }); /** diff --git a/src/testdata/input.ts b/src/testdata/input.ts new file mode 100644 index 0000000..97c7dbb --- /dev/null +++ b/src/testdata/input.ts @@ -0,0 +1,34 @@ +import z from "zod"; + +/** + * An example zod object for demonstration. This is declared as: + * + * ```ts + * export const abc = zod.object({ + * prop: z.string(), + * other: zod.object({ + * arr: zod.array(zod.number()), + * }), + * opt: z.string().optional(), + * def: z.string().default('abc') + * }); + * ``` + */ +export const abc = z.object({ + prop: z.string(), + other: z.object({ + arr: z.array(z.number()), + }), + opt: z.string().optional(), + def: z.string().default("abc"), +}); + +/** + * Exported type alias which infers its input type using the {@link abc} schema. + * + * This is declared as: + * ```ts + * export type Abc = zod.input; + * ``` + */ +export type Abc = z.input;