From 75ef99d2e3cc8d30531b45be0397fdc29aa74a0e Mon Sep 17 00:00:00 2001 From: Richard Zhang Date: Sat, 29 Dec 2018 18:46:20 -0500 Subject: [PATCH] change to enum validation --- middlewares/validators/account.validator.js | 7 +- middlewares/validators/hacker.validator.js | 5 +- middlewares/validators/validator.helper.js | 71 --------------------- 3 files changed, 7 insertions(+), 76 deletions(-) diff --git a/middlewares/validators/account.validator.js b/middlewares/validators/account.validator.js index aa788dfc..b12bbcdc 100644 --- a/middlewares/validators/account.validator.js +++ b/middlewares/validators/account.validator.js @@ -1,5 +1,6 @@ "use strict"; const VALIDATOR = require("./validator.helper"); +const Constants = require("../../constants/general.constant"); module.exports = { newAccountValidator: [ @@ -8,7 +9,7 @@ module.exports = { VALIDATOR.pronounValidator("body", "pronoun", false), VALIDATOR.emailValidator("body", "email", false), VALIDATOR.alphaArrayValidator("body", "dietaryRestrictions", false), - VALIDATOR.shirtSizeValidator("body", "shirtSize", false), + VALIDATOR.enumValidator("body", "shirtSize", Constants.SHIRT_SIZES, false), VALIDATOR.passwordValidator("body", "password", false), VALIDATOR.jwtValidator("param", "token", process.env.JWT_CONFIRM_ACC_SECRET, true), VALIDATOR.dateValidator("body", "birthDate", false), @@ -20,12 +21,12 @@ module.exports = { VALIDATOR.pronounValidator("body", "pronoun", true), VALIDATOR.emailValidator("body", "email", true), VALIDATOR.alphaArrayValidator("body", "dietaryRestrictions", true), - VALIDATOR.shirtSizeValidator("body", "shirtSize", true), + VALIDATOR.enumValidator("body", "shirtSize", Constants.SHIRT_SIZES, true), VALIDATOR.dateValidator("body", "birthDate", true), VALIDATOR.phoneNumberValidator("body", "phoneNumber", true) ], inviteAccountValidator: [ VALIDATOR.emailValidator("body", "email", false), - VALIDATOR.accountTypeValidator("body", "accountType", false) + VALIDATOR.enumValidator("body", "accountType", Constants.EXTENDED_USER_TYPES, false) ] }; \ No newline at end of file diff --git a/middlewares/validators/hacker.validator.js b/middlewares/validators/hacker.validator.js index bf90c54f..56c4aafd 100644 --- a/middlewares/validators/hacker.validator.js +++ b/middlewares/validators/hacker.validator.js @@ -1,5 +1,6 @@ "use strict"; const VALIDATOR = require("./validator.helper"); +const Constants = require("../../constants/general.constant"); module.exports = { newHackerValidator: [ @@ -29,10 +30,10 @@ module.exports = { VALIDATOR.booleanValidator("body", "needsBus", true) ], updateStatusValidator: [ - VALIDATOR.hackerStatusValidator("body", "status", false) + VALIDATOR.enumValidator("body", "status", Constants.HACKER_STATUSES, false), ], checkInStatusValidator: [ - VALIDATOR.hackerCheckInStatusValidator("body", "status", false) + VALIDATOR.enumValidator("body", "status", Constants.HACKER_STATUS_CHECKED_IN, false) ], uploadResumeValidator: [ VALIDATOR.mongoIdValidator("param", "id", false) diff --git a/middlewares/validators/validator.helper.js b/middlewares/validators/validator.helper.js index 86fd6d84..997671c2 100644 --- a/middlewares/validators/validator.helper.js +++ b/middlewares/validators/validator.helper.js @@ -256,25 +256,6 @@ function alphaArrayValidationHelper(value) { return true; } -/** - * Validates that field must be one of the shirt size enums. - * @param {"query" | "body" | "header" | "param"} fieldLocation the location where the field should be found - * @param {string} fieldname name of the field that needs to be validated. - * @param {boolean} optional whether the field is optional or not. - */ -function shirtSizeValidator(fieldLocation, fieldname, optional = true) { - const size = setProperValidationChainBuilder(fieldLocation, fieldname, "invalid shirt size"); - const shirtSizes = ["XS", "S", "M", "L", "XL", "XXL"]; - - if (optional) { - return size.optional({ - checkFalsy: true - }).isIn(shirtSizes).withMessage(`Must be one of ${shirtSizes.join(",")}`); - } else { - return size.exists().withMessage("shirt size must exist").isIn(shirtSizes).withMessage(`must be one of ${shirtSizes.join(",")}`); - } -} - /** * Validates that field must be at least 6 characters long. * TODO: impose better restrictions. @@ -297,35 +278,6 @@ function passwordValidator(fieldLocation, fieldname, optional = true) { } } -/** - * Validates that field must be one of the status enums. - * @param {"query" | "body" | "header" | "param"} fieldLocation the location where the field should be found - * @param {string} fieldname name of the field that needs to be validated. - * @param {boolean} optional whether the field is optional or not. - */ -function hackerStatusValidator(fieldLocation, fieldname, optional = true) { - const status = setProperValidationChainBuilder(fieldLocation, fieldname, "invalid status"); - - if (optional) { - return status.optional({ - checkFalsy: true - }).isIn(Constants.HACKER_STATUSES).withMessage(`Status must be in ${Constants.HACKER_STATUSES}`); - } else { - return status.exists().withMessage(`Status must be in ${Constants.HACKER_STATUSES}`); - } -} - -function hackerCheckInStatusValidator(fieldLocation, fieldname, optional = true) { - const status = setProperValidationChainBuilder(fieldLocation, fieldname, "invalid status"); - - if (optional) { - return status.optional({ - checkFalsy: true - }).isIn(Constants.HACKER_STATUS_CHECKED_IN).withMessage(`Status must be ${Constants.HACKER_STATUS_CHECKED_IN}`); - } else { - return status.exists().withMessage(`Status must be ${Constants.HACKER_STATUS_CHECKED_IN}`); - } -} /** * Validates that field must be a valid application. @@ -588,25 +540,6 @@ function phoneNumberValidator(fieldLocation, fieldname, optional = true) { } } -/** - * Validates that field must be a valid account type - * @param {"query" | "body" | "header" | "param"} fieldLocation the location where the field should be found - * @param {string} fieldname name of the field that needs to be validated. - * @param {boolean} optional whether the field is optional or not. - */ -function accountTypeValidator(fieldLocation, fieldname, optional = true) { - const accountType = setProperValidationChainBuilder(fieldLocation, fieldname, "Invalid account type"); - if (optional) { - return accountType.optional({ - checkFalsy: true - }).isIn(Constants.EXTENDED_USER_TYPES); - } else { - return accountType.exists() - .withMessage("Account type must be provided") - .isIn(Constants.EXTENDED_USER_TYPES); - } -} - /** * Validates that field must be an array of routes * @param {"query" | "body" | "header" | "param"} fieldLocation the location where the field should be found @@ -731,9 +664,7 @@ module.exports = { emailValidator: emailValidator, alphaValidator: alphaValidator, alphaArrayValidator: alphaArrayValidator, - shirtSizeValidator: shirtSizeValidator, passwordValidator: passwordValidator, - hackerStatusValidator: hackerStatusValidator, booleanValidator: booleanValidator, applicationValidator: applicationValidator, jwtValidator: jwtValidator, @@ -743,8 +674,6 @@ module.exports = { searchSortValidator: searchSortValidator, phoneNumberValidator: phoneNumberValidator, dateValidator: dateValidator, - hackerCheckInStatusValidator: hackerCheckInStatusValidator, - accountTypeValidator: accountTypeValidator, enumValidator: enumValidator, routesValidator: routesValidator, }; \ No newline at end of file