diff --git a/constants/subscription-validator.ts b/constants/subscription-validator.ts index 0854d7ac9..78b05a88a 100644 --- a/constants/subscription-validator.ts +++ b/constants/subscription-validator.ts @@ -1 +1,2 @@ -export const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; \ No newline at end of file +export const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; +export const phoneNumberRegex = /^[+]{1}(?:[0-9\-\\(\\)\\/.]\s?){6,15}[0-9]{1}$/; \ No newline at end of file diff --git a/middlewares/devFlag.ts b/middlewares/devFlag.ts index 5887925af..643208cfb 100644 --- a/middlewares/devFlag.ts +++ b/middlewares/devFlag.ts @@ -1,4 +1,4 @@ -import { NextFunction, Request, Response } from "express"; +import { NextFunction } from "express"; import { CustomRequest, CustomResponse } from "../types/global"; export const devFlagMiddleware = (req: CustomRequest, res: CustomResponse, next: NextFunction) => { @@ -9,6 +9,7 @@ export const devFlagMiddleware = (req: CustomRequest, res: CustomResponse, next: } next(); } catch (err) { + logger.error("Error occurred in devFlagMiddleware:", err.message); next(err); } }; diff --git a/middlewares/validators/subscription.ts b/middlewares/validators/subscription.ts index 4ac12576f..7a3ebcfdf 100644 --- a/middlewares/validators/subscription.ts +++ b/middlewares/validators/subscription.ts @@ -1,11 +1,18 @@ import { NextFunction } from "express"; import { CustomRequest, CustomResponse } from "../../types/global"; -import { emailRegex } from "../../constants/subscription-validator"; +import { emailRegex, phoneNumberRegex } from "../../constants/subscription-validator"; // Removed phoneNumberRegex import import Joi from 'joi'; export const validateSubscribe = (req: CustomRequest, res: CustomResponse, next: NextFunction) => { + + if(req.body.email){ + req.body.email = req.body.email.trim(); + } + if (req.body.phoneNumber) { + req.body.phoneNumber = req.body.phoneNumber.trim(); + } const subscribeSchema = Joi.object({ - phoneNumber: Joi.string().allow('').optional(), + phoneNumber: Joi.string().allow('').optional().regex(phoneNumberRegex), email: Joi.string().required().regex(emailRegex) }); const { error } = subscribeSchema.validate(req.body); diff --git a/test/unit/middlewares/subscription-validator.test.js b/test/unit/middlewares/subscription-validator.test.js index 8a2c5c79e..74d2ed9df 100644 --- a/test/unit/middlewares/subscription-validator.test.js +++ b/test/unit/middlewares/subscription-validator.test.js @@ -20,7 +20,7 @@ describe("Middleware | Validators | Subscription", function () { email: "test@example.com", }; - await validateSubscribe(req, res, nextSpy); + validateSubscribe(req, res, nextSpy); expect(nextSpy.calledOnce).to.be.equal(true); expect(res.status.called).to.be.equal(false); @@ -32,7 +32,7 @@ describe("Middleware | Validators | Subscription", function () { email: "test@example.com", }; - await validateSubscribe(req, res, nextSpy); + validateSubscribe(req, res, nextSpy); expect(nextSpy.calledOnce).to.be.equal(true); expect(res.status.called).to.be.equal(false); expect(res.json.called).to.be.equal(false); @@ -43,7 +43,7 @@ describe("Middleware | Validators | Subscription", function () { phoneNumber: "+911234567890", }; - await validateSubscribe(req, res, nextSpy); + validateSubscribe(req, res, nextSpy); expect(nextSpy.called).to.be.equal(false); expect(res.status.calledOnceWith(400)).to.be.equal(true); @@ -54,7 +54,7 @@ describe("Middleware | Validators | Subscription", function () { it("should return a 400 error when both phoneNumber and email are missing", async function () { req.body = {}; - await validateSubscribe(req, res, nextSpy); + validateSubscribe(req, res, nextSpy); expect(nextSpy.called).to.be.equal(false); expect(res.status.calledOnceWith(400)).to.be.equal(true); expect(res.json.calledOnce).to.be.equal(true); @@ -67,7 +67,7 @@ describe("Middleware | Validators | Subscription", function () { email: "invalid-email", }; - await validateSubscribe(req, res, nextSpy); + validateSubscribe(req, res, nextSpy); expect(nextSpy.called).to.be.equal(false); expect(res.status.calledOnceWith(400)).to.be.equal(true); @@ -83,9 +83,39 @@ describe("Middleware | Validators | Subscription", function () { email: "test@example.com", }; - await validateSubscribe(req, res, nextSpy); + validateSubscribe(req, res, nextSpy); expect(nextSpy.calledOnce).to.be.equal(true); expect(res.status.called).to.be.equal(false); expect(res.json.called).to.be.equal(false); }); + + it("should trim and validate phoneNumber if it contains leading or trailing spaces", async function () { + req.body = { + phoneNumber: " +911234567890 ", + email: "test@example.com", + }; + + validateSubscribe(req, res, nextSpy); + + expect(nextSpy.calledOnce).to.be.equal(true); + expect(res.status.called).to.be.equal(false); + expect(res.json.called).to.be.equal(false); + expect(req.body.phoneNumber).to.equal("+911234567890"); + }); + + it("should return a 400 error when phoneNumber is in incorrect format", async function () { + req.body = { + phoneNumber: "invalid-number", + email: "test@example.com", + }; + + validateSubscribe(req, res, nextSpy); + + expect(nextSpy.called).to.be.equal(false); + expect(res.status.calledOnceWith(400)).to.be.equal(true); + expect(res.json.calledOnce).to.be.equal(true); + expect(res.json.firstCall.args[0]) + .to.have.property("error") + .that.includes('"phoneNumber" with value "invalid-number" fails to match the required pattern'); + }); });