diff --git a/src/asserts/aba-account-number-assert.js b/src/asserts/aba-account-number-assert.js new file mode 100644 index 0000000..59d9c58 --- /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 = /^[a-zA-Z0-9]{5,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..5f9c70c --- /dev/null +++ b/test/asserts/aba-account-number-assert.test.js @@ -0,0 +1,63 @@ +'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('foo'); + + fail(); + } catch (e) { + expect(e).toBeInstanceOf(Violation); + expect(e.show().value).toBe('foo'); + } + }); + + it('should expose `assert` equal to `AbaAccountNumber`', () => { + try { + Assert.abaAccountNumber().validate(123); + + fail(); + } catch (e) { + expect(e.show().assert).toBe('AbaAccountNumber'); + } + }); + + it.only.each(['76175925', '76175925761759257', '76175', 'abcdefghijklmnopq', 'abcde'])( + 'should accept a valid ABA account number', + accountNumber => { + Assert.abaAccountNumber().validate(accountNumber); + } + ); +}); 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',