Skip to content

Instantly share code, notes, and snippets.

@depoulo

depoulo/requireParams.js

Last active Mar 4, 2016
Embed
What would you like to do?
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