From e6b11fee5fb25a29e42ea55100c4f87cc8f95cf2 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sun, 8 Jan 2023 03:25:12 +0100 Subject: [PATCH] Updated the validateSchema util --- server/controller/module.js | 2 +- server/routes/auth.js | 4 ++-- server/routes/domain.js | 4 ++-- server/routes/link.js | 12 ++++++------ server/util/validate.js | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/server/controller/module.js b/server/controller/module.js index b6ded77..9e862eb 100644 --- a/server/controller/module.js +++ b/server/controller/module.js @@ -10,7 +10,7 @@ module.exports.initialize = () => fs.readdirSync(path.join(process.cwd(), 'modul const currentModule = require(process.cwd() + `/modules/${moduleName}/module`); const moduleError = this.isValidModule(currentModule.info); - if (moduleError) return console.log(`Could not load module ${moduleName}: ${moduleError}`); + if (moduleError) return console.log(`Could not load module ${moduleName}: ${moduleError.message}`); modules[moduleName] = currentModule; console.log(`Module ${moduleName} has been loaded`); diff --git a/server/routes/auth.js b/server/routes/auth.js index 6187aaf..2d147de 100644 --- a/server/routes/auth.js +++ b/server/routes/auth.js @@ -7,7 +7,7 @@ const {authUser} = require("../controller/user"); app.post("/login", async (req, res) => { const error = await validateSchema(authValidation, req.body); - if (error) return res.status(400).json({message: error}); + if (error) return res.status(400).json(error); let user = await authUser(req.body.username, req.body.password); if (!user) return res.status(401).json({message: "Please use the correct username / password combination"}); @@ -19,4 +19,4 @@ app.post("/login", async (req, res) => { res.json({message: "Successfully logged in"}); }); -module.exports = app; \ No newline at end of file +module.exports = app; diff --git a/server/routes/domain.js b/server/routes/domain.js index c10d3d0..b254196 100644 --- a/server/routes/domain.js +++ b/server/routes/domain.js @@ -9,7 +9,7 @@ app.get("/", async (req, res) => { app.put("/", async (req, res) => { const error = await validateSchema(domainValidation, req.body); - if (error) return res.status(400).json({message: error}); + if (error) return res.status(400).json(error); const domain = await createDomain(req.body.domainName); if (domain === null) return res.status(409).json({message: "This domain already exists"}); @@ -24,4 +24,4 @@ app.delete("/:domain", async (req, res) => { res.json({message: "The provided domain has been successfully removed"}); }); -module.exports = app; \ No newline at end of file +module.exports = app; diff --git a/server/routes/link.js b/server/routes/link.js index 2fd0562..320eb85 100644 --- a/server/routes/link.js +++ b/server/routes/link.js @@ -14,7 +14,7 @@ const app = require('express').Router(); app.get("/:domain/list", async (req, res) => { const error = await validateSchema(listLinksValidation, req.query); - if (error) return res.status(400).json({message: error}); + if (error) return res.status(400).json(error); let links = await listLinks(req.params.domain, req.query); if (!links) return res.status(400).json({message: "Could not use the provided filter"}); @@ -38,7 +38,7 @@ app.get("/:id", async (req, res) => { app.put("/", async (req, res) => { const error = await validateSchema(createLinkValidation, req.body); - if (error) return res.status(400).json({message: error}); + if (error) return res.status(400).json(error); const module = getModule(req.body.type); if (!module) return res.status(404).json({message: "The provided module does not exist"}); @@ -47,7 +47,7 @@ app.put("/", async (req, res) => { return res.status(409).json({message: "The provided access id already exists"}); const moduleError = await validateSchema(module.info.validationSchema, req.body.meta); - if (moduleError) return res.status(400).json({message: moduleError}); + if (moduleError) return res.status(400).json(moduleError); let link = await createLink({...req.body, creatorId: req.user.id}); @@ -56,7 +56,7 @@ app.put("/", async (req, res) => { app.patch("/:id", async (req, res) => { const error = await validateSchema(editLinkValidation, req.body); - if (error) return res.status(400).json({message: error}); + if (error) return res.status(400).json(error); const currentLink = await getLinkById(req.params.id); if (currentLink === null) return res.status(409).json({message: "The provided link does not exist"}); @@ -66,7 +66,7 @@ app.patch("/:id", async (req, res) => { if (!module) return res.status(404).json({message: "The provided module does not exist"}); const moduleError = await validateSchema(module.info.validationSchema, req.body.meta); - if (req.body.meta && moduleError) return res.status(400).json({message: moduleError}); + if (req.body.meta && moduleError) return res.status(400).json(moduleError); } if (req.body.accessId || req.body.domainName) { @@ -86,4 +86,4 @@ app.delete("/:id", async (req, res) => { res.json({message: "Link successfully deleted"}); }); -module.exports = app; \ No newline at end of file +module.exports = app; diff --git a/server/util/validate.js b/server/util/validate.js index 0e3a293..830384c 100644 --- a/server/util/validate.js +++ b/server/util/validate.js @@ -1,5 +1,5 @@ module.exports.validateSchema = (schema, object) => { const {error} = schema.validate(object, {errors: {wrap: {label: ''}}}); - return error ? error.details[0].message : undefined; -} \ No newline at end of file + return error ? {message: error.details[0].message, field: error.details[0].path[0]}: undefined; +}