From b53e45fef43016d66d82ce5f09acae10987c8f18 Mon Sep 17 00:00:00 2001 From: JianJroh Date: Mon, 30 Sep 2024 12:19:35 +0800 Subject: [PATCH 1/2] feat(validate): provide validate error in `cause` --- src/utils/internal/validate.ts | 1 + test/validate.test.ts | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/utils/internal/validate.ts b/src/utils/internal/validate.ts index 011754e0..a4f6c784 100644 --- a/src/utils/internal/validate.ts +++ b/src/utils/internal/validate.ts @@ -36,5 +36,6 @@ function createValidationError(validateError?: any) { statusMessage: "Validation Error", message: validateError?.message || "Validation Error", data: validateError, + cause: validateError, }); } diff --git a/test/validate.test.ts b/test/validate.test.ts index 82ede41e..e4380f0a 100644 --- a/test/validate.test.ts +++ b/test/validate.test.ts @@ -1,7 +1,7 @@ import type { ValidateFunction } from "../src/types"; import { beforeEach } from "vitest"; -import { z } from "zod"; -import { readValidatedBody, getValidatedQuery } from "../src"; +import { z, ZodError } from "zod"; +import { readValidatedBody, getValidatedQuery, isError } from "../src"; import { describeMatrix } from "./_setup"; describeMatrix("validate", (t, { it, describe, expect }) => { @@ -36,6 +36,16 @@ describeMatrix("validate", (t, { it, describe, expect }) => { const data = await readValidatedBody(event, zodValidate); return data; }); + + t.app.post("/zod-caught", async (event) => { + try { + await readValidatedBody(event, zodValidate) + } catch (error_) { + if(isError(error_) && error_.cause instanceof ZodError) { + return true + } + } + }); }); describe("custom validator", () => { @@ -99,6 +109,14 @@ describeMatrix("validate", (t, { it, describe, expect }) => { "invalid_type", ); }); + + it("Caught", async () => { + const res = await t.fetch("/zod-caught", { + method: "POST", + body: JSON.stringify({ invalid: true }), + }); + expect(await res.json()).toEqual(true);; + }); }); }); From 094449c26644fef77b607e58cf07aa26ecc69475 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 17:49:09 +0000 Subject: [PATCH 2/2] chore: apply automated updates --- test/validate.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/validate.test.ts b/test/validate.test.ts index e4380f0a..00c2955a 100644 --- a/test/validate.test.ts +++ b/test/validate.test.ts @@ -39,10 +39,10 @@ describeMatrix("validate", (t, { it, describe, expect }) => { t.app.post("/zod-caught", async (event) => { try { - await readValidatedBody(event, zodValidate) + await readValidatedBody(event, zodValidate); } catch (error_) { - if(isError(error_) && error_.cause instanceof ZodError) { - return true + if (isError(error_) && error_.cause instanceof ZodError) { + return true; } } }); @@ -115,7 +115,7 @@ describeMatrix("validate", (t, { it, describe, expect }) => { method: "POST", body: JSON.stringify({ invalid: true }), }); - expect(await res.json()).toEqual(true);; + expect(await res.json()).toEqual(true); }); }); });