@@ -222,17 +222,19 @@ const composeValidationFactory = ({
222222 validator,
223223 encodeSchema = false ,
224224 isStaticResponse = false ,
225- hasSanitize = false
225+ hasSanitize = false ,
226+ allowUnsafeValidationDetails = false
226227} : {
227228 injectResponse ?: string
228229 normalize ?: ElysiaConfig < '' > [ 'normalize' ]
229230 validator : SchemaValidator
230231 encodeSchema ?: boolean
231232 isStaticResponse ?: boolean
232233 hasSanitize ?: boolean
234+ allowUnsafeValidationDetails ?: boolean
233235} ) => ( {
234236 validate : ( type : string , value = `c.${ type } ` , error ?: string ) =>
235- `c.set.status=422;throw new ValidationError('${ type } ',validator.${ type } ,${ value } ${ error ? ',' + error : '' } )` ,
237+ `c.set.status=422;throw new ValidationError('${ type } ',validator.${ type } ,${ value } , ${ allowUnsafeValidationDetails } ${ error ? ',' + error : '' } )` ,
236238 response : ( name = 'r' ) => {
237239 if ( isStaticResponse || ! validator . response ) return ''
238240
@@ -254,7 +256,7 @@ const composeValidationFactory = ({
254256 `let vare${ status } =validator.response[${ status } ].Check(${ name } )\n` +
255257 `if(vare${ status } instanceof Promise)vare${ status } =await vare${ status } \n` +
256258 `if(vare${ status } .issues)` +
257- `throw new ValidationError('response',validator.response[${ status } ],${ name } ,vare${ status } .issues)\n` +
259+ `throw new ValidationError('response',validator.response[${ status } ],${ name } ,${ allowUnsafeValidationDetails } , vare${ status } .issues)\n` +
258260 `${ name } =vare${ status } .value\n` +
259261 `c.set.status=${ status } \n` +
260262 'break\n'
@@ -313,17 +315,17 @@ const composeValidationFactory = ({
313315 clean ( { ignoreTryCatch : true } ) +
314316 `${ name } =validator.response[${ status } ].Encode(${ name } )\n` +
315317 `}catch{` +
316- `throw new ValidationError('response',validator.response[${ status } ],${ name } )` +
318+ `throw new ValidationError('response',validator.response[${ status } ],${ name } , ${ allowUnsafeValidationDetails } )` +
317319 `}`
318- : `throw new ValidationError('response',validator.response[${ status } ],${ name } )` ) +
320+ : `throw new ValidationError('response',validator.response[${ status } ],${ name } ), ${ allowUnsafeValidationDetails } ` ) +
319321 `}`
320322 } else {
321323 if ( ! appliedCleaner ) code += clean ( )
322324
323325 if ( ! noValidate )
324326 code +=
325327 `if(validator.response[${ status } ].Check(${ name } )===false)` +
326- `throw new ValidationError('response',validator.response[${ status } ],${ name } )\n` +
328+ `throw new ValidationError('response',validator.response[${ status } ],${ name } , ${ allowUnsafeValidationDetails } )\n` +
327329 `c.set.status=${ status } \n`
328330 }
329331
@@ -411,12 +413,13 @@ const isGenerator = (v: Function | HookContainer) => {
411413const coerceTransformDecodeError = (
412414 fnLiteral : string ,
413415 type : string ,
416+ allowUnsafeValidationDetails = false ,
414417 value = `c.${ type } `
415418) =>
416419 `try{${ fnLiteral } }catch(error){` +
417420 `if(error.constructor.name === 'TransformDecodeError'){` +
418421 `c.set.status=422\n` +
419- `throw error.error ?? new ValidationError('${ type } ',validator.${ type } ,${ value } )}` +
422+ `throw error.error ?? new ValidationError('${ type } ',validator.${ type } ,${ value } , ${ allowUnsafeValidationDetails } )}` +
420423 `}`
421424
422425export const composeHandler = ( {
@@ -590,13 +593,15 @@ export const composeHandler = ({
590593
591594 const normalize = app . config . normalize
592595 const encodeSchema = app . config . encodeSchema
596+ const allowUnsafeValidationDetails = app . config . allowUnsafeValidationDetails
593597
594598 const validation = composeValidationFactory ( {
595599 normalize,
596600 validator,
597601 encodeSchema,
598602 isStaticResponse : handler instanceof Response ,
599- hasSanitize : ! ! app . config . sanitize
603+ hasSanitize : ! ! app . config . sanitize ,
604+ allowUnsafeValidationDetails
600605 } )
601606
602607 if ( hasHeaders ) fnLiteral += adapter . headers
@@ -763,11 +768,14 @@ export const composeHandler = ({
763768 if ( ! hooks . afterResponse ?. length && ! hasTrace ) return ''
764769
765770 let afterResponse = ''
766- const prefix = hooks . afterResponse ?. some ( isAsync ) ? 'async ' : ''
767771
768772 afterResponse +=
769- `\nsetImmediate(${ prefix } ()=>{` +
770- `if(c.responseValue instanceof ElysiaCustomStatusResponse) c.set.status=c.responseValue.code\n`
773+ `\nsetImmediate(async()=>{` +
774+ `if(c.responseValue){` +
775+ `if(c.responseValue instanceof ElysiaCustomStatusResponse) c.set.status=c.responseValue.code\n` +
776+ `else if(c.responseValue[Symbol.iterator]) for (const v of c.responseValue) { }` +
777+ `else if(c.responseValue[Symbol.asyncIterator]) for await (const v of c.responseValue) { }` +
778+ `}`
771779
772780 const reporter = createReport ( {
773781 trace : hooks . trace ,
@@ -1184,7 +1192,8 @@ export const composeHandler = ({
11841192 if ( validator . headers . hasTransform )
11851193 fnLiteral += coerceTransformDecodeError (
11861194 `c.headers=validator.headers.Decode(c.headers)\n` ,
1187- 'headers'
1195+ 'headers' ,
1196+ allowUnsafeValidationDetails
11881197 )
11891198
11901199 if ( validator . headers . isOptional ) fnLiteral += '}'
@@ -1226,7 +1235,8 @@ export const composeHandler = ({
12261235 if ( validator . params . hasTransform )
12271236 fnLiteral += coerceTransformDecodeError (
12281237 `c.params=validator.params.Decode(c.params)\n` ,
1229- 'params'
1238+ 'params' ,
1239+ allowUnsafeValidationDetails
12301240 )
12311241 }
12321242
@@ -1279,11 +1289,13 @@ export const composeHandler = ({
12791289 // For query, we decode it twice to ensure that it works
12801290 fnLiteral += coerceTransformDecodeError (
12811291 `c.query=validator.query.Decode(c.query)\n` ,
1282- 'query'
1292+ 'query' ,
1293+ allowUnsafeValidationDetails
12831294 )
12841295 fnLiteral += coerceTransformDecodeError (
12851296 `c.query=validator.query.Decode(c.query)\n` ,
1286- 'query'
1297+ 'query' ,
1298+ allowUnsafeValidationDetails
12871299 )
12881300 }
12891301
@@ -1398,7 +1410,8 @@ export const composeHandler = ({
13981410 if ( validator . body . hasTransform )
13991411 fnLiteral += coerceTransformDecodeError (
14001412 `if(isNotEmptyObject)c.body=validator.body.Decode(c.body)\n` ,
1401- 'body'
1413+ 'body' ,
1414+ allowUnsafeValidationDetails
14021415 )
14031416
14041417 if ( hasUnion && validator . body . schema . anyOf ?. length ) {
@@ -1537,7 +1550,8 @@ export const composeHandler = ({
15371550 `for(const [key,value] of Object.entries(validator.cookie.Decode(cookieValue))){` +
15381551 `c.cookie[key].value=value` +
15391552 `}` ,
1540- 'cookie'
1553+ 'cookie' ,
1554+ allowUnsafeValidationDetails
15411555 )
15421556 }
15431557
0 commit comments