From 35bd66811697aeffe81f762023a6fcb71f4d8c5f Mon Sep 17 00:00:00 2001 From: asif Date: Thu, 7 Aug 2025 01:59:27 +0530 Subject: [PATCH] implemented validator to validate beneficiary details --- src/validators/beneficiary.validator.js | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/validators/beneficiary.validator.js diff --git a/src/validators/beneficiary.validator.js b/src/validators/beneficiary.validator.js new file mode 100644 index 0000000..1a0963c --- /dev/null +++ b/src/validators/beneficiary.validator.js @@ -0,0 +1,33 @@ +const db = require('../config/db'); + +const newBeneficiaryValidator = async (req, res, next) => { + const { accountNo, name, ifscCode, accountType } = req.body; + + if (!accountNo || !/^[0-9]{7,20}$/.test(accountNo)) { + res.status(400).json({ error: 'INVALID_ACCOUNT_NO' }); + return; + } + + if (!name || !accountType) { + res.status(400).json({ error: 'BAD_REQUEST' }); + return; + } + + if (!ifscCode || !/^[A-Z]{4}0[0-9]{6}$/.test(ifscCode)) { + res.status(400).json({ error: 'INVALID_IFSC' }); + return; + } + const query_str = + 'SELECT EXISTS(SELECT 1 FROM ifsc_code_bank WHERE ifsc_code = $1)'; + const result = await db.query(query_str, [ifscCode]); + const exists = result.rows[0].exists; + if (!exists) { + res.status(400).json({ error: 'INVALID_IFSC_CODE' }); + return; + } + + // if everthing is ok then move forward + next(); +}; + +module.exports = newBeneficiaryValidator;