Skip to content

Commit

Permalink
now only min is optional for freelance max always required
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-carlos-sousa committed Feb 25, 2024
1 parent 14c8def commit e2ddeb8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/api/middleware/validators/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 12 additions & 1 deletion src/models/Offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 3 additions & 5 deletions test/end-to-end/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
31 changes: 31 additions & 0 deletions test/offer_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit e2ddeb8

Please sign in to comment.