Repository: unjs/h3
Pull request #594 (2023-12-07 10:51:15)

feat(validate): provide validate error in `data`
<!---
☝️ PR title should follow conventional commits (https://conventionalcommits.org) 
-->

### 🔗 Linked issue

<!-- Please ensure there is an open issue and mention its number as #123 -->

### ❓ Type of change

<!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply. -->

- [x] 👌 Enhancement (improving an existing functionality like performance)

### 📚 Description

<!-- Describe your changes in detail -->
<!-- Why is this change required? What problem does it solve? -->
<!-- If it resolves an open issue, please link to the issue here. For example "Resolves #1337" -->

We were not forwarding the error from zod to the `data` property, making it impossible to get back the errors/issues from it.

Before:
![CleanShot 2023-12-07 at 11 50 19](https://github.com/unjs/h3/assets/904724/435d21e2-ea9d-4c94-9312-2dab2e914962)

After:
![CleanShot 2023-12-07 at 11 50 41](https://github.com/unjs/h3/assets/904724/72a0f296-fc65-47ff-8de9-b61788a48769)


Atinux: I guess we could also improve the error message but might be a breaking change so I kept it this way.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/unjs/h3/pull/594?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`afc4183`)](https://app.codecov.io/gh/unjs/h3/commit/afc4183b513d517bbe83449f1a3dd5291f230e6c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs) 84.43% compared to head [(`c584e6a`)](https://app.codecov.io/gh/unjs/h3/pull/594?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs) 84.44%.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #594      +/-   ##
==========================================
+ Coverage   84.43%   84.44%   +0.01%     
==========================================
  Files          32       32              
  Lines        3752     3755       +3     
  Branches      559      559              
==========================================
+ Hits         3168     3171       +3     
  Misses        584      584              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/unjs/h3/pull/594?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs).


Patch:
diff --git a/src/utils/internal/validate.ts b/src/utils/internal/validate.ts
index 32e581a..13417bf 100644
--- a/src/utils/internal/validate.ts
+++ b/src/utils/internal/validate.ts
@@ -38,7 +38,8 @@ export async function validateData<T>(
 function createValidationError(validateError?: any) {
   throw createError({
     status: 400,
-    message: validateError.message || "Validation Failed",
-    ...validateError,
+    statusMessage: "Validation Error",
+    message: validateError?.message || "Validation Error",
+    data: validateError,
   });
 }


Test patch:
diff --git a/test/validate.test.ts b/test/validate.test.ts
index b7ae8f8..6afbc84 100644
--- a/test/validate.test.ts
+++ b/test/validate.test.ts
@@ -92,6 +92,7 @@ describe("Validate", () => {
       it("Invalid", async () => {
         const res = await request.post("/zod").send({ invalid: true });
         expect(res.status).toEqual(400);
+        expect(res.body.data?.issues?.[0]?.code).toEqual("invalid_type");
       });
     });
   });