Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/artists/__tests__/createArtistPostHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe("createArtistPostHandler", () => {
expect(response.status).toBe(400);
});

it("returns 400 for invalid JSON body", async () => {
it("returns 400 for invalid JSON body (treated as empty)", async () => {
const request = new NextRequest("http://localhost/api/artists", {
method: "POST",
headers: {
Expand All @@ -181,8 +181,9 @@ describe("createArtistPostHandler", () => {
const response = await createArtistPostHandler(request);
const data = await response.json();

// safeParseJson returns {} for invalid JSON, so schema validation catches it
expect(response.status).toBe(400);
expect(data.error).toBe("Invalid JSON body");
expect(data.error).toBe("name is required");
});

it("returns 500 when artist creation fails", async () => {
Expand Down
5 changes: 3 additions & 2 deletions lib/artists/__tests__/validateCreateArtistBody.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe("validateCreateArtistBody", () => {
}
});

it("returns 400 for invalid JSON body", async () => {
it("returns schema error for invalid JSON body (treated as empty)", async () => {
const request = new NextRequest("http://localhost/api/artists", {
method: "POST",
headers: {
Expand All @@ -144,11 +144,12 @@ describe("validateCreateArtistBody", () => {

const result = await validateCreateArtistBody(request);

// safeParseJson returns {} for invalid JSON, so schema validation catches it
expect(result).toBeInstanceOf(NextResponse);
if (result instanceof NextResponse) {
expect(result.status).toBe(400);
const data = await result.json();
expect(data.error).toBe("Invalid JSON body");
expect(data.error).toBe("name is required");
}
});

Expand Down
26 changes: 5 additions & 21 deletions lib/artists/validateCreateArtistBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ import { NextRequest, NextResponse } from "next/server";
import { getCorsHeaders } from "@/lib/networking/getCorsHeaders";
import { getApiKeyDetails } from "@/lib/keys/getApiKeyDetails";
import { canAccessAccount } from "@/lib/organizations/canAccessAccount";
import { safeParseJson } from "@/lib/networking/safeParseJson";
import { z } from "zod";

export const createArtistBodySchema = z.object({
name: z
.string({ message: "name is required" })
.min(1, "name cannot be empty"),
account_id: z
.string()
.uuid("account_id must be a valid UUID")
.optional(),
organization_id: z
.string()
.uuid("organization_id must be a valid UUID")
.optional(),
name: z.string({ message: "name is required" }).min(1, "name cannot be empty"),
account_id: z.uuid({ message: "account_id must be a valid UUID" }).optional(),
organization_id: z.uuid({ message: "organization_id must be a valid UUID" }).optional(),
});

export type CreateArtistBody = z.infer<typeof createArtistBodySchema>;
Expand Down Expand Up @@ -52,16 +45,7 @@ export async function validateCreateArtistBody(
);
}

let body: unknown;
try {
body = await request.json();
} catch {
return NextResponse.json(
{ status: "error", error: "Invalid JSON body" },
{ status: 400, headers: getCorsHeaders() },
);
}

const body = await safeParseJson(request);
const result = createArtistBodySchema.safeParse(body);
if (!result.success) {
const firstError = result.error.issues[0];
Expand Down