From 9919818ba24313da5cf4dda9aa23c2923219cba7 Mon Sep 17 00:00:00 2001 From: Diana Vashchilina Date: Tue, 26 Nov 2024 16:15:29 +0200 Subject: [PATCH] Fix sonar issues in src/middlewares/entityValidation.js file (#963) * refactor entity validation middleware to extract single entity validation * refactor entity validation by extracting params and body functions * refactor entity validation by adding closure function --- src/middlewares/entityValidation.js | 78 +++++++++++++++++------------ 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/middlewares/entityValidation.js b/src/middlewares/entityValidation.js index 0423ed19..a8d60ef4 100644 --- a/src/middlewares/entityValidation.js +++ b/src/middlewares/entityValidation.js @@ -1,50 +1,62 @@ const { DOCUMENT_NOT_FOUND } = require('~/consts/errors') const { createError } = require('~/utils/errorsHelper') -const isEntityValid = (entities) => { - return async (req, _res, next) => { - const models = [] +const createEntityValidator = (invalidModels) => { + return async (id, model) => { + if (!id) { + return + } - let id = null + const document = await model.findById(id) - if (entities.params?.length) { - for (const { model, idName } of entities.params) { - id = req.params[idName] + if (!document && !invalidModels.includes(model.modelName)) { + invalidModels.push(model.modelName) + } + } +} - if (!id) continue +const validateEntitiesFromParams = async (entitiesParams, reqParams, validateSingleEntity) => { + if (!entitiesParams?.length) { + return + } - const document = await model.findById(id) + for (const { model, idName } of entitiesParams) { + const id = reqParams[idName] - if (!document && !models.includes(model.modelName)) models.push(model.modelName) - } - } + await validateSingleEntity(id, model) + } +} - if (entities.body?.length) { - for (const { model, idName } of entities.body) { - if (Array.isArray(req.body[idName])) { - await Promise.all( - req.body[idName].map(async (id) => { - const document = await model.findById(id) +const validateEntitiesFromBody = async (entitiesBody, reqBody, validateSingleEntity) => { + if (!entitiesBody?.length) { + return + } - if (!document && !models.includes(model.modelName)) { - models.push(model.modelName) - } - }) - ) - } else { - id = req.body[idName] + for (const { model, idName } of entitiesBody) { + if (Array.isArray(reqBody[idName])) { + await Promise.all( + reqBody[idName].map(async (id) => { + await validateSingleEntity(id, model) + }) + ) + return + } - if (!id) continue + const id = reqBody[idName] + await validateSingleEntity(id, model) + } +} - const document = await model.findById(id) +const isEntityValid = (entities) => { + return async (req, _res, next) => { + const invalidModels = [] - if (!document && !models.includes(model.modelName)) models.push(model.modelName) - } - } - } + await validateEntitiesFromParams(entities.params, req.params, createEntityValidator(invalidModels)) + + await validateEntitiesFromBody(entities.body, req.body, createEntityValidator(invalidModels)) - if (models.length) { - next(createError(404, DOCUMENT_NOT_FOUND(models))) + if (invalidModels.length) { + next(createError(404, DOCUMENT_NOT_FOUND(invalidModels))) } next()