Skip to content

Commit

Permalink
Finished input validation for company application search
Browse files Browse the repository at this point in the history
  • Loading branch information
Naapperas committed Mar 28, 2023
1 parent 9c75278 commit c2a11e5
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 385 deletions.
34 changes: 24 additions & 10 deletions src/api/middleware/validators/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export const reject = useExpressValidators([
.withMessage(ValidationReasons.TOO_SHORT(CompanyApplicationConstants.rejectReason.min_length)),
]);

const isAfterSubmissionDateFrom = (submissionDateTo, { req }) => {

const { submissionDateFrom } = req.body;

return submissionDateFrom <= submissionDateTo;
};

const sortByParamValidator = (val) => {

const regex = /^(\w+(:(desc|asc))?)(,\w+(:(desc|asc))?)*$/;
Expand All @@ -84,31 +91,38 @@ const parseSortByField = (val) => val.split(",");
export const search = useExpressValidators([
query("limit", ValidationReasons.DEFAULT)
.optional()
.isInt({ min: 1, max: MAX_LIMIT_RESULTS })
.withMessage(ValidationReasons.MAX(MAX_LIMIT_RESULTS)),
.isInt().withMessage(ValidationReasons.INT).bail()
.toInt()
.isInt({ min: 1 }).withMessage(ValidationReasons.MIN(1)).bail()
.isInt({ max: MAX_LIMIT_RESULTS }).withMessage(ValidationReasons.MAX(MAX_LIMIT_RESULTS)).bail()
.toInt(),
query("offset", ValidationReasons.DEFAULT)
.optional()
.isInt({ min: 0 })
.withMessage(ValidationReasons.MIN(0)),
.isInt().withMessage(ValidationReasons.INT).bail()
.toInt()
.isInt({ min: 0 }).withMessage(ValidationReasons.MIN(0)).bail()
.toInt(),
query("companyName", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING),
.isString().withMessage(ValidationReasons.STRING).bail(),
query("state", ValidationReasons.DEFAULT)
.optional()
.customSanitizer(ensureArray)
.isArray().withMessage(ValidationReasons.ARRAY).bail()
.customSanitizer(ensureArray)
.custom(valuesInSet(Object.keys(ApplicationStatus))),
query("submissionDateFrom", ValidationReasons.DEFAULT)
.optional()
.toDate()
.isISO8601().withMessage(ValidationReasons.DATE),
.isISO8601().withMessage(ValidationReasons.DATE).bail()
.toDate(),
query("submissionDateTo", ValidationReasons.DEFAULT)
.optional()
.isISO8601().withMessage(ValidationReasons.DATE).bail()
.toDate()
.isISO8601().withMessage(ValidationReasons.DATE),
.if((submissionDateTo, { req }) => req.query.submissionDateFrom !== undefined)
.custom(isAfterSubmissionDateFrom).withMessage(ValidationReasons.MUST_BE_AFTER("submissionDateFrom")),
query("sortBy", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING)
.isString().withMessage(ValidationReasons.STRING).bail()
.custom(sortByParamValidator)
.customSanitizer(parseSortByField),
]);
9 changes: 4 additions & 5 deletions src/api/middleware/validators/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export const create = useExpressValidators([
.custom(publishEndDateAfterPublishDate)
.custom(publishEndDateLimit),


body("jobMinDuration", ValidationReasons.DEFAULT)
.exists().withMessage(ValidationReasons.REQUIRED).bail()
.isInt().withMessage(ValidationReasons.INT),
Expand Down Expand Up @@ -119,7 +118,7 @@ export const create = useExpressValidators([
body("jobType", ValidationReasons.DEFAULT)
.exists().withMessage(ValidationReasons.REQUIRED).bail()
.isString().withMessage(ValidationReasons.STRING).bail()
.isIn(JobTypes).withMessage(ValidationReasons.IN_ARRAY(JobTypes)),
.isIn(JobTypes).withMessage((value) => ValidationReasons.IN_ARRAY(JobTypes, value)),

body("fields", ValidationReasons.DEFAULT)
.exists().withMessage(ValidationReasons.REQUIRED).bail()
Expand Down Expand Up @@ -388,7 +387,7 @@ export const edit = useExpressValidators([
body("jobType", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING).bail()
.isIn(JobTypes).withMessage(ValidationReasons.IN_ARRAY(JobTypes)),
.isIn(JobTypes).withMessage((value) => ValidationReasons.IN_ARRAY(JobTypes, value)),

body("fields", ValidationReasons.DEFAULT)
.optional()
Expand Down Expand Up @@ -495,7 +494,7 @@ export const get = useExpressValidators([
query("jobType")
.optional()
.isString().withMessage(ValidationReasons.STRING).bail()
.isIn(JobTypes).withMessage(ValidationReasons.IN_ARRAY(JobTypes)),
.isIn(JobTypes).withMessage((value) => ValidationReasons.IN_ARRAY(JobTypes, value)),

query("jobMinDuration", ValidationReasons.DEFAULT)
.optional()
Expand All @@ -522,7 +521,7 @@ export const get = useExpressValidators([
query("sortBy", ValidationReasons.DEFAULT)
.optional()
.isString().withMessage(ValidationReasons.STRING).bail()
.isIn(OfferConstants.SortableFields).withMessage(ValidationReasons.IN_ARRAY(OfferConstants.SortableFields)),
.isIn(OfferConstants.SortableFields).withMessage((value) => ValidationReasons.IN_ARRAY(OfferConstants.SortableFields, value)),

query("descending", ValidationReasons.DEFAULT)
.optional()
Expand Down
3 changes: 3 additions & 0 deletions test/end-to-end/applications/company/:id/approve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test("should return true", () => {
expect(true).toBe(true);
});
3 changes: 3 additions & 0 deletions test/end-to-end/applications/company/:id/reject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test("should return true", () => {
expect(true).toBe(true);
});
Loading

0 comments on commit c2a11e5

Please sign in to comment.