Skip to content

Commit 4875e1c

Browse files
authored
feat(zod-validator): support coerce (#411)
* fix(zod-validator): support `coerce` * changeset * refactored * make it as minor
1 parent d776ada commit 4875e1c

File tree

5 files changed

+85
-23
lines changed

5 files changed

+85
-23
lines changed

.changeset/eleven-birds-beg.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hono/zod-validator': minor
3+
---
4+
5+
feat: support coerce

packages/zod-validator/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"zod": "^3.19.1"
3232
},
3333
"devDependencies": {
34-
"hono": "^3.11.7",
34+
"hono": "^4.0.10",
3535
"jest": "^29.7.0",
3636
"rimraf": "^5.0.5",
3737
"typescript": "^5.3.3",
38-
"zod": "3.19.1"
38+
"zod": "^3.22.4"
3939
}
4040
}

packages/zod-validator/src/index.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono'
1+
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse, Input } from 'hono'
22
import { validator } from 'hono/validator'
33
import type { z, ZodSchema, ZodError } from 'zod'
44

@@ -14,20 +14,33 @@ export const zValidator = <
1414
Target extends keyof ValidationTargets,
1515
E extends Env,
1616
P extends string,
17-
I = z.input<T>,
18-
O = z.output<T>,
19-
V extends {
20-
in: HasUndefined<I> extends true ? { [K in Target]?: I } : { [K in Target]: I }
21-
out: { [K in Target]: O }
22-
} = {
23-
in: HasUndefined<I> extends true ? { [K in Target]?: I } : { [K in Target]: I }
24-
out: { [K in Target]: O }
25-
}
17+
In = z.input<T>,
18+
Out = z.output<T>,
19+
I extends Input = {
20+
in: HasUndefined<In> extends true
21+
? {
22+
[K in Target]?: K extends 'json'
23+
? In
24+
: HasUndefined<keyof ValidationTargets[K]> extends true
25+
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
26+
: { [K2 in keyof In]: ValidationTargets[K][K2] }
27+
}
28+
: {
29+
[K in Target]: K extends 'json'
30+
? In
31+
: HasUndefined<keyof ValidationTargets[K]> extends true
32+
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
33+
: { [K2 in keyof In]: ValidationTargets[K][K2] }
34+
}
35+
out: { [K in Target]: Out }
36+
},
37+
V extends I = I
2638
>(
2739
target: Target,
2840
schema: T,
2941
hook?: Hook<z.infer<T>, E, P>
3042
): MiddlewareHandler<E, P, V> =>
43+
// @ts-expect-error not typed well
3144
validator(target, async (value, c) => {
3245
const result = await schema.safeParseAsync(value)
3346

packages/zod-validator/test/index.test.ts

+46-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Basic', () => {
2828
const data = c.req.valid('json')
2929
const query = c.req.valid('query')
3030

31-
return c.jsonT({
31+
return c.json({
3232
success: true,
3333
message: `${data.name} is ${data.age}`,
3434
queryName: query?.name,
@@ -48,7 +48,7 @@ describe('Basic', () => {
4848
} & {
4949
query?:
5050
| {
51-
name?: string | undefined
51+
name?: string | string[] | undefined
5252
}
5353
| undefined
5454
}
@@ -92,6 +92,9 @@ describe('Basic', () => {
9292
age: '20',
9393
}),
9494
method: 'POST',
95+
headers: {
96+
'content-type': 'application/json',
97+
},
9598
})
9699
const res = await app.request(req)
97100
expect(res).not.toBeNull()
@@ -101,6 +104,47 @@ describe('Basic', () => {
101104
})
102105
})
103106

107+
describe('coerce', () => {
108+
const app = new Hono()
109+
110+
const querySchema = z.object({
111+
page: z.coerce.number(),
112+
})
113+
114+
const route = app.get('/page', zValidator('query', querySchema), (c) => {
115+
const { page } = c.req.valid('query')
116+
return c.json({ page })
117+
})
118+
119+
type Actual = ExtractSchema<typeof route>
120+
type Expected = {
121+
'/page': {
122+
$get: {
123+
input: {
124+
query: {
125+
page: string | string[]
126+
}
127+
}
128+
output: {
129+
page: number
130+
}
131+
}
132+
}
133+
}
134+
135+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
136+
type verify = Expect<Equal<Expected, Actual>>
137+
138+
it('Should return 200 response', async () => {
139+
const res = await app.request('/page?page=123')
140+
expect(res).not.toBeNull()
141+
expect(res.status).toBe(200)
142+
expect(await res.json()).toEqual({
143+
page: 123,
144+
})
145+
})
146+
})
147+
104148
describe('With Hook', () => {
105149
const app = new Hono()
106150

yarn.lock

+9-9
Original file line numberDiff line numberDiff line change
@@ -1748,11 +1748,11 @@ __metadata:
17481748
version: 0.0.0-use.local
17491749
resolution: "@hono/zod-validator@workspace:packages/zod-validator"
17501750
dependencies:
1751-
hono: "npm:^3.11.7"
1751+
hono: "npm:^4.0.10"
17521752
jest: "npm:^29.7.0"
17531753
rimraf: "npm:^5.0.5"
17541754
typescript: "npm:^5.3.3"
1755-
zod: "npm:3.19.1"
1755+
zod: "npm:^3.22.4"
17561756
peerDependencies:
17571757
hono: ">=3.9.0"
17581758
zod: ^3.19.1
@@ -8917,6 +8917,13 @@ __metadata:
89178917
languageName: node
89188918
linkType: hard
89198919

8920+
"hono@npm:^4.0.10":
8921+
version: 4.0.10
8922+
resolution: "hono@npm:4.0.10"
8923+
checksum: a68deed2a216dd956e6012a834312a09ffcf18a8e61b851ec6b168ad5cf13d9696f7fa3dce25286b4b2e92f6ea7102ece8f097f423ff385236f7b83c3a68032c
8924+
languageName: node
8925+
linkType: hard
8926+
89208927
"hono@npm:^4.0.2":
89218928
version: 4.0.2
89228929
resolution: "hono@npm:4.0.2"
@@ -18216,13 +18223,6 @@ __metadata:
1821618223
languageName: node
1821718224
linkType: hard
1821818225

18219-
"zod@npm:3.19.1":
18220-
version: 3.19.1
18221-
resolution: "zod@npm:3.19.1"
18222-
checksum: e08197793f26916f8abea40687fc968b2de0471049b29b7ff25825a9f28ba24205d1c5b8ad26df17538b051928192f9ef8f9ef3132aece9cb56f0830a7450c26
18223-
languageName: node
18224-
linkType: hard
18225-
1822618226
"zod@npm:^3.20.2, zod@npm:^3.20.6, zod@npm:^3.22.1, zod@npm:^3.22.4":
1822718227
version: 3.22.4
1822818228
resolution: "zod@npm:3.22.4"

0 commit comments

Comments
 (0)