Array of issue records; each must have a path array.
The value that was validated — used as the root for getPath.
A new array of issues, each augmented with an actual field.
const issues = [
{ path: ["user", "age"], code: "invalid_type", message: "Expected number" },
];
const obj = { user: { age: "forty" } };
zodAddActualValueToIssues(issues, obj);
// [
// {
// path: ["user", "age"],
// code: "invalid_type",
// message: "Expected number",
// actual: "forty",
// },
// ]
// Missing path → `actual` is undefined
zodAddActualValueToIssues([{ path: ["missing"] }], {});
// [{ path: ["missing"], actual: undefined }]
export function zodAddActualValueToIssues<T extends PathedIssue>(
issues: readonly T[],
obj: unknown,
): Array<T & { actual: unknown }> {
return issues.map((issue) => ({
...issue,
actual: getPath(obj, issue.path),
}));
}
Enriches a list of zod issues (or any
{ path }-shaped records) with the actual value found at each issue's path in the source object. Useful when logging validation failures — stock zod issues only say "expected X at path Y", and knowing what was there makes debugging drastically faster.The original issue objects are spread shallowly, so extra zod fields (
code,message,expected, etc.) pass through untouched; only the newactualkey is added.