From c6a16ab7aa8fba2403d4294e7673f96796020c65 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sat, 19 Jul 2025 10:20:40 +0900 Subject: [PATCH] fix(zod-validator): support transform (#1315) * fix(zod-validator): support transform * polish --- .changeset/mean-rice-spend.md | 5 ++++ packages/zod-validator/src/index.ts | 2 +- packages/zod-validator/src/v4.test.ts | 36 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .changeset/mean-rice-spend.md diff --git a/.changeset/mean-rice-spend.md b/.changeset/mean-rice-spend.md new file mode 100644 index 00000000..1097c238 --- /dev/null +++ b/.changeset/mean-rice-spend.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-validator': patch +--- + +fix: support transform diff --git a/packages/zod-validator/src/index.ts b/packages/zod-validator/src/index.ts index 4689e6d8..90d29a4a 100644 --- a/packages/zod-validator/src/index.ts +++ b/packages/zod-validator/src/index.ts @@ -73,7 +73,7 @@ export const zValidator = < if ((target === 'header' && '_def' in schema) || (target === 'header' && '_zod' in schema)) { // create an object that maps lowercase schema keys to lowercase // @ts-expect-error the schema is a Zod Schema - const schemaKeys = Object.keys(schema.shape) + const schemaKeys = Object.keys('in' in schema ? schema.in.shape : schema.shape) const caseInsensitiveKeymap = Object.fromEntries( schemaKeys.map((key) => [key.toLowerCase(), key]) ) diff --git a/packages/zod-validator/src/v4.test.ts b/packages/zod-validator/src/v4.test.ts index 884e6410..1a0fdff7 100644 --- a/packages/zod-validator/src/v4.test.ts +++ b/packages/zod-validator/src/v4.test.ts @@ -464,3 +464,39 @@ describe('With options + validationFunction', () => { }) }) }) + +describe('Transform', () => { + const schema = z + .object({ + 'user-agent': z.string(), + }) + .transform((data) => ({ + userAgent: data['user-agent'], + })) + + const zValidatorHeader = zValidator('header', schema) + + const app = new Hono() + + app.post('/test', zValidatorHeader, async (c) => { + const header = c.req.valid('header') + return c.json(header) + }) + + it('Should return 400 response', async () => { + const res = await app.request('/test', { + method: 'POST', + }) + expect(res.status).toBe(400) + }) + + it('Should return 200 response', async () => { + const res = await app.request('/test', { + method: 'POST', + headers: { + 'user-agent': 'my-agent', + }, + }) + expect(res.status).toBe(200) + }) +})