Skip to content

Commit

Permalink
test: extend schemas with openapi (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
vacas5 authored Dec 4, 2024
1 parent 6e435aa commit dd5aae4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
14 changes: 11 additions & 3 deletions mocks/schemas.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
import { z } from "zod";

const BodySchema = z.object({ name: z.string() });
const QuerySchema = z.object({ age: z.number().optional() });
extendZodWithOpenApi(z);

const BaseSchema = z.object({ name: z.string() });

const BodySchema = BaseSchema.openapi("Body", { title: "User", description: "Required user information" });
const QuerySchema = BaseSchema.extend({ age: z.number().optional() }).openapi({
title: "User details",
description: "Optional user information",
});
const ParamsSchema = z.object({ id: z.string() });
const ResponseSchema = z.object({ success: z.boolean() });

export { BodySchema, ParamsSchema, QuerySchema, ResponseSchema, UnusedSchema };
export { BodySchema, ParamsSchema, QuerySchema, ResponseSchema };
9 changes: 9 additions & 0 deletions src/openAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ describe("buildOpenAPIDocument", () => {
type: "object",
properties: { name: { type: "string" } },
required: ["name"],
title: "User",
description: "Required user information",
});
expect(document.components!.schemas!.QuerySchema).to.deep.equal({
type: "object",
properties: { name: { type: "string" }, age: { type: "number" } },
required: ["name"],
title: "User details",
description: "Optional user information",
});
});

Expand Down
10 changes: 5 additions & 5 deletions src/openAPIRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("openAPIRoute", () => {
},
);

const req = mockRequest({ name: "John" }, { age: 30 }, { id: "123" });
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

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

const req = mockRequest({ name: 123 }, { age: 30 }, { id: "123" });
const req = mockRequest({ name: 123 }, { age: 30, name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

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

const req = mockRequest({ name: "John" }, { age: "thirty" }, { id: "123" });
const req = mockRequest({ name: "John" }, { age: "thirty", name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

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

const req = mockRequest({ name: "John" }, { age: 30 }, { id: 123 });
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: 123 });
const res = mockResponse();
const next = mockNext();

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

const req = mockRequest({ name: "John" }, { age: 30 }, { id: "123" });
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

Expand Down

0 comments on commit dd5aae4

Please sign in to comment.