Files
outline/server/utils/zod.ts
Hemachandar 62ee075a6f feat: store user timezone (#7902)
* feat: store user timezone

* tz validation
2024-11-06 18:06:19 -08:00

31 lines
815 B
TypeScript

import emojiRegex from "emoji-regex";
import { z } from "zod";
import { IconLibrary } from "@shared/utils/IconLibrary";
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 zodIconType = () =>
z.union([
z.string().regex(emojiRegex()),
zodEnumFromObjectKeys(IconLibrary.mapping),
]);
export const zodTimezone = () =>
z.string().refine(
(timezone) => {
try {
Intl.DateTimeFormat(undefined, { timeZone: timezone });
return true;
} catch {
return false;
}
},
{ message: "invalid timezone" }
);