More granular error responses

This commit is contained in:
Tom Moor
2018-02-19 23:31:18 -08:00
parent d73196594d
commit 5b6c908215
11 changed files with 67 additions and 68 deletions

View File

@ -1,43 +1,43 @@
// @flow
import apiError from '../../errors';
import validator from 'validator';
import { ParamRequiredError, ValidationError } from '../../errors';
import { validateColorHex } from '../../../shared/utils/color';
export default function validation() {
return function validationMiddleware(ctx: Object, next: Function) {
ctx.assertPresent = function assertPresent(value, message) {
ctx.assertPresent = (value, message) => {
if (value === undefined || value === null || value === '') {
throw apiError(400, 'validation_error', message);
throw new ParamRequiredError(message);
}
};
ctx.assertNotEmpty = function assertNotEmpty(value, message) {
ctx.assertNotEmpty = (value, message) => {
if (value === '') {
throw apiError(400, 'validation_error', message);
throw new ValidationError(message);
}
};
ctx.assertEmail = (value, message) => {
if (!validator.isEmail(value)) {
throw apiError(400, 'validation_error', message);
throw new ValidationError(message);
}
};
ctx.assertUuid = (value, message) => {
if (!validator.isUUID(value)) {
throw apiError(400, 'validation_error', message);
throw new ValidationError(message);
}
};
ctx.assertPositiveInteger = (value, message) => {
if (!validator.isInt(value, { min: 0 })) {
throw apiError(400, 'validation_error', message);
throw new ValidationError(message);
}
};
ctx.assertHexColor = (value, message) => {
if (!validateColorHex(value)) {
throw apiError(400, 'validation_error', message);
throw new ValidationError(message);
}
};