refactor: move slugSchema into schemas file and rename to idSchema

This commit is contained in:
trafficlunar 2025-04-20 17:37:38 +01:00
parent b369d1f5c7
commit 48c99e442f
3 changed files with 9 additions and 12 deletions

View file

@ -6,20 +6,16 @@ import path from "path";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { idSchema } from "@/lib/schemas";
const uploadsDirectory = path.join(process.cwd(), "public", "mii");
const slugSchema = z.coerce
.number({ message: "Mii ID must be a number" })
.int({ message: "Mii ID must be an integer" })
.positive({ message: "Mii ID must be valid" });
export async function DELETE(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const { id: slugId } = await params;
const parsed = slugSchema.safeParse(slugId);
const parsed = idSchema.safeParse(slugId);
if (!parsed.success) return NextResponse.json({ error: parsed.error.errors[0].message }, { status: 400 });
const miiId = parsed.data;

View file

@ -3,18 +3,14 @@ import { z } from "zod";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
const slugSchema = z.coerce
.number({ message: "Mii ID must be a number" })
.int({ message: "Mii ID must be an integer" })
.positive({ message: "Mii ID must be valid" });
import { idSchema } from "@/lib/schemas";
export async function PATCH(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const { id: slugId } = await params;
const parsed = slugSchema.safeParse(slugId);
const parsed = idSchema.safeParse(slugId);
if (!parsed.success) return NextResponse.json({ error: parsed.error.errors[0].message }, { status: 400 });
const miiId = parsed.data;

View file

@ -32,6 +32,11 @@ export const tagsSchema = z
.min(1, { message: "There must be at least 1 tag" })
.max(8, { message: "There cannot be more than 8 tags" });
export const idSchema = z.coerce
.number({ message: "Mii ID must be a number" })
.int({ message: "Mii ID must be an integer" })
.positive({ message: "Mii ID must be valid" });
// Account Info
export const usernameSchema = z
.string()