✨ (user) create validation in user update data
This commit is contained in:
@ -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);
|
||||
|
||||
@ -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(),
|
||||
});
|
||||
|
||||
22
src/modules/user/schemas/editUser.schema.ts
Normal file
22
src/modules/user/schemas/editUser.schema.ts
Normal 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(),
|
||||
});
|
||||
Reference in New Issue
Block a user