Skip to content

Commit

Permalink
added validator in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-carlos-sousa committed Apr 17, 2024
1 parent 09eb81e commit 700be2a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/api/middleware/validators/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {

Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/api/middleware/validators/validationReasons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 700be2a

Please sign in to comment.