Description
I’m trying to make my middleware aware of the validated request body defined in the route schema.
Inside my route handler, c.req.valid('json') correctly infers and validates the body according to the schema, but inside middleware, it does not.
Here’s the route definition:
export const createCemeteryRoute = createRoute({
tags,
path: basePath,
method: 'post',
summary: 'Create a new cemetery',
description: 'Authenticated users can create a new cemetery.',
middleware: [
authMiddleware(),
createMiddleware(async (c, next) => {
const data = await c.req.valid('json'); // <-- not aware of the schema here
const resourceId = data.cityId;
return restrictAccessMiddleware({
check: 'role-and-geo',
accessLevel: 'city',
resourceId,
})(c, next);
}),
],
request: {
headers: authorizationHeaderSchema,
body: jsonContentRequired(
drizzleZodCemeteriesSchema.insert,
'The cemetery to create'
),
},
responses: {
[CREATED_CODE]: jsonContent(
drizzleZodCemeteriesSchema.select,
'The created cemetery'
),
[UNPROCESSABLE_ENTITY_CODE]: jsonContent(
createErrorSchema(drizzleZodCemeteriesSchema.insert),
'The validation error(s)'
),
[INTERNAL_SERVER_ERROR_CODE]: jsonContent(
internalServerErrorSchema,
'Failed to create cemetery'
),
[UNAUTHORIZED_CODE]: jsonContent(
unauthorizedSchema,
'Missing or invalid authentication'
),
[FORBIDDEN_CODE]: jsonContent(
forbiddenSchema,
'Authenticated but not allowed to access this resource'
),
},
});
export type CreateCemeteryRoute = typeof createCemeteryRoute;
In the route handler, this works perfectly:
export const createCemeteryHandler: AppRouteHandler<CreateCemeteryRoute> = async c => {
const data = c.req.valid('json'); // works fine here
const database = c.get('drizzle');
const { data: createdData, error } = await createCemetery(database, data);
if (error) {
return c.json(
{ success: false, message: 'Failed to create cemetery' },
INTERNAL_SERVER_ERROR_CODE
);
}
return c.json(createdData, CREATED_CODE);
};
Expected Behavior
c.req.valid('json') inside middleware should be aware of (and typed according to) the route’s request.body schema, just like in the handler.
Actual Behavior
In middleware, c.req.valid('json') doesn’t have type inference or schema awareness, it returns a generic type instead of the inferred validated data type.
Question
Is there a recommended way to make middleware aware of the validated request body defined in the route schema? Or is this currently unsupported by design? Or a suggested workaround, maybe?
Description
I’m trying to make my middleware aware of the validated request body defined in the route schema.
Inside my route handler,
c.req.valid('json')correctly infers and validates the body according to the schema, but inside middleware, it does not.Here’s the route definition:
In the route handler, this works perfectly:
Expected Behavior
c.req.valid('json')inside middleware should be aware of (and typed according to) the route’s request.body schema, just like in the handler.Actual Behavior
In middleware,
c.req.valid('json')doesn’t have type inference or schema awareness, it returns a generic type instead of the inferred validated data type.Question
Is there a recommended way to make middleware aware of the validated request body defined in the route schema? Or is this currently unsupported by design? Or a suggested workaround, maybe?