From d703577b551c82506e94c4a9c2ae9e7d933fb695 Mon Sep 17 00:00:00 2001 From: hiraoka <62982380+y-hiraoka@users.noreply.github.com> Date: Mon, 6 May 2024 10:10:56 +0900 Subject: [PATCH] [valibot-validator] Fix Type Inference for number Types in `valibot-validator` Query (#502) * [valibot-validator] Type inference failure in RPC routes when `"query"` contains `number` type(s). #501 * add changeset file. --- .changeset/thin-fireants-work.md | 5 +++ packages/valibot-validator/package.json | 2 +- packages/valibot-validator/src/index.ts | 35 ++++++++++++------- packages/valibot-validator/test/index.test.ts | 4 ++- yarn.lock | 2 +- 5 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 .changeset/thin-fireants-work.md diff --git a/.changeset/thin-fireants-work.md b/.changeset/thin-fireants-work.md new file mode 100644 index 00000000..259b1c04 --- /dev/null +++ b/.changeset/thin-fireants-work.md @@ -0,0 +1,5 @@ +--- +'@hono/valibot-validator': patch +--- + +improved vvalidator to infer route types with number queries diff --git a/packages/valibot-validator/package.json b/packages/valibot-validator/package.json index 491026e4..58a3e11e 100644 --- a/packages/valibot-validator/package.json +++ b/packages/valibot-validator/package.json @@ -31,7 +31,7 @@ "valibot": ">=0.13.1 <1" }, "devDependencies": { - "hono": "^3.11.7", + "hono": "^4.0.10", "jest": "^29.7.0", "rimraf": "^5.0.5", "valibot": "^0.24.1" diff --git a/packages/valibot-validator/src/index.ts b/packages/valibot-validator/src/index.ts index 23c51760..234fc8f3 100644 --- a/packages/valibot-validator/src/index.ts +++ b/packages/valibot-validator/src/index.ts @@ -1,4 +1,4 @@ -import type { Context, MiddlewareHandler, Env, ValidationTargets } from 'hono' +import type { Context, MiddlewareHandler, Env, ValidationTargets, Input as HonoInput } from 'hono' import { validator } from 'hono/validator' import type { BaseSchema, Input, Output, SafeParseResult } from 'valibot' import { safeParse } from 'valibot' @@ -15,22 +15,33 @@ export const vValidator = < Target extends keyof ValidationTargets, E extends Env, P extends string, - V extends { - in: HasUndefined> extends true - ? { [K in Target]?: Input } - : { [K in Target]: Input } - out: { [K in Target]: Output } - } = { - in: HasUndefined> extends true - ? { [K in Target]?: Input } - : { [K in Target]: Input } - out: { [K in Target]: Output } - } + In = Input, + Out = Output, + I extends HonoInput = { + in: HasUndefined extends true + ? { + [K in Target]?: K extends 'json' + ? In + : HasUndefined extends true + ? { [K2 in keyof In]?: ValidationTargets[K][K2] } + : { [K2 in keyof In]: ValidationTargets[K][K2] } + } + : { + [K in Target]: K extends 'json' + ? In + : HasUndefined 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 ): MiddlewareHandler => + // @ts-expect-error not typed well validator(target, (value, c) => { const result = safeParse(schema, value) diff --git a/packages/valibot-validator/test/index.test.ts b/packages/valibot-validator/test/index.test.ts index c0073b67..a085b3d1 100644 --- a/packages/valibot-validator/test/index.test.ts +++ b/packages/valibot-validator/test/index.test.ts @@ -17,6 +17,7 @@ describe('Basic', () => { const querySchema = optional( object({ search: optional(string()), + page: optional(number()), }) ) @@ -47,7 +48,8 @@ describe('Basic', () => { } & { query?: | { - search?: string | undefined + search?: string | string[] | undefined + page?: string | string[] | undefined } | undefined } diff --git a/yarn.lock b/yarn.lock index 117f1521..96715cea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2114,7 +2114,7 @@ __metadata: version: 0.0.0-use.local resolution: "@hono/valibot-validator@workspace:packages/valibot-validator" dependencies: - hono: "npm:^3.11.7" + hono: "npm:^4.0.10" jest: "npm:^29.7.0" rimraf: "npm:^5.0.5" valibot: "npm:^0.24.1"