Skip to content

Commit

Permalink
Expanded the hasId function to include the ability to not check for o…
Browse files Browse the repository at this point in the history
…ptional Ids
  • Loading branch information
MKHenson committed May 17, 2016
1 parent 4c8022c commit 7804994
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions server/dist/src/permission-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ exports.getUser = getUser;
* Checks for an id parameter and that its a valid mongodb ID. Returns an error of type IResponse if no ID is detected, or its invalid
* @param {string} idName The name of the ID to check for
* @param {string} rejectName The textual name of the ID when its rejected
* @param {boolean} optional If true, then an error wont be thrown if it doesnt exist
*/
function hasId(idName, rejectName) {
function hasId(idName, rejectName, optional = false) {
return function (req, res, next) {
// Make sure the id
if (!req.params[idName]) {
if (!req.params[idName] && !optional) {
res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify({
error: true,
message: "Please specify an " + idName
}));
}
else if (!mongodb.ObjectID.isValid(req.params[idName])) {
else if (req.params[idName] && !mongodb.ObjectID.isValid(req.params[idName])) {
res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify({
error: true,
Expand Down
7 changes: 4 additions & 3 deletions server/src/permission-controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ export function getUser(req: express.Request, res: express.Response, next: Funct
* Checks for an id parameter and that its a valid mongodb ID. Returns an error of type IResponse if no ID is detected, or its invalid
* @param {string} idName The name of the ID to check for
* @param {string} rejectName The textual name of the ID when its rejected
* @param {boolean} optional If true, then an error wont be thrown if it doesnt exist
*/
export function hasId( idName : string, rejectName : string )
export function hasId( idName : string, rejectName : string, optional: boolean = false )
{
return function( req: express.Request, res: express.Response, next: Function )
{
// Make sure the id
if (!req.params[idName])
if (!req.params[idName] && !optional)
{
res.setHeader( 'Content-Type', 'application/json');
return res.end(JSON.stringify(<IResponse>{
Expand All @@ -65,7 +66,7 @@ export function hasId( idName : string, rejectName : string )
}));
}
// Make sure the id format is correct
else if ( !mongodb.ObjectID.isValid(req.params[idName]))
else if ( req.params[idName] && !mongodb.ObjectID.isValid(req.params[idName]))
{
res.setHeader( 'Content-Type', 'application/json');
return res.end(JSON.stringify(<IResponse>{
Expand Down

0 comments on commit 7804994

Please sign in to comment.