mirror of https://github.com/xfarrow/blink
adding filter validator
This commit is contained in:
parent
4f92f2dab5
commit
b7144f1adb
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue