Skip to content

Commit

Permalink
Add nz bank account validator
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanMaRuiz committed Nov 2, 2020
1 parent f366b86 commit e5355c1
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 69 deletions.
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -34,6 +34,9 @@ npm i ccc-util --save-dev
## Usage

### Passing only a CCC

Use this if you are interested in validate only the account number.

```bash
const { validate } = require('ccc-util');

Expand All @@ -48,11 +51,13 @@ validate('0049-1500-05-1234567890'); //=> false
### Passing CCC and institution name
Optionally you can pass a second argument to `validate` method, the `institution`. If passed `ccc-util` will validate the account number with the institution name. If both are valid the result will be `true`, `false` otherwise.
```bash
const { validate } = require('ccc-util');

validate('0144 9181 1115 8790 3375', { institution: 'BNP Paribas Securities Services, S.E.') //=> true
validate('0144 9181 1115 8790 3375', { institution: 'BNP Paribas Securities Services') //=> flase
validate('0144 9181 1115 8790 3375', { institution: 'BNP Paribas Securities Services') //=> false
```
### Codes of banking entities in Spain
Expand Down
16 changes: 12 additions & 4 deletions __test__/validator.test.js
Expand Up @@ -13,12 +13,20 @@ test('it should return FALSE if the passed value is not a valid CCC', () => {
});

test('it should throw an error when the "institution" option is not a string', () => {
expect( () => validate('7220199120124205847470', { institution: 2})).toThrow('Institution is not a valid string');
expect( () => validate('7220199120124205847470', { institution: []})).toThrow('Institution is not a valid string');
expect( () => validate('7220199120124205847470', { institution: true})).toThrow('Institution is not a valid string');
expect( () => validate('7220199120124205847470', { institution: {}})).toThrow('Institution is not a valid string');
expect( () => validate('7220199120124205847470', { institution: 2})).toThrow('Institution param is not a valid string');
expect( () => validate('7220199120124205847470', { institution: []})).toThrow('Institution param is not a valid string');
expect( () => validate('7220199120124205847470', { institution: true})).toThrow('Institution param is not a valid string');
expect( () => validate('7220199120124205847470', { institution: {}})).toThrow('Institution param is not a valid string');
});

test('it return TRUE when passed insitution code and codeEntity value matches', () => {
expect(validate('0144 9181 1115 8790 3375', { institution: 'BNP Paribas Securities Services, S.E.'})).toBe(true);
});

test('it return TRUE when country is "nz" and passed account is correct', () => {
expect(validate('01090200068389000', { country: 'nz'})).toBe(true);
});

test('it return FALSE when country is "nz" and passed account is incorrect', () => {
expect(validate('26485749385729888', { country: 'nz'})).toBe(false);
});
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -24,5 +24,8 @@
"type": "git",
"url": "https://github.com/JuanMaRuiz/ccc-util.git"
},
"homepage": "https://github.com/JuanMaRuiz/ccc-util"
"homepage": "https://github.com/JuanMaRuiz/ccc-util",
"dependencies": {
"@fnzc/nz-bank-account-validator": "^1.0.3"
}
}
65 changes: 2 additions & 63 deletions src/validator.js
@@ -1,69 +1,8 @@
const formatString = string => string.split('-').join('').replace(/\s/g, '');

const factors = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6];

const getEntityAndOfficeNumbers = (str) => {
return `00${str.substring(0,8)}`;
};

const getAccountNumber = (str) => {
return str.substring(10,20);
};

const getControlDigit = (str) => {
let controlDigit;

const sum = Array.from(str).map((el, index) => {
return el * (factors[index]);
}).reduce((acc, curr) => acc + curr);

const num = 11 - (sum % 11);

if (num === 11 ) {
controlDigit = 0;
} else if (num === 10) {
controlDigit = 1;
} else {
controlDigit = num;
}

return controlDigit;

};

const checkInstitution = (institution) => {
if (typeof institution === 'string') {
return true;
} else {
throw 'Institution is not a valid string';
}
};

const validateBankEnity = ({ institutionName, country, entityCode}) => {
const institutionCodes = require(`./${country}-institution-list.js`);
return institutionCodes[`ES${entityCode.substring(2, 6)}`] === institutionName;
};

const validate = (ccc, { institution = '', country = 'es' } = {}) => {
const cccSanitized = formatString(ccc);
const entityOfficeNumber = getEntityAndOfficeNumbers(cccSanitized);
let isValidBankEntity = true;

if (checkInstitution(institution) && institution !== '') {
isValidBankEntity = validateBankEnity({
institutionName: institution,
country: country,
entityCode: entityOfficeNumber
});
}

const accountNumber = getAccountNumber(cccSanitized);
const firstControlDigit = getControlDigit(entityOfficeNumber);
const secondControlDigit = getControlDigit(accountNumber);

return parseInt(cccSanitized.substring(8, 9)) === firstControlDigit &&
parseInt(cccSanitized.substring(9, 10)) === secondControlDigit &&
isValidBankEntity;
const validate = require(`./validators/${country}-validator.js`);
return validate(ccc, {institution, country});
};

module.exports = validate;
69 changes: 69 additions & 0 deletions src/validators/es-validator.js
@@ -0,0 +1,69 @@
const formatString = string => string.split('-').join('').replace(/\s/g, '');

const factors = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6];

const getEntityAndOfficeNumbers = (str) => {
return `00${str.substring(0,8)}`;
};

const getAccountNumber = (str) => {
return str.substring(10,20);
};

const getControlDigit = (str) => {
let controlDigit;

const sum = Array.from(str).map((el, index) => {
return el * (factors[index]);
}).reduce((acc, curr) => acc + curr);

const num = 11 - (sum % 11);

if (num === 11 ) {
controlDigit = 0;
} else if (num === 10) {
controlDigit = 1;
} else {
controlDigit = num;
}

return controlDigit;

};

const checkInstitution = (institution) => {
if (typeof institution === 'string') {
return true;
} else {
throw 'Institution param is not a valid string';
}
};

const validateBankEnity = ({ institutionName, country, entityCode}) => {
const institutionCodes = require(`./../${country}-institution-list.js`);
return institutionCodes[`ES${entityCode.substring(2, 6)}`] === institutionName;
};

const validator = (ccc, { institution = '', country = 'es'} = {}) => {
const cccSanitized = formatString(ccc);
const entityOfficeNumber = getEntityAndOfficeNumbers(cccSanitized);
let isValidBankEntity = true;

if (checkInstitution(institution) && institution !== '') {
isValidBankEntity = validateBankEnity({
institutionName: institution,
country: country,
entityCode: entityOfficeNumber
});
}

const accountNumber = getAccountNumber(cccSanitized);
const firstControlDigit = getControlDigit(entityOfficeNumber);
const secondControlDigit = getControlDigit(accountNumber);

return parseInt(cccSanitized.substring(8, 9)) === firstControlDigit &&
parseInt(cccSanitized.substring(9, 10)) === secondControlDigit &&
isValidBankEntity;
};

module.exports = validator;
23 changes: 23 additions & 0 deletions src/validators/nz-validator.js
@@ -0,0 +1,23 @@
const { isValidNZBankNumber } = require('@fnzc/nz-bank-account-validator');

// '00', '0000', '00000000', '000'
const getFormattedAccountNumber = (ccc) => {
const group1 = ccc.substring(0,2);
const group2 = ccc.substring(2,6);
const group3 = ccc.substring(6,14);
const group4 = ccc.substring(14,17);

return {
group1,
group2,
group3,
group4
};
};

const validate = (ccc) => {
const { group1, group2, group3, group4 } = getFormattedAccountNumber(ccc);
return isValidNZBankNumber(group1, group2, group3, group4);
}

module.exports = validate;

0 comments on commit e5355c1

Please sign in to comment.