ChemPal Documentation - v0.0.13-beta.5
    Preparing search index...

    Function zodAddActualValueToIssues

    • 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 new actual key is added.

      Type Parameters

      Parameters

      • issues: readonly T[]

        Array of issue records; each must have a path array.

      • obj: unknown

        The value that was validated — used as the root for getPath.

      Returns (T & { actual: unknown })[]

      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),
      }));
      }