79570593

Date: 2025-04-12 15:04:40
Score: 5.5
Natty:
Report link
// 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!

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • RegEx Blacklisted phrase (3): Thanks in advance
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): Is this an
  • Low reputation (1):
Posted by: Rupesh Thapa