Skip to content

Commit

Permalink
feat(zod-validator): support coerce (#411)
Browse files Browse the repository at this point in the history
* fix(zod-validator): support `coerce`

* changeset

* refactored

* make it as minor
  • Loading branch information
yusukebe authored Mar 6, 2024
1 parent d776ada commit 4875e1c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-birds-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-validator': minor
---

feat: support coerce
4 changes: 2 additions & 2 deletions packages/zod-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
"zod": "^3.19.1"
},
"devDependencies": {
"hono": "^3.11.7",
"hono": "^4.0.10",
"jest": "^29.7.0",
"rimraf": "^5.0.5",
"typescript": "^5.3.3",
"zod": "3.19.1"
"zod": "^3.22.4"
}
}
33 changes: 23 additions & 10 deletions packages/zod-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse } from 'hono'
import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse, Input } from 'hono'
import { validator } from 'hono/validator'
import type { z, ZodSchema, ZodError } from 'zod'

Expand All @@ -14,20 +14,33 @@ export const zValidator = <
Target extends keyof ValidationTargets,
E extends Env,
P extends string,
I = z.input<T>,
O = z.output<T>,
V extends {
in: HasUndefined<I> extends true ? { [K in Target]?: I } : { [K in Target]: I }
out: { [K in Target]: O }
} = {
in: HasUndefined<I> extends true ? { [K in Target]?: I } : { [K in Target]: I }
out: { [K in Target]: O }
}
In = z.input<T>,
Out = z.output<T>,
I extends Input = {
in: HasUndefined<In> extends true
? {
[K in Target]?: K extends 'json'
? In
: HasUndefined<keyof ValidationTargets[K]> extends true
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
: { [K2 in keyof In]: ValidationTargets[K][K2] }
}
: {
[K in Target]: K extends 'json'
? In
: HasUndefined<keyof ValidationTargets[K]> extends true
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
: { [K2 in keyof In]: ValidationTargets[K][K2] }
}
out: { [K in Target]: Out }
},
V extends I = I
>(
target: Target,
schema: T,
hook?: Hook<z.infer<T>, E, P>
): MiddlewareHandler<E, P, V> =>
// @ts-expect-error not typed well
validator(target, async (value, c) => {
const result = await schema.safeParseAsync(value)

Expand Down
48 changes: 46 additions & 2 deletions packages/zod-validator/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Basic', () => {
const data = c.req.valid('json')
const query = c.req.valid('query')

return c.jsonT({
return c.json({
success: true,
message: `${data.name} is ${data.age}`,
queryName: query?.name,
Expand All @@ -48,7 +48,7 @@ describe('Basic', () => {
} & {
query?:
| {
name?: string | undefined
name?: string | string[] | undefined
}
| undefined
}
Expand Down Expand Up @@ -92,6 +92,9 @@ describe('Basic', () => {
age: '20',
}),
method: 'POST',
headers: {
'content-type': 'application/json',
},
})
const res = await app.request(req)
expect(res).not.toBeNull()
Expand All @@ -101,6 +104,47 @@ describe('Basic', () => {
})
})

describe('coerce', () => {
const app = new Hono()

const querySchema = z.object({
page: z.coerce.number(),
})

const route = app.get('/page', zValidator('query', querySchema), (c) => {
const { page } = c.req.valid('query')
return c.json({ page })
})

type Actual = ExtractSchema<typeof route>
type Expected = {
'/page': {
$get: {
input: {
query: {
page: string | string[]
}
}
output: {
page: number
}
}
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type verify = Expect<Equal<Expected, Actual>>

it('Should return 200 response', async () => {
const res = await app.request('/page?page=123')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
page: 123,
})
})
})

describe('With Hook', () => {
const app = new Hono()

Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1748,11 +1748,11 @@ __metadata:
version: 0.0.0-use.local
resolution: "@hono/zod-validator@workspace:packages/zod-validator"
dependencies:
hono: "npm:^3.11.7"
hono: "npm:^4.0.10"
jest: "npm:^29.7.0"
rimraf: "npm:^5.0.5"
typescript: "npm:^5.3.3"
zod: "npm:3.19.1"
zod: "npm:^3.22.4"
peerDependencies:
hono: ">=3.9.0"
zod: ^3.19.1
Expand Down Expand Up @@ -8917,6 +8917,13 @@ __metadata:
languageName: node
linkType: hard

"hono@npm:^4.0.10":
version: 4.0.10
resolution: "hono@npm:4.0.10"
checksum: a68deed2a216dd956e6012a834312a09ffcf18a8e61b851ec6b168ad5cf13d9696f7fa3dce25286b4b2e92f6ea7102ece8f097f423ff385236f7b83c3a68032c
languageName: node
linkType: hard

"hono@npm:^4.0.2":
version: 4.0.2
resolution: "hono@npm:4.0.2"
Expand Down Expand Up @@ -18216,13 +18223,6 @@ __metadata:
languageName: node
linkType: hard

"zod@npm:3.19.1":
version: 3.19.1
resolution: "zod@npm:3.19.1"
checksum: e08197793f26916f8abea40687fc968b2de0471049b29b7ff25825a9f28ba24205d1c5b8ad26df17538b051928192f9ef8f9ef3132aece9cb56f0830a7450c26
languageName: node
linkType: hard

"zod@npm:^3.20.2, zod@npm:^3.20.6, zod@npm:^3.22.1, zod@npm:^3.22.4":
version: 3.22.4
resolution: "zod@npm:3.22.4"
Expand Down

0 comments on commit 4875e1c

Please sign in to comment.