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
pull/927/head
Álvaro Ortiz 2025-01-08 05:48:44 -05:00 committed by GitHub
parent d48ec05519
commit c5abbc993d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 18 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/valibot-validator': patch
---
Fix request query types for valibot schemas

View File

@ -1,4 +1,4 @@
import type { Context, Env, Input as HonoInput, MiddlewareHandler, ValidationTargets } from 'hono' import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
import { validator } from 'hono/validator' import { validator } from 'hono/validator'
import type { import type {
GenericSchema, GenericSchema,
@ -9,10 +9,18 @@ import type {
} from 'valibot' } from 'valibot'
import { safeParseAsync } from 'valibot' import { safeParseAsync } from 'valibot'
export type Hook<T extends GenericSchema | GenericSchemaAsync, E extends Env, P extends string> = ( export type Hook<
result: SafeParseResult<T>, T extends GenericSchema | GenericSchemaAsync,
E extends Env,
P extends string,
Target extends keyof ValidationTargets = keyof ValidationTargets,
O = {}
> = (
result: SafeParseResult<T> & {
target: Target
},
c: Context<E, P> c: Context<E, P>
) => Response | Promise<Response> | void | Promise<Response | void> ) => Response | void | TypedResponse<O> | Promise<Response | void | TypedResponse<O>>
type HasUndefined<T> = undefined extends T ? true : false type HasUndefined<T> = undefined extends T ? true : false
@ -23,20 +31,16 @@ export const vValidator = <
P extends string, P extends string,
In = InferInput<T>, In = InferInput<T>,
Out = InferOutput<T>, Out = InferOutput<T>,
I extends HonoInput = { I extends Input = {
in: HasUndefined<In> extends true in: HasUndefined<In> extends true
? { ? {
[K in Target]?: K extends 'json' [K in Target]?: In extends ValidationTargets[K]
? In ? In
: HasUndefined<keyof ValidationTargets[K]> extends true : { [K2 in keyof In]?: ValidationTargets[K][K2] }
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
: { [K2 in keyof In]: ValidationTargets[K][K2] }
} }
: { : {
[K in Target]: K extends 'json' [K in Target]: In extends ValidationTargets[K]
? In ? In
: HasUndefined<keyof ValidationTargets[K]> extends true
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
: { [K2 in keyof In]: ValidationTargets[K][K2] } : { [K2 in keyof In]: ValidationTargets[K][K2] }
} }
out: { [K in Target]: Out } out: { [K in Target]: Out }
@ -45,23 +49,28 @@ export const vValidator = <
>( >(
target: Target, target: Target,
schema: T, schema: T,
hook?: Hook<T, E, P> hook?: Hook<T, E, P, Target>
): MiddlewareHandler<E, P, V> => ): MiddlewareHandler<E, P, V> =>
// @ts-expect-error not typed well // @ts-expect-error not typed well
validator(target, async (value, c) => { validator(target, async (value, c) => {
const result = await safeParseAsync(schema, value) const result = await safeParseAsync(schema, value)
if (hook) { if (hook) {
const hookResult = hook(result, c) const hookResult = await hook({ ...result, target }, c)
if (hookResult instanceof Response || hookResult instanceof Promise) { if (hookResult) {
if (hookResult instanceof Response) {
return hookResult return hookResult
} }
if ('response' in hookResult) {
return hookResult.response
}
}
} }
if (!result.success) { if (!result.success) {
return c.json(result, 400) return c.json(result, 400)
} }
const data = result.output as InferOutput<T> return result.output
return data
}) })

View File

@ -324,3 +324,28 @@ describe('With Hook Async', () => {
expect(res.status).toBe(400) expect(res.status).toBe(400)
}) })
}) })
describe('Test types', () => {
it('Should return correct types when validating a query', () => {
const app = new Hono()
const routes = app.post(
'/',
vValidator(
'query',
object({
foo: string(),
})
),
(c) => {
return c.json(c.req.valid('query'))
}
)
type T = ExtractSchema<typeof routes>
type Actual = T['/']['$post']['input']['query']
type Expected = { foo: string }
type verify = Expect<Equal<Expected, Actual>>
})
})