1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/admin-api/util.js
Christopher Kolstad 43801f1f13
Add Tags and tag types
- First iteration of api for tags and tag-types
- Documentation in place
- Adds three new tables
   - tag_types
   - tags
   - feature_tag
- Tagging a feature is adding a row in the feature_tag
  join table

* #665

Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
2021-01-14 13:09:05 +01:00

52 lines
1.4 KiB
JavaScript

'use strict';
const joi = require('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);
// eslint-disable-next-line no-param-reassign
error.isJoi = true;
switch (error.name) {
case 'NotFoundError':
return res.status(404).end();
case 'InvalidOperationError':
case 'NameExistsError':
return res
.status(409)
.json(error)
.end();
case 'ValidationError':
return res
.status(400)
.json(error)
.end();
default:
logger.error('Server failed executing request', error);
return res.status(500).end();
}
};
module.exports = { customJoi, nameType, handleErrors };