bigbuffet-rw/app/soapbox/schemas/utils.ts

33 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-10 10:42:49 -08:00
import z from 'zod';
import type { CustomEmoji } from './custom-emoji';
2023-05-04 09:42:20 -07:00
/** Ensure HTML content is a string, and drop empty `<p>` tags. */
const contentSchema = z.string().catch('').transform((value) => value === '<p></p>' ? '' : value);
2023-05-04 10:13:39 -07:00
/** Validate to Mastodon's date format, or use the current date. */
const dateSchema = z.string().datetime().catch(new Date().toUTCString());
2023-03-10 10:42:49 -08:00
/** Validates individual items in an array, dropping any that aren't valid. */
function filteredArray<T extends z.ZodTypeAny>(schema: T) {
2023-05-02 16:30:21 -07:00
return z.any().array().catch([])
2023-03-13 14:37:40 -07:00
.transform((arr) => (
arr.map((item) => {
const parsed = schema.safeParse(item);
return parsed.success ? parsed.data : undefined;
}).filter((item): item is z.infer<T> => Boolean(item))
));
2023-03-10 10:42:49 -08:00
}
2023-05-04 08:24:34 -07:00
/** Validates the string as an emoji. */
const emojiSchema = z.string().refine((v) => /\p{Extended_Pictographic}/u.test(v));
2023-03-10 10:42:49 -08:00
/** Map a list of CustomEmoji to their shortcodes. */
function makeCustomEmojiMap(customEmojis: CustomEmoji[]) {
return customEmojis.reduce<Record<string, CustomEmoji>>((result, emoji) => {
result[`:${emoji.shortcode}:`] = emoji;
return result;
}, {});
}
2023-05-04 10:13:39 -07:00
export { filteredArray, makeCustomEmojiMap, emojiSchema, contentSchema, dateSchema };