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
26 changes: 14 additions & 12 deletions src/app/api/profile/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,20 @@ export async function PUT(request: NextRequest) {
}

// Check if username is taken by another user
const { data: existingUser } = await supabase
.from("profiles")
.select("id")
.eq("username", validationResult.data.username)
.neq("id", user.id)
.single();

if (existingUser) {
return NextResponse.json(
{ error: "Username is already taken" },
{ status: 400 }
);
if (validationResult.data.username) {
const { data: existingUser } = await supabase
.from("profiles")
.select("id")
.eq("username", validationResult.data.username)
.neq("id", user.id)
.maybeSingle();

if (existingUser) {
return NextResponse.json(
{ error: "Username is already taken" },
{ status: 400 }
);
}
}
Comment on lines +82 to 96

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Guard is unreachable — schema still requires username

profileSchema defines username without .optional() (see src/lib/validations.ts lines 76-83), so when a PATCH request omits username, safeParse returns success: false and the handler already returns a 400 at lines 57-62 — the execution never reaches the new if (validationResult.data.username) guard. The partial-update behaviour described in the PR is therefore not actually unlocked: requests without username still fail with a validation error, not a successful partial update.

To make the guard meaningful, username in profileSchema (or a dedicated partial schema used only by PATCH) needs to be marked .optional().


// Get current profile to check for resume changes
Expand Down
3 changes: 2 additions & 1 deletion src/lib/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export const profileSchema = z.object({
.regex(
/^[a-zA-Z0-9_-]+$/,
"Username can only contain letters, numbers, underscores, and hyphens"
),
)
.optional(),
full_name: z.string().max(100).optional().nullable(),
bio: z.string().max(1000).optional().nullable(),
skills: z.array(z.string()).max(20).default([]),
Expand Down
Loading