From fa7971bb2bb2a11727b161e1f57188d1e817a5d3 Mon Sep 17 00:00:00 2001 From: Pau Date: Tue, 1 Apr 2025 20:36:00 +0200 Subject: [PATCH] added validators for data in route and authorization --- .../expressValidator/customValidators.js | 11 ++++++++++ .../payloadExpressValidator.js | 21 +++++++++++++++++++ src/validators/filters.js | 6 ++++++ src/validators/jwtValidators.js | 16 ++++++++++++++ src/validators/result-validators.js | 5 +++++ 5 files changed, 59 insertions(+) create mode 100644 src/validators/expressValidator/customValidators.js create mode 100644 src/validators/expressValidator/payloadExpressValidator.js create mode 100644 src/validators/filters.js create mode 100644 src/validators/jwtValidators.js create mode 100644 src/validators/result-validators.js diff --git a/src/validators/expressValidator/customValidators.js b/src/validators/expressValidator/customValidators.js new file mode 100644 index 0000000..abd0ef6 --- /dev/null +++ b/src/validators/expressValidator/customValidators.js @@ -0,0 +1,11 @@ +import { check } from 'express-validator' + +const varChar = (field, { max = 255 } = {}) => check(field).isString().trim().isLength({ min: 1, max }).withMessage(`|${field}| must be a string with a length between 1 and ${max}`) +const integer = field => check(field).isInt({min: Number.MIN_SAFE_INTEGER, max: Number.MAX_SAFE_INTEGER}).withMessage(`|${field}| must be an integer`) +const uuid = field => check(field).isUUID('all').withMessage(`|${field}| must be a valid UUID`) + +export { + integer, + uuid, + varChar, +} diff --git a/src/validators/expressValidator/payloadExpressValidator.js b/src/validators/expressValidator/payloadExpressValidator.js new file mode 100644 index 0000000..b771f37 --- /dev/null +++ b/src/validators/expressValidator/payloadExpressValidator.js @@ -0,0 +1,21 @@ +import { validationResult } from 'express-validator' +import { errorHandler } from '../../utils/errors.js' + +const payloadExpressValidator = (req, res, next, config) => { + const errors = validationResult(req) + if (!errors.isEmpty()) { + const badRequest = errors.array().find(val => undefined === val.value) + + if (badRequest) { + const errorMessage = errorHandler({ errors: errors.array(), code: 'BAD_REQUEST' }, config.environment) + return res.status(errorMessage.code).json(errorMessage) + } + + const error = errorHandler({ errors: errors.array(), code: 'UNPROCESSABLE_ENTITY' }, config.environment) + return res.status(error.code).json(error) + } + + next() +} + +export { payloadExpressValidator } diff --git a/src/validators/filters.js b/src/validators/filters.js new file mode 100644 index 0000000..8a63e61 --- /dev/null +++ b/src/validators/filters.js @@ -0,0 +1,6 @@ +const filterByStock = (arr, min = 0, max = Number.MAX_VALUE) => { + return arr + .filter((item) => item.stock >= min && item.stock <= max) +} + +export {filterByStock} \ No newline at end of file diff --git a/src/validators/jwtValidators.js b/src/validators/jwtValidators.js new file mode 100644 index 0000000..9fd40ae --- /dev/null +++ b/src/validators/jwtValidators.js @@ -0,0 +1,16 @@ +import { sendResponseAccessDenied } from "../utils/responses" + +const validateKeyValuePairFromToken = ({ key, values, singleValidation }) => (req, res, next, config) => { + const fieldValue = ((req.auth || {}).user || {})[key] + if (fieldValue && values.includes(fieldValue)) { + return singleValidation + ? next({ error: false }) + : true + } + + return singleValidation + ? sendResponseAccessDenied({ res, config }) + : (req, res, env, next) => next({ error: true }) +} + +export { validateKeyValuePairFromToken } diff --git a/src/validators/result-validators.js b/src/validators/result-validators.js new file mode 100644 index 0000000..0fafff6 --- /dev/null +++ b/src/validators/result-validators.js @@ -0,0 +1,5 @@ +const noResults = (data) => { + return data && data.length < 1 +} + +export { noResults }