@@ -2,6 +2,7 @@ import type { StandardSchemaV1 } from '@standard-schema/spec';
22import type {
33 HandlerResponse ,
44 Middleware ,
5+ TypedRequestContext ,
56 ValidatedRequest ,
67 ValidatedResponse ,
78 ValidationConfig ,
@@ -24,8 +25,8 @@ export const createValidationMiddleware = <
2425 const resSchemas = config ?. res ;
2526
2627 return async ( { reqCtx, next } ) => {
27- // Initialize valid object
28- ( reqCtx as any ) . valid = {
28+ const typedReqCtx = reqCtx as TypedRequestContext < TReqBody , TResBody > ;
29+ typedReqCtx . valid = {
2930 req : { } as ValidatedRequest < TReqBody > ,
3031 res : { } as ValidatedResponse < TResBody > ,
3132 } ;
@@ -53,19 +54,19 @@ export const createValidationMiddleware = <
5354 bodyData ,
5455 'body'
5556 ) ;
56- ( ( reqCtx as any ) . valid . req as ValidatedRequest < TReqBody > ) . body =
57+ ( typedReqCtx . valid . req as ValidatedRequest < TReqBody > ) . body =
5758 validatedBody as TReqBody ;
5859 }
5960 if ( reqSchemas . headers ) {
6061 const headers = Object . fromEntries ( reqCtx . req . headers . entries ( ) ) ;
61- ( reqCtx as any ) . valid . req . headers = ( await validateRequest (
62+ typedReqCtx . valid . req . headers = ( await validateRequest (
6263 reqSchemas . headers ,
6364 headers ,
6465 'headers'
6566 ) ) as Record < string , string > ;
6667 }
6768 if ( reqSchemas . path ) {
68- ( reqCtx as any ) . valid . req . path = ( await validateRequest (
69+ typedReqCtx . valid . req . path = ( await validateRequest (
6970 reqSchemas . path ,
7071 reqCtx . params ,
7172 'path'
@@ -75,7 +76,7 @@ export const createValidationMiddleware = <
7576 const query = Object . fromEntries (
7677 new URL ( reqCtx . req . url ) . searchParams . entries ( )
7778 ) ;
78- ( reqCtx as any ) . valid . req . query = ( await validateRequest (
79+ typedReqCtx . valid . req . query = ( await validateRequest (
7980 reqSchemas . query ,
8081 query ,
8182 'query'
@@ -97,7 +98,7 @@ export const createValidationMiddleware = <
9798 ? await clonedResponse . json ( )
9899 : await clonedResponse . text ( ) ;
99100
100- ( reqCtx as any ) . valid . res . body = ( await validateResponse (
101+ typedReqCtx . valid . res . body = ( await validateResponse (
101102 resSchemas . body ,
102103 bodyData ,
103104 'body'
@@ -106,7 +107,7 @@ export const createValidationMiddleware = <
106107
107108 if ( resSchemas . headers ) {
108109 const headers = Object . fromEntries ( response . headers . entries ( ) ) ;
109- ( reqCtx as any ) . valid . res . headers = ( await validateResponse (
110+ typedReqCtx . valid . res . headers = ( await validateResponse (
110111 resSchemas . headers ,
111112 headers ,
112113 'headers'
0 commit comments