Skip to content

Commit 192105a

Browse files
committed
fix: add Zod validation schema for user creation endpoint
Add createUserSchema with email, name, and role validation to POST /api/users endpoint to prevent arbitrary data storage. Fixes #2160
1 parent 7865310 commit 192105a

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { ok } from "../utils/response.js";
22
import { createUser, listUsers } from "../services/userService.js";
3+
import { createUserSchema } from "../validators/user.js";
34

45
export async function getUsers(req, res) {
56
return ok(res, await listUsers());
67
}
78

89
export async function postUser(req, res) {
9-
return ok(res, await createUser(req.body), 201);
10+
const payload = createUserSchema.parse(req.body);
11+
return ok(res, await createUser(payload), 201);
1012
}

apps/api/src/validators/user.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from "zod";
2+
3+
export const createUserSchema = z.object({
4+
email: z.string().email(),
5+
name: z.string().min(1).max(100),
6+
role: z.enum(["client", "freelancer", "admin"]).default("client")
7+
});
8+
9+
export const updateUserSchema = createUserSchema.partial();

0 commit comments

Comments
 (0)