1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/routes/admin-api/util.js
Ivar Conradi Østhus d01c9d2dac
fix: use airbnb lint rules directly (#583)
This drops usage of finn-eslint rules as they are no
longer maintained.
2020-04-14 22:29:11 +02:00

47 lines
1.2 KiB
JavaScript

'use strict';
const joi = require('@hapi/joi');
const customJoi = joi.extend(j => ({
type: 'isUrlFriendly',
base: j.string(),
messages: {
'isUrlFriendly.base': '{{#label}} must be URL friendly',
},
validate(value, helpers) {
// Base validation regardless of the rules applied
if (encodeURIComponent(value) !== value) {
// Generate an error, state and options need to be passed
return { value, errors: helpers.error('isUrlFriendly.base') };
}
return undefined;
},
}));
const nameType = customJoi
.isUrlFriendly()
.min(2)
.max(100)
.required();
const handleErrors = (res, logger, error) => {
logger.warn(error.message);
switch (error.name) {
case 'NotFoundError':
return res.status(404).end();
case 'NameExistsError':
case 'ValidationError':
// eslint-disable-next-line no-param-reassign
error.isJoi = true;
return res
.status(400)
.json(error)
.end();
default:
logger.error('Server failed executing request', error);
return res.status(500).end();
}
};
module.exports = { nameType, handleErrors };