From 8c39d32f1b5ab5be77e2410dbaa9b3ea0d4e1e7f Mon Sep 17 00:00:00 2001 From: halilturkoglu Date: Sun, 12 Feb 2023 10:08:03 +0100 Subject: [PATCH 1/2] Tum post endpointlere validation eklendic --- routes/controllers/iletisim.js | 11 ++++++++++- routes/controllers/yardim.js | 11 ++++++++++- routes/controllers/yardimEt.js | 11 ++++++++++- routes/controllers/yardimEtKaydi.js | 12 +++++++++++- routes/controllers/yardimKaydi.js | 12 +++++++++++- routes/utils.js | 8 ++++++++ 6 files changed, 60 insertions(+), 5 deletions(-) diff --git a/routes/controllers/iletisim.js b/routes/controllers/iletisim.js index fd1c5e7..329b27e 100644 --- a/routes/controllers/iletisim.js +++ b/routes/controllers/iletisim.js @@ -1,4 +1,4 @@ -const { checkConnection } = require("../utils"); +const { checkConnection, validateModel} = require("../utils"); const Iletisim = require("../../models/iletisimModel"); const check = new (require("../../lib/Check"))(); @@ -53,6 +53,15 @@ module.exports = async function (fastifyInstance) { ip: req.ip, }); + try { + await validateModel(newIletisim); + } catch (e) { + res.statusCode = 400; + return { + error: e.message, + }; + } + await newIletisim.save(); return { message: "İletişim talebiniz başarıyla alındı" }; }, diff --git a/routes/controllers/yardim.js b/routes/controllers/yardim.js index 151213e..29d9770 100644 --- a/routes/controllers/yardim.js +++ b/routes/controllers/yardim.js @@ -1,6 +1,6 @@ const cache = require("../../cache"); const check = new (require("../../lib/Check"))(); -const { checkConnection } = require("../utils"); +const { checkConnection, validateModel} = require("../utils"); const Yardim = require("../../models/yardimModel"); const YardimKaydi = require("../../models/yardimKaydiModel"); const xss = require("xss"); @@ -188,6 +188,15 @@ module.exports = async function (fastifyInstance) { fields: fields || {}, }); + try { + await validateModel(newYardim); + } catch (e) { + res.statusCode = 400; + return { + error: e.message, + }; + } + cache.getCache().flushAll(); await newYardim.save(); return { message: "Yardım talebiniz başarıyla alındı" }; diff --git a/routes/controllers/yardimEt.js b/routes/controllers/yardimEt.js index 970edae..0523d70 100644 --- a/routes/controllers/yardimEt.js +++ b/routes/controllers/yardimEt.js @@ -1,6 +1,6 @@ const cache = require("../../cache"); const check = new (require("../../lib/Check"))(); -const { checkConnection } = require("../utils"); +const { checkConnection, validateModel } = require("../utils"); const YardimEt = require("../../models/yardimEtModel"); const YardimKaydi = require("../../models/yardimKaydiModel"); @@ -89,6 +89,15 @@ module.exports = async function (fastifyInstance) { ip: req.ip, }); + try { + await validateModel(newYardim); + } catch (e) { + res.statusCode = 400; + return { + error: e.message, + }; + } + cache.getCache().flushAll(); await newYardim.save(); return { message: "Yardım talebiniz başarıyla alındı" }; diff --git a/routes/controllers/yardimEtKaydi.js b/routes/controllers/yardimEtKaydi.js index 6a0e8ad..d7ed1cd 100644 --- a/routes/controllers/yardimEtKaydi.js +++ b/routes/controllers/yardimEtKaydi.js @@ -1,4 +1,4 @@ -const { checkConnection } = require("../utils"); +const { checkConnection, validateModel} = require("../utils"); const YardimEt = require("../../models/yardimEtModel"); const check = new (require("../../lib/Check"))(); const YardimKaydi = require("../../models/yardimKaydiModel"); @@ -49,6 +49,16 @@ module.exports = async function (fastifyInstance) { email: req.body.email || "", aciklama: req.body.aciklama || "", }); + + try { + await validateModel(newYardimKaydi); + } catch (e) { + res.statusCode = 400; + return { + error: e.message, + }; + } + cache.getCache().flushAll(); await newYardimKaydi.save(); } else { diff --git a/routes/controllers/yardimKaydi.js b/routes/controllers/yardimKaydi.js index 7032866..10aa096 100644 --- a/routes/controllers/yardimKaydi.js +++ b/routes/controllers/yardimKaydi.js @@ -1,4 +1,4 @@ -const { checkConnection } = require("../utils"); +const { checkConnection, validateModel} = require("../utils"); const Yardim = require("../../models/yardimModel"); const check = new (require("../../lib/Check"))(); const YardimKaydi = require("../../models/yardimKaydiModel"); @@ -49,6 +49,16 @@ module.exports = async function (fastifyInstance) { email: req.body.email || "", aciklama: req.body.aciklama || "", }); + + try { + await validateModel(newYardimKaydi); + } catch (e) { + res.statusCode = 400; + return { + error: e.message, + }; + } + cache.getCache().flushAll(); await newYardimKaydi.save(); } else { diff --git a/routes/utils.js b/routes/utils.js index 401c934..9947c26 100644 --- a/routes/utils.js +++ b/routes/utils.js @@ -8,6 +8,14 @@ async function checkConnection(fastifyInstance) { } } +async function validateModel(model) { + let validationSonuc = await model.validate() + if (validationSonuc && validationSonuc.errors) { + throw new Error(validationSonuc.message); + } +} + module.exports = { checkConnection, + validateModel }; From 9c5cc41045294ae8233d15180709d1892f55546c Mon Sep 17 00:00:00 2001 From: halilturkoglu Date: Sun, 12 Feb 2023 10:09:25 +0100 Subject: [PATCH 2/2] Lint calistirildi --- routes/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/utils.js b/routes/utils.js index 9947c26..9b34435 100644 --- a/routes/utils.js +++ b/routes/utils.js @@ -10,7 +10,7 @@ async function checkConnection(fastifyInstance) { async function validateModel(model) { let validationSonuc = await model.validate() - if (validationSonuc && validationSonuc.errors) { + if (validationSonuc?.errors) { throw new Error(validationSonuc.message); } }