20 lines
566 B
TypeScript
20 lines
566 B
TypeScript
import type { ZodError } from "zod/v4";
|
|
|
|
import { ValidationErrorCollection, type ValidationErrorDetail } from "../errors";
|
|
|
|
export function translateZodValidationError<T>(
|
|
message: string,
|
|
zodError: ZodError<T>,
|
|
errorValue?: unknown
|
|
) {
|
|
const errors: ValidationErrorDetail[] = [];
|
|
for (const issue of zodError.issues) {
|
|
errors.push({
|
|
message: `${message}: ${issue.message}`,
|
|
path: issue.path ? issue.path.join(".") : undefined,
|
|
value: errorValue ?? issue.input,
|
|
});
|
|
}
|
|
return new ValidationErrorCollection(message, errors);
|
|
}
|