Fix filteredArray logic
This commit is contained in:
parent
d0ceac9987
commit
e9ae8d2c45
1 changed files with 7 additions and 4 deletions
|
@ -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))
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue