diff --git a/src/__test__/review.test.ts b/src/__test__/review.test.ts index d038ecc..f9bc96f 100644 --- a/src/__test__/review.test.ts +++ b/src/__test__/review.test.ts @@ -91,4 +91,13 @@ describe('Review controller test', () => { expect(responseReview.body.message).toEqual('you are already reviewed the product'); }) + + it('should return 400 for failed validation on create review', async () => { + const reviewBody = {content:'good', rating:15, productId:'some id'} + const responseReview = await request(app) + .post('/api/v1/review') + .set('Authorization', `Bearer ${buyerToken}`) + .send(reviewBody) + expect(responseReview.statusCode).toEqual(400); + }) }) \ No newline at end of file diff --git a/src/controller/reviewController.ts b/src/controller/reviewController.ts index 069b6c7..8e07a23 100644 --- a/src/controller/reviewController.ts +++ b/src/controller/reviewController.ts @@ -4,13 +4,24 @@ import Product from '../database/models/productEntity'; import dbConnection from '../database'; import errorHandler from '../middlewares/errorHandler'; import UserModel from '../database/models/userModel'; +import {createReviewSchema} from '../middlewares/createReviewSchema'; const productRepository = dbConnection.getRepository(Product); const reviewRepository = dbConnection.getRepository(Review); const userRepository = dbConnection.getRepository(UserModel); export const createReview = errorHandler(async (req: Request, res: Response) => { - const { content, rating, productId } = req.body; + const formData = req.body; + + const validationResult = createReviewSchema.validate(formData); + if (validationResult.error) { + return res + .status(400) + .json({ msg: validationResult.error.details[0].message }); + } + + const {content, rating, productId} = formData + const userId = req.user!.id; const product = await productRepository.findOne({ diff --git a/src/docs/reviewDocs.ts b/src/docs/reviewDocs.ts index 05ae5c4..2fdd6d7 100644 --- a/src/docs/reviewDocs.ts +++ b/src/docs/reviewDocs.ts @@ -6,31 +6,19 @@ * tags: [Reviews] * security: * - bearerAuth: [] - * consumes: - * - application/json - * produces: - * - application/json - * parameters: - * - in: body - * name: review - * description: The review object - * required: true - * schema: - * type: object - * properties: - * content: - * type: string - * description: The content of the review - * rating: - * type: integer - * description: The rating of the review (1 to 5) - * productId: - * type: integer - * description: The ID of the product being reviewed - * example: - * content: "This product is amazing!" - * rating: 5 - * productId: 50 + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * content: + * type: string + * rating: + * type: number + * productId: + * type: number * responses: * '201': * description: Review created successfully @@ -42,7 +30,7 @@ * description: A success message * review: * '400': - * description: Bad request, check the request body + * description: Bad request, failed validation * '404': * description: Product not found * '409': diff --git a/src/middlewares/createReviewSchema.ts b/src/middlewares/createReviewSchema.ts new file mode 100644 index 0000000..3eba04d --- /dev/null +++ b/src/middlewares/createReviewSchema.ts @@ -0,0 +1,8 @@ +import Joi from 'joi'; + + +export const createReviewSchema = Joi.object({ + content: Joi.string().required(), + rating: Joi.number().min(0).max(5).required(), + productId: Joi.number().required() +}) \ No newline at end of file