diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/routes/service/configPOST.js | 27 | ||||
| -rw-r--r-- | src/api/structures/Setting.js | 6 | ||||
| -rw-r--r-- | src/api/utils/Util.js | 2 |
3 files changed, 26 insertions, 9 deletions
diff --git a/src/api/routes/service/configPOST.js b/src/api/routes/service/configPOST.js index 28d034d..9129950 100644 --- a/src/api/routes/service/configPOST.js +++ b/src/api/routes/service/configPOST.js @@ -1,19 +1,36 @@ -const Joi = require('joi'); - const Route = require('../../structures/Route'); const Util = require('../../utils/Util'); const { schema } = require('../../structures/Setting'); +const joiOptions = { + abortEarly: false, // include all errors + allowUnknown: true, // ignore unknown props + stripUnknown: true // remove unknown props +}; + class configGET extends Route { constructor() { super('/service/config', 'post', { adminOnly: true }); } - run(req, res) { + async run(req, res) { const { settings } = req.body; - const validationRes = schema.validate(settings, { abortEarly: false }); - console.log(JSON.stringify(validationRes)); + const { error, value } = schema.validate(settings, joiOptions); + if (error) { + return res.status(400).json({ + errors: error.details.reduce((acc, v) => { + for (const p of v.path) { + acc[p] = (acc[p] || []).concat(v.message); + } + return acc; + }, {}) + }); + } + + await Util.writeConfigToDb(value); + + return res.status(200).json({ value }); } } diff --git a/src/api/structures/Setting.js b/src/api/structures/Setting.js index ff98339..7650ccb 100644 --- a/src/api/structures/Setting.js +++ b/src/api/structures/Setting.js @@ -116,9 +116,9 @@ const schema = Joi.object({ .description('Allows people to create new accounts'), // Social and sharing - metaThemeColor: Joi.string().hex().min(3) - .max(6) - .default('20222b') + metaThemeColor: Joi.string().pattern(/^#([0-9a-f]{6}|[0-9a-f]{3})$/i).min(4) + .max(7) + .default('#20222b') .meta({ section: Sections.SOCIAL_AND_SHARING }) diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 3780460..628be82 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -64,7 +64,7 @@ class Util { static async writeConfigToDb(config) { // TODO: Check that the config passes the joi schema validation - if (!config || !config.key || !config.key) return; + if (!config || !config.key) return; try { config.value = JSON.stringify(config.value); await db.table('settings').insert(config); |