Last active
March 4, 2016 14:49
-
-
Save depoulo/5718954bbe5568201b2f to your computer and use it in GitHub Desktop.
express middleware that throws if the specified request parameters are missing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {difference} from 'lodash'; | |
export function BadRequestError(message) { | |
this.name = 'BadRequestError'; | |
this.statusCode = 400; | |
this.message = 'Bad request. ' + message; | |
} | |
BadRequestError.prototype = Object.create(Error.prototype); | |
BadRequestError.prototype.constructor = BadRequestError; | |
const requireParams = (...requiredParams) => (req, res, next) => { | |
const missingParams = difference(requiredParams, Object.keys(req.query)); | |
next(missingParams.length ? new BadRequestError('Missing parameters: ' + missingParams) : null); | |
}; | |
export default requireParams; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment