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;