Skip to content

Commit 76ec217

Browse files
committed
allow booleanValidator to take comparator value for codeOfConduct
1 parent b220176 commit 76ec217

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

middlewares/validators/hacker.validator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
VALIDATOR.alphaArrayValidator("body", "ethnicity", false),
1414
VALIDATOR.nameValidator("body", "major", false),
1515
VALIDATOR.integerValidator("body", "graduationYear", false, 2019, 2030),
16-
VALIDATOR.booleanValidator("body", "codeOfConduct", false),
16+
VALIDATOR.booleanValidator("body", "codeOfConduct", false, true),
1717
],
1818

1919
updateConfirmationValidator: [

middlewares/validators/validator.helper.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,29 @@ function mongoIdArrayValidator(fieldLocation, fieldname, optional = true) {
103103
}
104104

105105
/**
106-
* Validates that field must be boolean.
106+
* Validates that field must be boolean. Optionally checks for desired boolean
107107
* @param {"query" | "body" | "header" | "param"} fieldLocation the location where the field should be found
108108
* @param {string} fieldname name of the field that needs to be validated.
109109
* @param {boolean} optional whether the field is optional or not.
110110
*/
111-
function booleanValidator(fieldLocation, fieldname, optional = true) {
111+
function booleanValidator(fieldLocation, fieldname, optional = true, desire = null) {
112112
const booleanField = setProperValidationChainBuilder(fieldLocation, fieldname, "invalid boolean");
113113

114114
if (optional) {
115115
// do not use check falsy option as a 'false' boolean will be skipped
116-
return booleanField.optional().isBoolean().withMessage("must be boolean");
116+
return booleanField.optional().isBoolean().withMessage("must be boolean").custom((val) => {
117+
if (desire !== null) {
118+
return desire === val;
119+
}
120+
return true;
121+
}).withMessage(`Must be equal to ${desire}`);
117122
} else {
118-
return booleanField.exists().isBoolean().withMessage("must be boolean");
123+
return booleanField.exists().isBoolean().withMessage("must be boolean").custom((val) => {
124+
if (desire !== null) {
125+
return desire === val;
126+
}
127+
return true;
128+
}).withMessage(`Must be equal to ${desire}`);
119129
}
120130
}
121131

0 commit comments

Comments
 (0)