Skip to content

Commit 4d764af

Browse files
committed
Testing interface schema within integration.
1 parent 0dacc53 commit 4d764af

2 files changed

Lines changed: 104 additions & 26 deletions

File tree

express-zod-api/tests/__snapshots__/integration.spec.ts.snap

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,76 @@ export type Request = keyof Input;
586586
"
587587
`;
588588
589-
exports[`Integration > Should support types variant and handle recursive schemas 1`] = `
589+
exports[`Integration > Should support types variant and handle recursive schemas 0 1`] = `
590+
"type Type1 = {
591+
name: string;
592+
features: Type1;
593+
};
594+
595+
type SomeOf<T> = T[keyof T];
596+
597+
/** post /v1/test */
598+
type PostV1TestInput = {
599+
features: Type1;
600+
};
601+
602+
/** post /v1/test */
603+
type PostV1TestPositiveVariant1 = {
604+
status: "success";
605+
data: {};
606+
};
607+
608+
/** post /v1/test */
609+
interface PostV1TestPositiveResponseVariants {
610+
200: PostV1TestPositiveVariant1;
611+
}
612+
613+
/** post /v1/test */
614+
type PostV1TestNegativeVariant1 = {
615+
status: "error";
616+
error: {
617+
message: string;
618+
};
619+
};
620+
621+
/** post /v1/test */
622+
interface PostV1TestNegativeResponseVariants {
623+
400: PostV1TestNegativeVariant1;
624+
}
625+
626+
export type Path = "/v1/test";
627+
628+
export type Method = "get" | "post" | "put" | "delete" | "patch";
629+
630+
export interface Input {
631+
/** @deprecated */
632+
"post /v1/test": PostV1TestInput;
633+
}
634+
635+
export interface PositiveResponse {
636+
/** @deprecated */
637+
"post /v1/test": SomeOf<PostV1TestPositiveResponseVariants>;
638+
}
639+
640+
export interface NegativeResponse {
641+
/** @deprecated */
642+
"post /v1/test": SomeOf<PostV1TestNegativeResponseVariants>;
643+
}
644+
645+
export interface EncodedResponse {
646+
/** @deprecated */
647+
"post /v1/test": PostV1TestPositiveResponseVariants & PostV1TestNegativeResponseVariants;
648+
}
649+
650+
export interface Response {
651+
/** @deprecated */
652+
"post /v1/test": PositiveResponse["post /v1/test"] | NegativeResponse["post /v1/test"];
653+
}
654+
655+
export type Request = keyof Input;"
656+
`;
657+
658+
exports[`Integration > Should support types variant and handle recursive schemas 1 1`] = `
590659
"type Type1 = {
591660
name: string;
592661
features: Type1;

express-zod-api/tests/integration.spec.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,42 @@ import {
99
} from "../src";
1010

1111
describe("Integration", () => {
12-
test("Should support types variant and handle recursive schemas", () => {
13-
const recursiveSchema: z.ZodTypeAny = z.lazy(() =>
14-
z.object({
15-
name: z.string(),
16-
features: recursiveSchema,
17-
}),
18-
);
12+
const recursive1: z.ZodTypeAny = z.lazy(() =>
13+
z.object({
14+
name: z.string(),
15+
features: recursive1,
16+
}),
17+
);
18+
const recursive2 = z.interface({
19+
name: z.string(),
20+
get features() {
21+
return recursive2;
22+
},
23+
});
1924

20-
const client = new Integration({
21-
variant: "types",
22-
routing: {
23-
v1: {
24-
test: defaultEndpointsFactory
25-
.build({
26-
method: "post",
27-
input: z.object({
28-
features: recursiveSchema,
29-
}),
30-
output: z.object({}),
31-
handler: async () => ({}),
32-
})
33-
.deprecated(),
25+
test.each([recursive1, recursive2])(
26+
"Should support types variant and handle recursive schemas %#",
27+
(recursiveSchema) => {
28+
const client = new Integration({
29+
variant: "types",
30+
routing: {
31+
v1: {
32+
test: defaultEndpointsFactory
33+
.build({
34+
method: "post",
35+
input: z.object({
36+
features: recursiveSchema,
37+
}),
38+
output: z.object({}),
39+
handler: async () => ({}),
40+
})
41+
.deprecated(),
42+
},
3443
},
35-
},
36-
});
37-
expect(client.print()).toMatchSnapshot();
38-
});
44+
});
45+
expect(client.print()).toMatchSnapshot();
46+
},
47+
);
3948

4049
test("Should treat optionals the same way as z.infer() by default", async () => {
4150
const client = new Integration({

0 commit comments

Comments
 (0)