重构
This commit is contained in:
13
src/utils/validate.ts
Normal file
13
src/utils/validate.ts
Normal 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);
|
||||
Reference in New Issue
Block a user