Skip to content

Commit

Permalink
Merge pull request #3 from boneskull/boneskull/z-input
Browse files Browse the repository at this point in the history
feat: support z.input
  • Loading branch information
Gerrit0 authored Jan 6, 2024
2 parents 0ac9414 + a504fd4 commit 30e0a70
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
37 changes: 36 additions & 1 deletion src/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -59,6 +60,40 @@ test("infer.ts", () => {
expect(schemaDeclaration.toStringHierarchy()).toBe(
"Variable abc:ZodObject<Abc>",
);

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<Abc>",
);

expect(
(typeDeclaration.type as ReflectionType)!.declaration!.getChildByName(
"def",
)!.flags.isOptional,
).toBe(true);
});

test("Schemas which have multiple declarations, #2", () => {
Expand Down
2 changes: 2 additions & 0 deletions src/testdata/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import z from "zod";
* arr: zod.array(zod.number()),
* }),
* opt: z.string().optional(),
* def: z.string().default("abc"),
* });
* ```
*/
Expand All @@ -19,6 +20,7 @@ export const abc = z.object({
arr: z.array(z.number()),
}),
opt: z.string().optional(),
def: z.string().default("abc"),
});

/**
Expand Down
34 changes: 34 additions & 0 deletions src/testdata/input.ts
Original file line number Diff line number Diff line change
@@ -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<typeof abc>;
* ```
*/
export type Abc = z.input<typeof abc>;

0 comments on commit 30e0a70

Please sign in to comment.