You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to update a route's request body schema (request.routeOptions.schema.body) according to the provided route param in runtime, using the preValidation hook? It's possible to achieve this using the preHandler hook but that's past the builtin validation stage of the request, and requires manually validating the request body.
import{typeZodTypeProvider}from'fastify-type-provider-zod';fastify.withTypeProvider<ZodTypeProvider>().put('/userInteractions/:interactionKind',{schema: updateUserInteractionRouteSchema,// doesn't workpreValidation: asyncrequest=>{constinteractionKindPayloadSchema=userInteractionPayloadSchemaByKind[request.params.interactionKind];request.routeOptions.schema.body=interactionKindPayloadSchema/* requires `as any` to work */;},// works, but requires manual validationpreHandler: asyncrequest=>{constinteractionKindPayloadSchema=userInteractionPayloadSchemaByKind[request.params.interactionKind];try{interactionKindPayloadSchema.parse(request.body);}catch(error){thrownewError('Invalid request body for the provided interaction kind',{cause: error},);}},},(request,reply)=>{ ... },);
Updating request.routeOptions.schema.body in the preValidation hook doesn't seem to work - when getting a request for the "bookmark" interaction kind with { "reaction": "like" } as the body, which matches another interaction kind's schema, the handler is reached even though it should fail. My current workaround is to manually validate the request body in the preHandler hook.
Your Environment
node version: 20.10.0
fastify version: 4.24.3
os: Mac
The text was updated successfully, but these errors were encountered:
amitbeck
changed the title
Update a route's request body schema according to the provided route param in runtime
Update request body schema according to the provided route param in runtime
Oct 28, 2024
I'm not an expert on zod, but that cannot be achieved directly in the schema definition to do a lookup for the property and use a variant?
At the moment the request is received, the context of the request is sealed and cannot be altered (which affects the schema). request.routeOptions properties are read-only.
This has to be achieved using the fastify.setValidationCompiler. There you can customize how the validation gets set.
💬 Question here
Is it possible to update a route's request body schema (
request.routeOptions.schema.body
) according to the provided route param in runtime, using thepreValidation
hook? It's possible to achieve this using thepreHandler
hook but that's past the builtin validation stage of the request, and requires manually validating the request body.This is my schema:
And this is my route:
Updating
request.routeOptions.schema.body
in thepreValidation
hook doesn't seem to work - when getting a request for the"bookmark"
interaction kind with{ "reaction": "like" }
as the body, which matches another interaction kind's schema, the handler is reached even though it should fail. My current workaround is to manually validate the request body in thepreHandler
hook.Your Environment
The text was updated successfully, but these errors were encountered: