Skip to content

Commit dd5aae4

Browse files
authored
test: extend schemas with openapi (#17)
1 parent 6e435aa commit dd5aae4

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

mocks/schemas.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
12
import { z } from "zod";
23

3-
const BodySchema = z.object({ name: z.string() });
4-
const QuerySchema = z.object({ age: z.number().optional() });
4+
extendZodWithOpenApi(z);
5+
6+
const BaseSchema = z.object({ name: z.string() });
7+
8+
const BodySchema = BaseSchema.openapi("Body", { title: "User", description: "Required user information" });
9+
const QuerySchema = BaseSchema.extend({ age: z.number().optional() }).openapi({
10+
title: "User details",
11+
description: "Optional user information",
12+
});
513
const ParamsSchema = z.object({ id: z.string() });
614
const ResponseSchema = z.object({ success: z.boolean() });
715

8-
export { BodySchema, ParamsSchema, QuerySchema, ResponseSchema, UnusedSchema };
16+
export { BodySchema, ParamsSchema, QuerySchema, ResponseSchema };

src/openAPI.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ describe("buildOpenAPIDocument", () => {
6767
type: "object",
6868
properties: { name: { type: "string" } },
6969
required: ["name"],
70+
title: "User",
71+
description: "Required user information",
72+
});
73+
expect(document.components!.schemas!.QuerySchema).to.deep.equal({
74+
type: "object",
75+
properties: { name: { type: "string" }, age: { type: "number" } },
76+
required: ["name"],
77+
title: "User details",
78+
description: "Optional user information",
7079
});
7180
});
7281

src/openAPIRoute.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("openAPIRoute", () => {
6060
},
6161
);
6262

63-
const req = mockRequest({ name: "John" }, { age: 30 }, { id: "123" });
63+
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: "123" });
6464
const res = mockResponse();
6565
const next = mockNext();
6666

@@ -75,7 +75,7 @@ describe("openAPIRoute", () => {
7575
res.json({ success: true });
7676
});
7777

78-
const req = mockRequest({ name: 123 }, { age: 30 }, { id: "123" });
78+
const req = mockRequest({ name: 123 }, { age: 30, name: "John" }, { id: "123" });
7979
const res = mockResponse();
8080
const next = mockNext();
8181

@@ -91,7 +91,7 @@ describe("openAPIRoute", () => {
9191
res.json({ success: true });
9292
});
9393

94-
const req = mockRequest({ name: "John" }, { age: "thirty" }, { id: "123" });
94+
const req = mockRequest({ name: "John" }, { age: "thirty", name: "John" }, { id: "123" });
9595
const res = mockResponse();
9696
const next = mockNext();
9797

@@ -107,7 +107,7 @@ describe("openAPIRoute", () => {
107107
res.json({ success: true });
108108
});
109109

110-
const req = mockRequest({ name: "John" }, { age: 30 }, { id: 123 });
110+
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: 123 });
111111
const res = mockResponse();
112112
const next = mockNext();
113113

@@ -124,7 +124,7 @@ describe("openAPIRoute", () => {
124124
res.json({ success: "true" });
125125
});
126126

127-
const req = mockRequest({ name: "John" }, { age: 30 }, { id: "123" });
127+
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: "123" });
128128
const res = mockResponse();
129129
const next = mockNext();
130130

0 commit comments

Comments
 (0)