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 5f18af1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 5f18af1

Please sign in to comment.