From 700be2ad1bac6009ffbd903bc532bb23bb1b7c39 Mon Sep 17 00:00:00 2001 From: jose-carlos-sousa <139002032+jose-carlos-sousa@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:50:07 +0100 Subject: [PATCH] added validator in middleware --- src/api/middleware/validators/offer.js | 8 ++++++++ src/api/middleware/validators/validationReasons.js | 1 + 2 files changed, 9 insertions(+) diff --git a/src/api/middleware/validators/offer.js b/src/api/middleware/validators/offer.js index 1f0b8141..38371c5e 100644 --- a/src/api/middleware/validators/offer.js +++ b/src/api/middleware/validators/offer.js @@ -18,6 +18,13 @@ import { import * as companyMiddleware from "../company.js"; import config from "../../../config/env.js"; import { validApplyURL } from "../../../models/modelUtils.js"; +const jobMinDurationMustNotExistInFreelance = (jobMinDuration, { req }) => { + if (req.body.jobType !== "FREELANCE") return true; + if (jobMinDuration !== null && jobMinDuration !== undefined) { + throw new Error(ValidationReasons.FREELANCE_OFFER_CANT_HAVE_MIN_DURATION); + } + return true; +}; const jobMaxDurationGreaterOrEqualThanJobMinDuration = (jobMaxDuration, { req }) => { @@ -82,6 +89,7 @@ export const create = useExpressValidators([ body("jobMinDuration", ValidationReasons.DEFAULT) + .custom(jobMinDurationMustNotExistInFreelance) .if((value, { req }) => req.body.jobType !== "FREELANCE") .exists().withMessage(ValidationReasons.REQUIRED).bail() .isInt().withMessage(ValidationReasons.INT), diff --git a/src/api/middleware/validators/validationReasons.js b/src/api/middleware/validators/validationReasons.js index 7a38a793..fcd35d97 100644 --- a/src/api/middleware/validators/validationReasons.js +++ b/src/api/middleware/validators/validationReasons.js @@ -50,6 +50,7 @@ const ValidationReasons = Object.freeze({ OFFER_HIDDEN: "offer-is-hidden", FILE_TOO_LARGE: (max) => `file-cant-be-larger-than-${max}MB`, MUST_BE_GREATER_THAN_OR_EQUAL_TO: (field) => `must-be-greater-than-or-equal-to:${field}`, + FREELANCE_OFFER_CANT_HAVE_MIN_DURATION: "freelance-offer-cant-have-min-duration", }); export default ValidationReasons;