Add good validation to all existing endpoints

This commit is contained in:
Kevin Puig
2025-09-27 14:17:15 -04:00
parent b9982b626f
commit 27807357ac
3 changed files with 82 additions and 23 deletions

View File

@@ -1,33 +1,67 @@
import { Hono } from "hono";
import { fetchAllUsers, fetchUserData, createNewUser } from "../controller/userController";
import { createUserSchema } from "../validators/userValidator";
import { createUserSchema, queryAllUsersByInstanceId, queryUserByIdSchema } from "../validators/userValidator";
import { zValidator } from "@hono/zod-validator";
import { describeRoute, resolver } from "hono-openapi";
const actions = new Hono();
actions.get("user/:id", async (c) => {
const id = c.req.param("id");
const userData = await fetchUserData(id);
if (userData) {
return c.json(userData);
} else {
return c.json({ error: "User not found" }, 404);
actions.get("user/:id",
describeRoute({
description: "Get user by id",
responses: {
200: {
description: "Success getting user",
content: {
"application/json": { schema: resolver(queryUserByIdSchema) }
}
},
404: {
description: "User id not found",
content: {
"application/json": { schema: resolver(queryUserByIdSchema) }
}
}
}
}),
zValidator('param', queryUserByIdSchema),
async (c) => {
const id = c.req.param("id");
const userData = await fetchUserData(id);
if (userData) {
return c.json(userData);
} else {
return c.json({ error: "User not found" }, 404);
}
}
});
);
actions.get("user", async (c) => {
const instanceId = c.req.query("instance_id");
if (!instanceId) {
return c.json({ error: "No instance id provided" }, 400);
}
actions.get("user",
describeRoute({
description: "Get all users by instance id",
responses: {
200: {
description: "Success getting all users in instance",
content: {
"application/json": { schema: resolver(queryAllUsersByInstanceId) }
}
}
}
}),
zValidator('query', queryAllUsersByInstanceId),
async (c) => {
const instanceId = c.req.query("instanceId");
if (!instanceId) {
return c.json({ error: "No instance id provided" }, 400);
}
const userData = await fetchAllUsers(instanceId);
if (userData) {
return c.json(userData);
} else {
return c.json({ error: "Error getting all users from instance" }, 500);
const userData = await fetchAllUsers(instanceId);
if (userData) {
return c.json(userData);
} else {
return c.json({ error: "Error getting all users from instance" }, 500);
}
}
});
);
actions.post(
"user",
@@ -39,6 +73,12 @@ actions.post(
content: {
'application/json': { schema: resolver(createUserSchema) },
},
},
400: {
description: "Bad request (user exists)",
content: {
'application/json': { schema: resolver(createUserSchema) }
}
}
}
}),
@@ -47,6 +87,9 @@ actions.post(
try {
const data = await c.req.json();
const newUser = await createNewUser(data);
if (!newUser) {
return c.json({ error: "User already exists" }, 400);
}
return c.json(newUser, 201);
} catch (error) {
return c.json({ error: "Error creating user" }, 500);