Skip to content

Commit

Permalink
Fix sonar issues in src/middlewares/entityValidation.js file (#963)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
luiqor authored Nov 26, 2024
1 parent c95063d commit 9919818
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions src/middlewares/entityValidation.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 9919818

Please sign in to comment.