Skip to content

Instantly share code, notes, and snippets.

@depoulo
Last active March 4, 2016 14:49
Show Gist options
  • Save depoulo/5718954bbe5568201b2f to your computer and use it in GitHub Desktop.
Save depoulo/5718954bbe5568201b2f to your computer and use it in GitHub Desktop.
express middleware that throws if the specified request parameters are missing
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