14 lines
533 B
TypeScript
14 lines
533 B
TypeScript
import { ApiError } from "./api";
|
|
|
|
type ValidationIssue = { path?: Array<string | number>; message?: string };
|
|
|
|
export function apiFieldErrors(caught: unknown): Record<string, string> {
|
|
if (!(caught instanceof ApiError) || !Array.isArray(caught.details)) return {};
|
|
const errors: Record<string, string> = {};
|
|
for (const issue of caught.details as ValidationIssue[]) {
|
|
const field = issue.path?.[0];
|
|
if (typeof field === "string" && issue.message && !errors[field]) errors[field] = issue.message;
|
|
}
|
|
return errors;
|
|
}
|