From 5f18af1ae43e4bc4d7510209df128ababd4ca9ee 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 --- .env | 2 +- src/api/middleware/validators/offer.js | 8 ++++++++ src/api/middleware/validators/validationReasons.js | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 59857deb..0c847778 100644 --- a/.env +++ b/.env @@ -35,7 +35,7 @@ ADMIN_PASSWORD=n1j0bs_ftw.12345 # List of regexes or url's specifying allowed origins. Example: # ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app"] -ACCESS_CONTROL_ALLOW_ORIGINS= +ACCESS_CONTROL_ALLOW_ORIGINS=["https:\\/\\/deploy-preview-\\d+--nijobs\\.netlify\\.app", "https://nijobs.netlify.app","https://localhost"] # Mail service information. If you don't provide a MAIL_FROM, no emails will be sent. The app will execute no-ops and won't crash # However, if you want to send emails, you need to fill all of the following 2 fields 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;