feat: parseAcfMedia
This commit is contained in:
29
wp-content/themes/moonshine/app/utils/acf-media.ts
Normal file
29
wp-content/themes/moonshine/app/utils/acf-media.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import type { AcfMediaFragment } from "#graphql/operations";
|
||||
import * as z from "zod";
|
||||
|
||||
export const acfImageSchema = z.object({
|
||||
src: z.url(),
|
||||
alt: z.string(),
|
||||
mediaDetails: z.object({
|
||||
width: z.number(),
|
||||
height: z.number(),
|
||||
}),
|
||||
objectPosition: z.string().optional().default("center"),
|
||||
});
|
||||
|
||||
export const acfMediaSchema = z.object({
|
||||
image: z.object({
|
||||
node: acfImageSchema,
|
||||
}),
|
||||
aspectRatio: z.enum(["square", "video", "portrait", "auto"]).optional().default("auto"),
|
||||
objectFit: z.enum(["cover", "contain"]).optional().default("cover"),
|
||||
});
|
||||
|
||||
export function parseAcfMedia(data?: Partial<AcfMediaFragment>) {
|
||||
try {
|
||||
return acfMediaSchema.parse(data);
|
||||
}
|
||||
catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user