Skip to content

Commit c5abbc9

Browse files
authored
fix(valibot-validator): Fix response query types on RPC (#914)
Fixes #899 * fix(valibot-validator): Fix query types on RPC * Add changeset * Remove valibot issue flatenning * Add types test
1 parent d48ec05 commit c5abbc9

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

.changeset/warm-plants-thank.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hono/valibot-validator': patch
3+
---
4+
5+
Fix request query types for valibot schemas
+27-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Context, Env, Input as HonoInput, MiddlewareHandler, ValidationTargets } from 'hono'
1+
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
22
import { validator } from 'hono/validator'
33
import type {
44
GenericSchema,
@@ -9,10 +9,18 @@ import type {
99
} from 'valibot'
1010
import { safeParseAsync } from 'valibot'
1111

12-
export type Hook<T extends GenericSchema | GenericSchemaAsync, E extends Env, P extends string> = (
13-
result: SafeParseResult<T>,
12+
export type Hook<
13+
T extends GenericSchema | GenericSchemaAsync,
14+
E extends Env,
15+
P extends string,
16+
Target extends keyof ValidationTargets = keyof ValidationTargets,
17+
O = {}
18+
> = (
19+
result: SafeParseResult<T> & {
20+
target: Target
21+
},
1422
c: Context<E, P>
15-
) => Response | Promise<Response> | void | Promise<Response | void>
23+
) => Response | void | TypedResponse<O> | Promise<Response | void | TypedResponse<O>>
1624

1725
type HasUndefined<T> = undefined extends T ? true : false
1826

@@ -23,20 +31,16 @@ export const vValidator = <
2331
P extends string,
2432
In = InferInput<T>,
2533
Out = InferOutput<T>,
26-
I extends HonoInput = {
34+
I extends Input = {
2735
in: HasUndefined<In> extends true
2836
? {
29-
[K in Target]?: K extends 'json'
37+
[K in Target]?: In extends ValidationTargets[K]
3038
? In
31-
: HasUndefined<keyof ValidationTargets[K]> extends true
32-
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
33-
: { [K2 in keyof In]: ValidationTargets[K][K2] }
39+
: { [K2 in keyof In]?: ValidationTargets[K][K2] }
3440
}
3541
: {
36-
[K in Target]: K extends 'json'
42+
[K in Target]: In extends ValidationTargets[K]
3743
? In
38-
: HasUndefined<keyof ValidationTargets[K]> extends true
39-
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
4044
: { [K2 in keyof In]: ValidationTargets[K][K2] }
4145
}
4246
out: { [K in Target]: Out }
@@ -45,23 +49,28 @@ export const vValidator = <
4549
>(
4650
target: Target,
4751
schema: T,
48-
hook?: Hook<T, E, P>
52+
hook?: Hook<T, E, P, Target>
4953
): MiddlewareHandler<E, P, V> =>
5054
// @ts-expect-error not typed well
5155
validator(target, async (value, c) => {
5256
const result = await safeParseAsync(schema, value)
5357

5458
if (hook) {
55-
const hookResult = hook(result, c)
56-
if (hookResult instanceof Response || hookResult instanceof Promise) {
57-
return hookResult
59+
const hookResult = await hook({ ...result, target }, c)
60+
if (hookResult) {
61+
if (hookResult instanceof Response) {
62+
return hookResult
63+
}
64+
65+
if ('response' in hookResult) {
66+
return hookResult.response
67+
}
5868
}
5969
}
6070

6171
if (!result.success) {
6272
return c.json(result, 400)
6373
}
6474

65-
const data = result.output as InferOutput<T>
66-
return data
75+
return result.output
6776
})

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

+25
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,28 @@ describe('With Hook Async', () => {
324324
expect(res.status).toBe(400)
325325
})
326326
})
327+
328+
describe('Test types', () => {
329+
it('Should return correct types when validating a query', () => {
330+
const app = new Hono()
331+
332+
const routes = app.post(
333+
'/',
334+
vValidator(
335+
'query',
336+
object({
337+
foo: string(),
338+
})
339+
),
340+
(c) => {
341+
return c.json(c.req.valid('query'))
342+
}
343+
)
344+
345+
type T = ExtractSchema<typeof routes>
346+
347+
type Actual = T['/']['$post']['input']['query']
348+
type Expected = { foo: string }
349+
type verify = Expect<Equal<Expected, Actual>>
350+
})
351+
})

0 commit comments

Comments
 (0)