diff --git a/src/ambassadors/images.ts b/src/ambassadors/images.ts index 530eb74..f109d62 100644 --- a/src/ambassadors/images.ts +++ b/src/ambassadors/images.ts @@ -1,3 +1,5 @@ +import { z } from "zod"; + import { isAmbassadorKey, type Ambassadors, type AmbassadorKey } from "./core"; import { isAmbassadorWithPlushKey, @@ -220,13 +222,30 @@ import winnieTheMooImageIcon from "../../assets/ambassadors/winnieTheMoo/icon.pn type OneToNine = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; type ZeroToNine = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; -type Percentage = `${OneToNine}${ZeroToNine}%` | `${ZeroToNine}%` | "100%"; - -export type AmbassadorImage = { - src: typeof stompyImage1; - alt: string; - position?: `${Percentage} ${Percentage}`; +type Percentage = `${ZeroToNine}%` | `${OneToNine}${ZeroToNine}%` | "100%"; +type Position = `${Percentage} ${Percentage}`; + +const isPercentage = (str: string): str is Percentage => + /^(100|[1-9]?[0-9])%$/.test(str); +const isPosition = (str: string): str is Position => { + const [x, y, ...rest] = str.split(" "); + return !x || !y || rest.length > 0 || !isPercentage(x) || !isPercentage(y); }; + +export const ambassadorImageSchema = z.object({ + // Use an always true refine to narrow down the type + // Ensure the image import type includes jpg + png + src: z + .unknown() + .refine( + (src: unknown): src is typeof abbottImage1 | typeof abbottImageIcon => + true, + ), + alt: z.string(), + position: z.string().refine(isPosition).optional(), +}); + +export type AmbassadorImage = z.infer; export type AmbassadorImages = [AmbassadorImage, ...AmbassadorImage[]]; const ambassadorImages: {