Skip to content

Commit

Permalink
Add ABA account number assert
Browse files Browse the repository at this point in the history
  • Loading branch information
acostalima committed Jun 19, 2023
1 parent b37a5d9 commit 3f005dc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
43 changes: 43 additions & 0 deletions src/asserts/aba-account-number-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

/**
* Module dependencies.
*/

const { Validator, Violation } = require('validator.js');

/**
* Account number regex.
*/

const accountNumberRegex = /^\d{8,17}$/;

/**
* Export `AbaAccountNumberAssert`.
*/

module.exports = function abaAccountNumberAssert() {
/**
* Class name.
*/

this.__class__ = 'AbaAccountNumber';

/**
* Validation algorithm.
*/

this.validate = value => {
if (typeof value !== 'string') {
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
}

if (!accountNumberRegex.test(value)) {
throw new Violation(this, value);
}

return true;
};

return this;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Module dependencies.
*/

const AbaAccountNumber = require('./asserts/aba-account-number-assert');
const AbaRoutingNumber = require('./asserts/aba-routing-number-assert.js');
const BankIdentifierCode = require('./asserts/bank-identifier-code-assert.js');
const BigNumber = require('./asserts/big-number-assert.js');
Expand Down Expand Up @@ -48,6 +49,7 @@ const Uuid = require('./asserts/uuid-assert.js');
*/

module.exports = {
AbaAccountNumber,
AbaRoutingNumber,
BankIdentifierCode,
BigNumber,
Expand Down
60 changes: 60 additions & 0 deletions test/asserts/aba-account-number-assert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

/**
* Module dependencies.
*/

const { Assert: BaseAssert, Violation } = require('validator.js');
const AbaAccountNumberAssert = require('../../src/asserts/aba-account-number-assert');

/**
* Extend `Assert` with `AbaAccountNumberAssert`.
*/

const Assert = BaseAssert.extend({
AbaAccountNumber: AbaAccountNumberAssert
});

/**
* Test `AbaAccountNumberAssert`.
*/

describe('AbaAccountNumberAssert', () => {
it('should throw an error if the input value is not a string', () => {
[{}, []].forEach(choice => {
try {
Assert.abaAccountNumber().validate(choice);

fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.violation.value).toBe('must_be_a_string');
}
});
});

it('should throw an error if the input value is not a valid ABA account number', () => {
try {
Assert.abaAccountNumber().validate('foobar');

fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().value).toBe('foobar');
}
});

it('should expose `assert` equal to `AbaAccountNumber`', () => {
try {
Assert.abaAccountNumber().validate(123);

fail();
} catch (e) {
expect(e.show().assert).toBe('AbaAccountNumber');
}
});

it('should accept a valid ABA account number', () => {
Assert.abaAccountNumber().validate('76175925');
});
});
2 changes: 1 addition & 1 deletion test/asserts/aba-routing-number-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Assert = BaseAssert.extend({
});

/**
* Test `RoutingNumberAssert`.
* Test `AbaRoutingNumberAssert`.
*/

describe('AbaRoutingNumberAssert', () => {
Expand Down
3 changes: 2 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ describe('validator.js-asserts', () => {
it('should export all asserts', () => {
const assertNames = Object.keys(asserts);

expect(assertNames).toHaveLength(38);
expect(assertNames).toHaveLength(39);
expect(assertNames).toEqual(
expect.arrayContaining([
'AbaAccountNumber',
'AbaRoutingNumber',
'BankIdentifierCode',
'BigNumber',
Expand Down

0 comments on commit 3f005dc

Please sign in to comment.