implemented validator to validate beneficiary details
This commit is contained in:
33
src/validators/beneficiary.validator.js
Normal file
33
src/validators/beneficiary.validator.js
Normal file
@@ -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;
|
Reference in New Issue
Block a user