Files
outline/server/utils/zod.ts
Tom Moor 6f3534c713 feat: Custom emoji reactions (#10805)
* Claude first pass

* Move custom emojis first in search results

* refactor

* fix: Remove extra load emoji call
2025-12-05 18:47:12 -05:00

47 lines
1.3 KiB
TypeScript

import emojiRegex from "emoji-regex";
import { z } from "zod";
import { IconLibrary } from "@shared/utils/IconLibrary";
import { UrlHelper } from "@shared/utils/UrlHelper";
export function zodEnumFromObjectKeys<
TI extends Record<string, any>,
R extends string = TI extends Record<infer R, any> ? R : never,
>(input: TI): z.ZodEnum<[R, ...R[]]> {
const [firstKey, ...otherKeys] = Object.keys(input) as [R, ...R[]];
return z.enum([firstKey, ...otherKeys]);
}
export const zodIdType = () =>
z.union([z.string().regex(UrlHelper.SLUG_URL_REGEX), z.string().uuid()], {
message: "Must be a valid UUID or slug",
});
export const zodIconType = () =>
z.union([
z.string().regex(emojiRegex()),
zodEnumFromObjectKeys(IconLibrary.mapping),
z.string().uuid(),
]);
export const zodEmojiType = () =>
z.union([z.string().regex(emojiRegex()), z.string().uuid()]);
export const zodShareIdType = () =>
z.union([
z.string().uuid(),
z.string().regex(UrlHelper.SHARE_URL_SLUG_REGEX),
]);
export const zodTimezone = () =>
z.string().refine(
(timezone) => {
try {
Intl.DateTimeFormat(undefined, { timeZone: timezone });
return true;
} catch {
return false;
}
},
{ message: "invalid timezone" }
);