mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Switch all routes to use Zod for strict validation
This commit is contained in:
parent
53fa9ca545
commit
0b1c1ba128
67 changed files with 2459 additions and 2600 deletions
|
|
@ -58,11 +58,11 @@ describe("RequestParser", () => {
|
|||
headers: { "Content-Type": "application/json" },
|
||||
body: "invalid json",
|
||||
});
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
const result = new RequestParser(request).toObject<{
|
||||
param1: string;
|
||||
param2: string;
|
||||
}>();
|
||||
expect(result).toEqual({});
|
||||
expect(result).rejects.toThrow();
|
||||
});
|
||||
|
||||
describe("should parse form data correctly", () => {
|
||||
|
|
|
|||
|
|
@ -108,8 +108,8 @@ export const processRoute = async (
|
|||
}
|
||||
}
|
||||
|
||||
// Check if Content-Type header is missing in POST, PUT and PATCH requests
|
||||
if (["POST", "PUT", "PATCH"].includes(request.method)) {
|
||||
// Check if Content-Type header is missing if there is a body
|
||||
if (request.body) {
|
||||
if (!request.headers.has("Content-Type")) {
|
||||
return errorResponse(
|
||||
`Content-Type header is missing but required on method ${request.method}`,
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ describe("Route Processor", () => {
|
|||
expect(output.status).toBe(401);
|
||||
});
|
||||
|
||||
it("should return a 400 when the Content-Type header is missing in POST, PUT and PATCH requests", async () => {
|
||||
it("should return a 400 when the Content-Type header is missing but there is a body", async () => {
|
||||
mock.module(
|
||||
"./route",
|
||||
() =>
|
||||
|
|
@ -147,35 +147,12 @@ describe("Route Processor", () => {
|
|||
} as MatchedRoute,
|
||||
new Request("https://test.com/route", {
|
||||
method: "POST",
|
||||
body: "test",
|
||||
}),
|
||||
new LogManager(Bun.file("/dev/null")),
|
||||
);
|
||||
|
||||
expect(output.status).toBe(400);
|
||||
|
||||
const output2 = await processRoute(
|
||||
{
|
||||
filePath: "./route",
|
||||
} as MatchedRoute,
|
||||
new Request("https://test.com/route", {
|
||||
method: "PUT",
|
||||
}),
|
||||
new LogManager(Bun.file("/dev/null")),
|
||||
);
|
||||
|
||||
expect(output2.status).toBe(400);
|
||||
|
||||
const output3 = await processRoute(
|
||||
{
|
||||
filePath: "./route",
|
||||
} as MatchedRoute,
|
||||
new Request("https://test.com/route", {
|
||||
method: "PATCH",
|
||||
}),
|
||||
new LogManager(Bun.file("/dev/null")),
|
||||
);
|
||||
|
||||
expect(output3.status).toBe(400);
|
||||
});
|
||||
|
||||
it("should return a 400 when the request could not be parsed", async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue