mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
12 lines
429 B
TypeScript
12 lines
429 B
TypeScript
import z from "zod";
|
|
|
|
/** Validates individual items in an array, dropping any that aren't valid. */
|
|
export function filteredArray<T extends z.ZodTypeAny>(schema: T) {
|
|
return z.any().array().catch([])
|
|
.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))
|
|
));
|
|
} |