fixed duplicate check
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m22s

This commit is contained in:
christian 2024-06-02 16:05:45 +02:00
parent aa209bc0a8
commit f52d6d6004

View File

@ -6,16 +6,19 @@ import { countries } from "../db/schema";
import { ZodError, z } from "zod"; import { ZodError, z } from "zod";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
const schema = z.object({
name: z.string().min(1, { message: "Name is required" }),
});
export async function addCountry(prevstate: any, formData: FormData) { export async function addCountry(prevstate: any, formData: FormData) {
const name = formData.get("name") as string; const name = formData.get("name") as string;
const exists = await db const exists = await db
.select({ name: countries.name }) .select({ name: countries.name })
.from(countries) .from(countries)
.where(eq(countries.name, name)); .where(eq(countries.name, name));
const schema = z.object({
name: z
.string()
.min(1, { message: "Name is required" })
.refine(() => !exists[0], { message: "Country already exists" }),
});
try { try {
schema.parse({ schema.parse({
name, name,