// route
export const todoRoute = new Hono().post(
"/",
zValidator("json", createTodoSchema, (result, c: Context) => {
if (!result.success) {
return c.json(
{
message: "Validation failed",
errors: result.error.errors.map(({ message }) => message),
},
400
);
}
c.set("validatedData", result.data);
// Optional: c.set("isValidated", result.success);
}),
createTodo
);
export async function createTodo(c: Context) {
const body = c.get("validatedData") as CreateTodoSchemaType;
const newTodo = new Todo(body);
await newTodo.save();
return c.json({
message: "Todo created successfully",
data: {
title: body.title,
completed: body.completed,
},
});
}
Is this an acceptable way to avoid using Context<Env, Path, Input> generics and still make full use of validation?
Does storing the validated data in c.set() have any performance or memory drawbacks, or is this considered a clean solution?
Thanks in advance!