1
- import type { Context , Env , Input as HonoInput , MiddlewareHandler , ValidationTargets } from 'hono'
1
+ import type { Context , Env , Input , MiddlewareHandler , TypedResponse , ValidationTargets } from 'hono'
2
2
import { validator } from 'hono/validator'
3
3
import type {
4
4
GenericSchema ,
@@ -9,10 +9,18 @@ import type {
9
9
} from 'valibot'
10
10
import { safeParseAsync } from 'valibot'
11
11
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
+ } ,
14
22
c : Context < E , P >
15
- ) => Response | Promise < Response > | void | Promise < Response | void >
23
+ ) => Response | void | TypedResponse < O > | Promise < Response | void | TypedResponse < O > >
16
24
17
25
type HasUndefined < T > = undefined extends T ? true : false
18
26
@@ -23,20 +31,16 @@ export const vValidator = <
23
31
P extends string ,
24
32
In = InferInput < T > ,
25
33
Out = InferOutput < T > ,
26
- I extends HonoInput = {
34
+ I extends Input = {
27
35
in : HasUndefined < In > extends true
28
36
? {
29
- [ K in Target ] ?: K extends 'json'
37
+ [ K in Target ] ?: In extends ValidationTargets [ K ]
30
38
? 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 ] }
34
40
}
35
41
: {
36
- [ K in Target ] : K extends 'json'
42
+ [ K in Target ] : In extends ValidationTargets [ K ]
37
43
? In
38
- : HasUndefined < keyof ValidationTargets [ K ] > extends true
39
- ? { [ K2 in keyof In ] ?: ValidationTargets [ K ] [ K2 ] }
40
44
: { [ K2 in keyof In ] : ValidationTargets [ K ] [ K2 ] }
41
45
}
42
46
out : { [ K in Target ] : Out }
@@ -45,23 +49,28 @@ export const vValidator = <
45
49
> (
46
50
target : Target ,
47
51
schema : T ,
48
- hook ?: Hook < T , E , P >
52
+ hook ?: Hook < T , E , P , Target >
49
53
) : MiddlewareHandler < E , P , V > =>
50
54
// @ts -expect-error not typed well
51
55
validator ( target , async ( value , c ) => {
52
56
const result = await safeParseAsync ( schema , value )
53
57
54
58
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
+ }
58
68
}
59
69
}
60
70
61
71
if ( ! result . success ) {
62
72
return c . json ( result , 400 )
63
73
}
64
74
65
- const data = result . output as InferOutput < T >
66
- return data
75
+ return result . output
67
76
} )
0 commit comments