@@ -2,6 +2,8 @@ import { Hono } from 'hono'
2
2
import type { Equal , Expect } from 'hono/utils/types'
3
3
import { z } from 'zod'
4
4
import { zValidator } from '../src'
5
+ import { vi } from 'vitest'
6
+
5
7
6
8
// eslint-disable-next-line @typescript-eslint/no-unused-vars
7
9
type ExtractSchema < T > = T extends Hono < infer _ , infer S > ? S : never
@@ -256,3 +258,46 @@ describe('With Async Hook', () => {
256
258
expect ( await res . text ( ) ) . toBe ( '123 is invalid!' )
257
259
} )
258
260
} )
261
+
262
+
263
+ describe ( 'With target' , ( ) => {
264
+ it ( 'should call hook for correctly validated target' , async ( ) => {
265
+ const app = new Hono ( )
266
+
267
+ const schema = z . object ( {
268
+ id : z . string ( ) ,
269
+ } )
270
+
271
+ const jsonHook = vi . fn ( )
272
+ const paramHook = vi . fn ( )
273
+ const queryHook = vi . fn ( )
274
+ app . post (
275
+ '/:id/post' ,
276
+ zValidator ( 'json' , schema , jsonHook ) ,
277
+ zValidator ( 'param' , schema , paramHook ) ,
278
+ zValidator ( 'query' , schema , queryHook ) ,
279
+ ( c ) => {
280
+ return c . text ( 'ok' )
281
+ }
282
+ )
283
+
284
+ const req = new Request ( 'http://localhost/1/post?id=2' , {
285
+ body : JSON . stringify ( {
286
+ id : '3' ,
287
+ } ) ,
288
+ method : 'POST' ,
289
+ headers : {
290
+ 'Content-Type' : 'application/json' ,
291
+ } ,
292
+ } )
293
+
294
+ const res = await app . request ( req )
295
+ expect ( res ) . not . toBeNull ( )
296
+ expect ( res . status ) . toBe ( 200 )
297
+ expect ( await res . text ( ) ) . toBe ( 'ok' )
298
+ expect ( paramHook ) . toHaveBeenCalledWith ( { data : { id : '1' } , success : true , target :
299
+ 'param' } , expect . anything ( ) )
300
+ expect ( queryHook ) . toHaveBeenCalledWith ( { data : { id : '2' } , success : true , target : 'query' } , expect . anything ( ) )
301
+ expect ( jsonHook ) . toHaveBeenCalledWith ( { data : { id : '3' } , success : true , target : 'json' } , expect . anything ( ) )
302
+ } )
303
+ } )
0 commit comments