[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.pull/504/head
parent
3e96b5e128
commit
d703577b55
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/valibot-validator': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
improved vvalidator to infer route types with number queries
|
|
@ -31,7 +31,7 @@
|
||||||
"valibot": ">=0.13.1 <1"
|
"valibot": ">=0.13.1 <1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"hono": "^3.11.7",
|
"hono": "^4.0.10",
|
||||||
"jest": "^29.7.0",
|
"jest": "^29.7.0",
|
||||||
"rimraf": "^5.0.5",
|
"rimraf": "^5.0.5",
|
||||||
"valibot": "^0.24.1"
|
"valibot": "^0.24.1"
|
||||||
|
|
|
@ -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 { validator } from 'hono/validator'
|
||||||
import type { BaseSchema, Input, Output, SafeParseResult } from 'valibot'
|
import type { BaseSchema, Input, Output, SafeParseResult } from 'valibot'
|
||||||
import { safeParse } from 'valibot'
|
import { safeParse } from 'valibot'
|
||||||
|
@ -15,22 +15,33 @@ export const vValidator = <
|
||||||
Target extends keyof ValidationTargets,
|
Target extends keyof ValidationTargets,
|
||||||
E extends Env,
|
E extends Env,
|
||||||
P extends string,
|
P extends string,
|
||||||
V extends {
|
In = Input<T>,
|
||||||
in: HasUndefined<Input<T>> extends true
|
Out = Output<T>,
|
||||||
? { [K in Target]?: Input<T> }
|
I extends HonoInput = {
|
||||||
: { [K in Target]: Input<T> }
|
in: HasUndefined<In> extends true
|
||||||
out: { [K in Target]: Output<T> }
|
? {
|
||||||
} = {
|
[K in Target]?: K extends 'json'
|
||||||
in: HasUndefined<Input<T>> extends true
|
? In
|
||||||
? { [K in Target]?: Input<T> }
|
: HasUndefined<keyof ValidationTargets[K]> extends true
|
||||||
: { [K in Target]: Input<T> }
|
? { [K2 in keyof In]?: ValidationTargets[K][K2] }
|
||||||
out: { [K in Target]: Output<T> }
|
: { [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,
|
target: Target,
|
||||||
schema: T,
|
schema: T,
|
||||||
hook?: Hook<T, E, P>
|
hook?: Hook<T, E, P>
|
||||||
): MiddlewareHandler<E, P, V> =>
|
): MiddlewareHandler<E, P, V> =>
|
||||||
|
// @ts-expect-error not typed well
|
||||||
validator(target, (value, c) => {
|
validator(target, (value, c) => {
|
||||||
const result = safeParse(schema, value)
|
const result = safeParse(schema, value)
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ describe('Basic', () => {
|
||||||
const querySchema = optional(
|
const querySchema = optional(
|
||||||
object({
|
object({
|
||||||
search: optional(string()),
|
search: optional(string()),
|
||||||
|
page: optional(number()),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,7 +48,8 @@ describe('Basic', () => {
|
||||||
} & {
|
} & {
|
||||||
query?:
|
query?:
|
||||||
| {
|
| {
|
||||||
search?: string | undefined
|
search?: string | string[] | undefined
|
||||||
|
page?: string | string[] | undefined
|
||||||
}
|
}
|
||||||
| undefined
|
| undefined
|
||||||
}
|
}
|
||||||
|
|
|
@ -2114,7 +2114,7 @@ __metadata:
|
||||||
version: 0.0.0-use.local
|
version: 0.0.0-use.local
|
||||||
resolution: "@hono/valibot-validator@workspace:packages/valibot-validator"
|
resolution: "@hono/valibot-validator@workspace:packages/valibot-validator"
|
||||||
dependencies:
|
dependencies:
|
||||||
hono: "npm:^3.11.7"
|
hono: "npm:^4.0.10"
|
||||||
jest: "npm:^29.7.0"
|
jest: "npm:^29.7.0"
|
||||||
rimraf: "npm:^5.0.5"
|
rimraf: "npm:^5.0.5"
|
||||||
valibot: "npm:^0.24.1"
|
valibot: "npm:^0.24.1"
|
||||||
|
|
Loading…
Reference in New Issue