@@ -0,0 +1,42 @@
|
||||
import { MediaKind } from "@prisma/client";
|
||||
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>;
|
||||
Reference in New Issue
Block a user