New API responses with good errors

This commit is contained in:
Jori Lallo
2016-09-17 14:53:30 -07:00
parent e631025887
commit fcc83dd2d6
11 changed files with 86 additions and 50 deletions

View File

@ -1,23 +1,23 @@
import httpErrors from 'http-errors';
import apiError from '../../errors';
import validator from 'validator';
export default function validation() {
return function validationMiddleware(ctx, next) {
ctx.assertPresent = function assertPresent(value, message) {
if (value === undefined || value === null || value === '') {
throw httpErrors.BadRequest(message);
throw apiError(400, 'validation_error', message);
}
};
ctx.assertEmail = function assertEmail(value, message) {
if (!validator.isEmail(value)) {
throw httpErrors.BadRequest(message);
throw apiError(400, 'validation_error', message);
}
};
ctx.assertUuid = function assertUuid(value, message) {
if (!validator.isUUID(value)) {
throw httpErrors.BadRequest(message);
throw apiError(400, 'validation_error', message);
}
};