Fix filteredArray logic

This commit is contained in:
Alex Gleason 2023-03-13 16:37:40 -05:00
parent d0ceac9987
commit e9ae8d2c45
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -4,9 +4,12 @@ import type { CustomEmoji } from './custom-emoji';
/** Validates individual items in an array, dropping any that aren't valid. */ /** Validates individual items in an array, dropping any that aren't valid. */
function filteredArray<T extends z.ZodTypeAny>(schema: T) { function filteredArray<T extends z.ZodTypeAny>(schema: T) {
return z.any().array().transform((arr) => ( return z.any().array()
arr.map((item) => schema.safeParse(item).success ? item as z.infer<T> : undefined) .transform((arr) => (
.filter((item): item is z.infer<T> => Boolean(item)) arr.map((item) => {
const parsed = schema.safeParse(item);
return parsed.success ? parsed.data : undefined;
}).filter((item): item is z.infer<T> => Boolean(item))
)); ));
} }