adding filter validator

This commit is contained in:
xfarrow 2024-03-20 10:55:03 +01:00
parent 4f92f2dab5
commit b7144f1adb
3 changed files with 18 additions and 4 deletions

View File

@ -111,7 +111,7 @@ async function remove(organizationId, requester) {
}
/**
* Gets a list of Organizations given their prefix.
* Gets a list of Organizations given the prefix of their name.
* E.g. "Can" --> "Canonical"
* @param {*} name
*/

View File

@ -163,8 +163,14 @@ async function getOrganization(req, res) {
}
}
async function filterByPrefix(req, res) {
async function filter(req, res) {
try {
const errors = organizationValidator.validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
errors: errors.array()
});
}
const organizations = await Organization.filterByPrefix(req.body.name);
return res.status(200).json(organizations).send();
} catch (error) {
@ -180,7 +186,7 @@ async function filterByPrefix(req, res) {
// that do not require the authorization, e.g. job offers
const routes = express.Router();
routes.get('/:id', organizationValidator.deleteOrGetOrganizationValidator, getOrganization);
routes.post('/filterByPrefix', filterByPrefix);
routes.post('/filter', organizationValidator.filterValidator, filter);
routes.post('/', jwtUtils.verifyToken, organizationValidator.createOrganizationValidator, createOrganization);
routes.patch('/:id', jwtUtils.verifyToken, organizationValidator.updateOrganizationValidator, updateOrganization);
routes.delete('/:id', jwtUtils.verifyToken, organizationValidator.deleteOrGetOrganizationValidator, deleteOrganization);

View File

@ -42,9 +42,17 @@ const deleteOrGetOrganizationValidator = [
check('id').notEmpty().escape()
]
const filterValidator = [
check('name').trim().notEmpty().escape().isLength({
min: 3, // to avoid database overhead
max: 128
}).withMessage('The name must be at least 3 characters and cannot exceed 128')
]
module.exports = {
validationResult,
createOrganizationValidator,
updateOrganizationValidator,
deleteOrGetOrganizationValidator
deleteOrGetOrganizationValidator,
filterValidator
}