diff --git a/app/soapbox/utils/normalizers.ts b/app/soapbox/utils/normalizers.ts index 6604f1caa..64638dd29 100644 --- a/app/soapbox/utils/normalizers.ts +++ b/app/soapbox/utils/normalizers.ts @@ -1,3 +1,5 @@ +import z from 'zod'; + /** Use new value only if old value is undefined */ export const mergeDefined = (oldVal: any, newVal: any) => oldVal === undefined ? newVal : oldVal; @@ -10,3 +12,18 @@ export const makeEmojiMap = (emojis: any) => emojis.reduce((obj: any, emoji: any export const normalizeId = (id: any): string | null => { return typeof id === 'string' ? id : null; }; + +export type Normalizer = (value: V) => R; + +/** + * Allows using any legacy normalizer function as a zod schema. + * + * @example + * ```ts + * const statusSchema = toSchema(normalizeStatus); + * statusSchema.parse(status); + * ``` + */ +export const toSchema = (normalizer: Normalizer) => { + return z.custom().transform(normalizer); +}; \ No newline at end of file