From e2ddeb8b073bc091715e9b240720ca684969cc94 Mon Sep 17 00:00:00 2001 From: jose-carlos-sousa <2409jmsousa@gmail.com> Date: Sun, 25 Feb 2024 21:55:58 +0000 Subject: [PATCH] now only min is optional for freelance max always required --- src/api/middleware/validators/offer.js | 1 - src/models/Offer.js | 13 ++++++++++- test/end-to-end/offer.js | 8 +++---- test/offer_schema.js | 31 ++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/api/middleware/validators/offer.js b/src/api/middleware/validators/offer.js index 8c7d0507..1f0b8141 100644 --- a/src/api/middleware/validators/offer.js +++ b/src/api/middleware/validators/offer.js @@ -88,7 +88,6 @@ export const create = useExpressValidators([ body("jobMaxDuration", ValidationReasons.DEFAULT) - .if((value, { req }) => req.body.jobType !== "FREELANCE") .exists().withMessage(ValidationReasons.REQUIRED).bail() .isInt().withMessage(ValidationReasons.INT).bail() .custom(jobMaxDurationGreaterOrEqualThanJobMinDuration), diff --git a/src/models/Offer.js b/src/models/Offer.js index 054ee8c5..fa0cee66 100644 --- a/src/models/Offer.js +++ b/src/models/Offer.js @@ -32,15 +32,20 @@ const OfferSchema = new Schema({ }, jobMinDuration: { - type: Number + type: Number, + required: validateMinDuration, }, + jobMaxDuration: { + required: true, type: Number, validate: [ validateJobMaxDuration, "`jobMaxDuration` must be larger than `jobMinDuration`", + ], }, + jobStartDate: { type: Date }, description: { type: String, @@ -133,9 +138,15 @@ export function validatePublishEndDateLimit(publishDate, publishEndDate) { // jobMaxDuration must be larger than jobMinDuration function validateJobMaxDuration(value) { + if (this.jobType === "FREELANCE" && this.jobMinDuration === undefined) return true; return value >= this.jobMinDuration; } +function validateMinDuration() { + if (this.jobType === "FREELANCE") return false; + return true; +} + function validateOwnerConcurrentOffers(value) { return concurrentOffersNotExceeded(this.constructor)(value, this.publishDate, this.publishEndDate); } diff --git a/test/end-to-end/offer.js b/test/end-to-end/offer.js index 8dc40668..373e102d 100644 --- a/test/end-to-end/offer.js +++ b/test/end-to-end/offer.js @@ -241,11 +241,9 @@ describe("Offer endpoint tests", () => { }); describe("jobMaxDuration", () => { const FieldValidatorTester = BodyValidatorTester("jobMaxDuration"); - if (BodyValidatorTester("jobType") !== "freelance") { - FieldValidatorTester.isRequired(); - FieldValidatorTester.mustBeNumber(); - FieldValidatorTester.mustBeGreaterThanOrEqualToField("jobMinDuration"); - } + FieldValidatorTester.isRequired(); + FieldValidatorTester.mustBeNumber(); + FieldValidatorTester.mustBeGreaterThanOrEqualToField("jobMinDuration"); }); describe("jobStartDate", () => { const FieldValidatorTester = BodyValidatorTester("jobStartDate"); diff --git a/test/offer_schema.js b/test/offer_schema.js index dbc1949b..1694974b 100644 --- a/test/offer_schema.js +++ b/test/offer_schema.js @@ -129,6 +129,37 @@ describe("# Offer Schema tests", () => { }); }); + test("'jobMinDuration' is required if type offer different than freelance", async () => { + const offer = new Offer({}); + try { + await offer.validate(); + } catch (err) { + expect(err.errors.jobMinDuration).toBeDefined(); + expect(err.errors.jobMinDuration).toHaveProperty("kind", "required"); + expect(err.errors.jobMinDuration).toHaveProperty("message", "Path `jobMinDuration` is required."); + } + }); + + test("'jobMinDuration' is not required for 'FREELANCE' job type", async () => { + const offer = new Offer({ jobType: "FREELANCE" }); + try { + await offer.validate(); + } catch (err) { + expect(err.errors.jobMinDuration).toBeFalsy(); + } + }); + + test("'jobMaxDuration' is required", async () => { + const offer = new Offer({}); + try { + await offer.validate(); + } catch (err) { + expect(err.errors.jobMaxDuration).toBeDefined(); + expect(err.errors.jobMaxDuration).toHaveProperty("kind", "required"); + expect(err.errors.jobMaxDuration).toHaveProperty("message", "Path `jobMaxDuration` is required."); + } + }); + describe("required using custom validators (checking for array lengths, etc)", () => { describe(`'fields' must have between ${MIN_FIELDS} and ${MAX_FIELDS} values`, () => { test("Below minimum should throw error", async () => {