Files
sass-mohfarawati/lib/media-validation.ts
T
Moh ba63f75ea8
CI / quality (push) Waiting to run
REFACTORED - Replace Prisma with Drizzle ORM
- Add lib/db (schema, postgres.js client, enums, seed, migrations) on Drizzle
- Rewrite all lib and admin action queries from Prisma to Drizzle
- Keep existing table/column names so no data migration is needed
- Preserve signed-cookie admin auth unchanged
- Map unique-violation handling from Prisma P2002 to SQLSTATE 23505
- Swap deps, scripts, Makefile, and Dockerfile from Prisma to Drizzle
2026-08-08 02:09:03 +02:00

43 lines
1.2 KiB
TypeScript

import { MediaKind } from "@/lib/db/enums";
import { z } from "zod";
const mediaModeSchema = z.enum(["library", "external", "upload"]);
const optionalTrimmedText = z.string().trim().optional().transform((value) => value ?? "");
export const mediaFieldInputSchema = z
.object({
mode: mediaModeSchema,
assetId: optionalTrimmedText,
url: optionalTrimmedText,
label: optionalTrimmedText,
kind: z.nativeEnum(MediaKind),
})
.superRefine((value, context) => {
if (value.mode === "library" && !value.assetId) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: ["assetId"],
message: "Library selection requires a media asset.",
});
}
if (value.mode === "external" && !value.url) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: ["url"],
message: "External media requires a URL.",
});
}
if (value.url && !/^https?:\/\//.test(value.url) && !value.url.startsWith("/")) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: ["url"],
message: "Media URL must be an absolute URL or start with /.",
});
}
});
export type MediaFieldInput = z.infer<typeof mediaFieldInputSchema>;