From e5ad4fc0c1e02a72ffddf2ba13c7614e0039c345 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sat, 24 Jun 2023 19:30:46 +0300 Subject: [PATCH] removed Create user validation from login + done middleware of product creation --- src/middlewares/productResourceValidation.ts | 14 ++++++++++ src/middlewares/usersResourceValidation.ts | 28 ++++++++++++++++++++ src/models/userModel.ts | 16 ----------- src/routes/productRouter.ts | 2 +- src/routes/userRouter.ts | 5 ++-- src/schemas/productSchema.ts | 1 - 6 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 src/middlewares/productResourceValidation.ts create mode 100644 src/middlewares/usersResourceValidation.ts diff --git a/src/middlewares/productResourceValidation.ts b/src/middlewares/productResourceValidation.ts new file mode 100644 index 0000000..0972382 --- /dev/null +++ b/src/middlewares/productResourceValidation.ts @@ -0,0 +1,14 @@ +import { Request, Response, NextFunction } from 'express'; +import { ApiError } from '../utils/ApiError'; + +const isValidBody = async (req: Request, res: Response, next: NextFunction) => { + const { name, description, price } = req.body; + if (!name || !description || !price) { + console.log("Error in product validation") + const error = new ApiError(`${!name ? 'name' : !description ? 'description' : 'price'} is required}`); + error.statusCode = 400; + error.status = 'fail'; + return next(error); + } + next(); +} \ No newline at end of file diff --git a/src/middlewares/usersResourceValidation.ts b/src/middlewares/usersResourceValidation.ts new file mode 100644 index 0000000..f1d444c --- /dev/null +++ b/src/middlewares/usersResourceValidation.ts @@ -0,0 +1,28 @@ +import { Request, Response, NextFunction } from 'express'; +import { ApiError } from '../utils/ApiError'; +import validate from 'deep-email-validator'; + +const isValidBody = async (req: Request, res: Response, next: NextFunction) => { + + const user = req.body; + if (!user.email || !user.password || !user.address) { + const error = new ApiError(`${!user.email ? 'email' : !user.password ? 'password' : 'address'} is required}`); + error.statusCode = 400; + error.status = 'fail'; + return next(error); + } + + const { valid, reason } = await validate(user.email); + if (!valid) { + const error = new ApiError(`Invalid email: ${reason}`); + error.statusCode = 400; + error.status = 'fail'; + return next(error); + } + + next(); +} + +export { + isValidBody +} \ No newline at end of file diff --git a/src/models/userModel.ts b/src/models/userModel.ts index 3c4b6bc..f6035e4 100644 --- a/src/models/userModel.ts +++ b/src/models/userModel.ts @@ -1,25 +1,9 @@ import { User } from "../schemas/userSchema"; -import validate from 'deep-email-validator'; import { ApiError } from "../utils/ApiError"; import bcrypt from 'bcryptjs'; const createUser = async (user: any) => { - if (!user.email || !user.password || !user.address) { - const error = new ApiError('Missing required fields'); - error.statusCode = 400; - error.status = 'fail'; - return error; - } - - const { valid, reason, validators } = await validate(user.email); - if (!valid) { - const error = new ApiError(reason); - error.statusCode = 400; - error.status = 'fail'; - return error; - } - const userExists = await User.exists({ email: user.email }); if (userExists) { const error = new ApiError('User already exists, Try login :)'); diff --git a/src/routes/productRouter.ts b/src/routes/productRouter.ts index 5c3dd84..a9f313c 100644 --- a/src/routes/productRouter.ts +++ b/src/routes/productRouter.ts @@ -1,6 +1,6 @@ import express from 'express'; import { authenticateToken } from '../middlewares/checkAuth'; -import { create, getAll, getProduct } from '../controllers/productController'; +import { create, getAll, getProduct } from '../controllers/ProductController'; const productRouter = express.Router(); diff --git a/src/routes/userRouter.ts b/src/routes/userRouter.ts index 58e1856..46a08fe 100644 --- a/src/routes/userRouter.ts +++ b/src/routes/userRouter.ts @@ -1,9 +1,10 @@ import express from 'express'; -import { create, login, logout, getAll, deleteHandler } from '../controllers/userController'; +import { create, login, logout, getAll, deleteHandler } from '../controllers/UserController'; +import { isValidBody } from '../middlewares/usersResourceValidation'; const userRouter = express.Router(); -userRouter.post('/', create); +userRouter.post('/', isValidBody, create); userRouter.get('/', getAll); userRouter.post('/login', login); userRouter.post('/logout', logout); diff --git a/src/schemas/productSchema.ts b/src/schemas/productSchema.ts index 57cf79d..0b43c63 100644 --- a/src/schemas/productSchema.ts +++ b/src/schemas/productSchema.ts @@ -1,5 +1,4 @@ import mongoose, { Schema, Document } from 'mongoose'; -import { Request } from 'express'; interface IProduct extends Document { name: string;