aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/service/configPOST.js
diff options
context:
space:
mode:
authorZephyrrus <[email protected]>2021-06-17 16:06:53 +0300
committerZephyrrus <[email protected]>2021-06-17 16:06:53 +0300
commit0cae7e9eda3b62c17cfa7ec620913f4a504bc5ee (patch)
tree8573814b4c86f03390cf8e641a03bc8dc7572ec0 /src/api/routes/service/configPOST.js
parentfeat: show setting values on the settings page and implement sending to backe... (diff)
downloadhost.fuwn.me-0cae7e9eda3b62c17cfa7ec620913f4a504bc5ee.tar.xz
host.fuwn.me-0cae7e9eda3b62c17cfa7ec620913f4a504bc5ee.zip
feat: show validation errors from joi on the frontend
Diffstat (limited to 'src/api/routes/service/configPOST.js')
-rw-r--r--src/api/routes/service/configPOST.js27
1 files changed, 22 insertions, 5 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 });
}
}