This commit is contained in:
2026-01-14 16:57:35 +08:00
parent 804baa64b2
commit ec265be26b
38 changed files with 585 additions and 294 deletions

13
src/utils/validate.ts Normal file
View File

@@ -0,0 +1,13 @@
import { ValidateError } from "@/lib/errors";
import z from "zod";
export const validate = <T, U extends z.ZodType>(dto: T, schema: U) => {
const result = schema.safeParse(dto);
if (result.success) return result.data as z.infer<U>;
const errorMessages = result.error.issues.map((issue) =>
`${issue.path.join('.')}: ${issue.message}`
).join('; ');
throw new ValidateError(`Validation failed: ${errorMessages}`);
};
export const generateValidator = <T extends z.ZodType>(schema: T) => <T>(dto: T) => validate(dto, schema);