(user) create validation in user update data

This commit is contained in:
unknown
2025-06-28 17:44:31 +07:00
parent 89c4fb79a7
commit 01b29e2a78
7 changed files with 1089 additions and 14 deletions

1024
bun.lock Normal file

File diff suppressed because it is too large Load Diff

BIN
bun.lockb

Binary file not shown.

View File

@ -0,0 +1,12 @@
/*
Warnings:
- You are about to drop the column `commentPicture` on the `users` table. All the data in the column will be lost.
- You are about to drop the column `profilePicture` on the `users` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "users" DROP COLUMN "commentPicture",
DROP COLUMN "profilePicture",
ADD COLUMN "avatar" TEXT,
ADD COLUMN "commentBackground" TEXT;

View File

@ -2,6 +2,10 @@ import { saveFile } from "..";
import { AppError } from "../../../error/instances/app";
export const saveAvatar = async (file: File): Promise<string> => {
if (Array.isArray(file)) {
throw new AppError(415, "Can't upload more than 1 file");
}
const allowedTypes = ["image/png", "image/jpeg", "image/webp"];
if (!allowedTypes.includes(file.type)) {
throw new AppError(

View File

@ -1,5 +1,8 @@
import { Context } from "elysia";
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
import {
returnErrorResponse,
returnWriteResponse,
} from "../../../helpers/callback/httpResponse";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { Prisma } from "@prisma/client";
import { editUserService } from "../services/editUser.service";
@ -8,6 +11,7 @@ import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUs
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
import { jwtEncode } from "../../../helpers/http/jwt/encode";
import { editUserSchema } from "../schemas/editUser.schema";
/**
* @function editUserController
@ -36,8 +40,8 @@ import { jwtEncode } from "../../../helpers/http/jwt/encode";
* "phoneCC": 62,
* "phoneNumber": 81234567890,
* "bioProfile": "Updated bio",
* "profilePicture": "https://example.com/new-profile.jpg",
* "commentPicture": "https://example.com/new-comment.jpg",
* "profilePicture": JPG/PNG/JPEG File,
* "commentPicture": JPG/PNG/JPEG File,
* "deletedAt": null
* }
*
@ -58,6 +62,11 @@ export const editUserController = async (
body: Prisma.UserUncheckedCreateInput;
}
) => {
// Validate the request body against the edit user schema
const { error } = editUserSchema.validate(ctx.body);
if (error)
return returnErrorResponse(ctx.set, 422, "Invalid form input", error);
try {
// Get the user JWT token from cookies, if the token is not found, return an error response
const userCookie = getCookie(ctx);

View File

@ -1,15 +1,19 @@
import Joi from "joi";
export const createUserSchema = Joi.object({
name: Joi.string().min(4).max(255).required(),
username: Joi.string().min(4).max(255).required(),
email: Joi.string().email().required(),
password: Joi.string().min(8).max(255).required(),
birthdate: Joi.date(),
gender: Joi.string().valid("male", "female"),
phoneCC: Joi.string().min(2).max(2),
phoneNumber: Joi.string().min(7).max(15),
bioProfile: Joi.string().max(300),
avatar: Joi.string().uri(),
commentBackground: Joi.string().uri(),
name: Joi.string()
.min(4)
.max(255)
.required(),
username: Joi.string()
.min(4)
.max(255)
.required(),
email: Joi.string()
.email()
.required(),
password: Joi.string()
.min(8)
.max(255)
.required(),
});

View File

@ -0,0 +1,22 @@
import Joi from "joi";
export const editUserSchema = Joi.object({
name: Joi.string()
.min(4)
.max(255),
username: Joi.string()
.min(4)
.max(255),
birthdate: Joi.date(),
gender: Joi.string().valid("male", "female"),
phoneCC: Joi.string()
.min(2)
.max(2),
phoneNumber: Joi.string()
.min(7)
.max(15),
bioProfile: Joi.string().max(300),
avatar: Joi.string().uri(),
commentBackground: Joi.string().uri(),
deletedAt: Joi.date(),
});