18 lines
401 B
TypeScript
18 lines
401 B
TypeScript
import { ZodSchema, z } from "zod";
|
|
|
|
export function parseBody<T>(
|
|
schema: ZodSchema<T>,
|
|
body: unknown,
|
|
): { data: T } | { error: string } {
|
|
try {
|
|
return { data: schema.parse(body) };
|
|
} catch (e) {
|
|
if (e instanceof z.ZodError) {
|
|
return {
|
|
error: e.issues.map((err: z.ZodIssue) => err.message).join(", "),
|
|
};
|
|
}
|
|
return { error: "Neplatný požadavek" };
|
|
}
|
|
}
|