Skip to content

Commit

Permalink
Add nz bank account validator (#3)
Browse files Browse the repository at this point in the history
* Add nz bank account validator

* Upgrade package version
  • Loading branch information
JuanMaRuiz committed Nov 4, 2020
1 parent f366b86 commit 3d566b7
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## [1.2.0] - 2020-08-26
### Add validator for NZ accounts
* Add the possibility to pass 'nz' as country option and validate the account number for this country.

## [1.1.0] - 2020-08-26
### Add options param
* Add the possibility to pass an options object with the institution name. Validator will check the correctness of the ccc number and if
Expand Down
12 changes: 11 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 All @@ -73,6 +78,11 @@ In the list below you will find some of the codes of monetary financial institut
| ES2048 | Liberbank, S.A.|
| ES0073 | Open Bank, S.A.|
### Available validators
* Spain account validator.
* NZ account validator
### Reference
* [How to validate an Spanish CCC](https://es.wikipedia.org/wiki/Código_cuenta_cliente#D%C3%ADgitos_de_control)
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);
});
7 changes: 5 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "ccc-util",
"version": "1.1.0",
"version": "1.2.0",
"description": "Util to validate CCC code. It also can be used to generate a valid CCC for testing purposes",
"main": "index.js",
"scripts": {
Expand All @@ -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 3d566b7

Please sign in to comment.