diff --git a/src/asserts/aba-account-number-assert.js b/src/asserts/aba-account-number-assert.js new file mode 100644 index 0000000..47d3593 --- /dev/null +++ b/src/asserts/aba-account-number-assert.js @@ -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; +}; diff --git a/src/index.js b/src/index.js index 6675139..8502306 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); @@ -48,6 +49,7 @@ const Uuid = require('./asserts/uuid-assert.js'); */ module.exports = { + AbaAccountNumber, AbaRoutingNumber, BankIdentifierCode, BigNumber, diff --git a/test/asserts/aba-account-number-assert.test.js b/test/asserts/aba-account-number-assert.test.js new file mode 100644 index 0000000..2f1801b --- /dev/null +++ b/test/asserts/aba-account-number-assert.test.js @@ -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'); + }); +}); diff --git a/test/asserts/aba-routing-number-assert.test.js b/test/asserts/aba-routing-number-assert.test.js index 264eca0..f51aa3f 100644 --- a/test/asserts/aba-routing-number-assert.test.js +++ b/test/asserts/aba-routing-number-assert.test.js @@ -16,7 +16,7 @@ const Assert = BaseAssert.extend({ }); /** - * Test `RoutingNumberAssert`. + * Test `AbaRoutingNumberAssert`. */ describe('AbaRoutingNumberAssert', () => { diff --git a/test/index.test.js b/test/index.test.js index 6704d3d..dc55af5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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',