From 3e4365581aa62b00168bf77a0d73369c7e344fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Mon, 4 Jan 2016 16:43:15 +0000 Subject: [PATCH] Add ABA Routing Number assert --- README.md | 5 ++ package.json | 2 + src/asserts/aba-routing-number-assert.js | 38 ++++++++++++ .../asserts/aba-routing-number-assert_test.js | 60 +++++++++++++++++++ test/index_test.js | 1 + 5 files changed, 106 insertions(+) create mode 100644 src/asserts/aba-routing-number-assert.js create mode 100644 test/asserts/aba-routing-number-assert_test.js diff --git a/README.md b/README.md index 0ce11d8..4fe0d00 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ You should pin these package's peer dependencies to the ranges listed on the *op The following set of extra asserts are provided by this package: +* [AbaRoutingNumber](#abaroutingnumber) (requires `abavalidator`) * [BankIdentifierCode](#bankidentifiercode-bic) (*BIC*) * [BigNumber](#bignumber) (requires `bignumber.js`) * [BigNumberGreaterThan](#bignumbergreaterthan) (requires `bignumber.js`) @@ -55,6 +56,10 @@ The following set of extra asserts are provided by this package: * [UsSubdivision](#ussubdivision) * [Uuid](#uuid) +### AbaRoutingNumber + +Tests if the value is a valid [ABA Routing Number](http://www.accuity.com/PageFiles/255/ROUTING_NUMBER_POLICY.pdf). + ### BankIdentifierCode (*BIC*) Tests if the value is a valid Bank Identifier Code (*BIC*) as defined in the [ISO-9362](http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=60390) standard. diff --git a/package.json b/package.json index 37ae687..e061066 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "validator.js": "^1.2.2" }, "devDependencies": { + "abavalidator": "^2.0.1", "babel-cli": "^6.3.17", "babel-core": "^6.3.26", "babel-eslint": "^4.1.6", @@ -73,6 +74,7 @@ }, "optionalPeerDependencies": { "urijs": ">=1 <2", + "abavalidator": ">=2 <3", "bignumber.js": ">=2 <3", "creditcard": ">=0.0.1 <1.0.0", "iban": ">=0.0.6 <1.0.0", diff --git a/src/asserts/aba-routing-number-assert.js b/src/asserts/aba-routing-number-assert.js new file mode 100644 index 0000000..407d0f2 --- /dev/null +++ b/src/asserts/aba-routing-number-assert.js @@ -0,0 +1,38 @@ + +/** + * Module dependencies. + */ + +import abaValidator from 'abavalidator'; +import { Validator, Violation } from 'validator.js'; + +/** + * Export `AbaRoutingNumberAssert`. + */ + +export default function abaRoutingNumberAssert() { + /** + * Class name. + */ + + this.__class__ = 'AbaRoutingNumber'; + + /** + * Validation algorithm. + */ + + this.validate = (value) => { + if (typeof value !== 'string') { + // jscs:disable requireCamelCaseOrUpperCaseIdentifiers + throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string }); + } + + if (!abaValidator.validate(value)) { + throw new Violation(this, value); + } + + return true; + }; + + return this; +} diff --git a/test/asserts/aba-routing-number-assert_test.js b/test/asserts/aba-routing-number-assert_test.js new file mode 100644 index 0000000..ced4bf9 --- /dev/null +++ b/test/asserts/aba-routing-number-assert_test.js @@ -0,0 +1,60 @@ + +/** + * Module dependencies. + */ + +import { Assert as BaseAssert, Violation } from 'validator.js'; +import AbaRoutingNumberAssert from '../../src/asserts/aba-routing-number-assert'; +import should from 'should'; + +/** + * Extend `Assert` with `AbaRoutingNumberAssert`. + */ + +const Assert = BaseAssert.extend({ + AbaRoutingNumber: AbaRoutingNumberAssert +}); + +/** + * Test `RoutingNumberAssert`. + */ + +describe('AbaRoutingNumberAssert', () => { + it('should throw an error if the input value is not a string', () => { + [{}, []].forEach((choice) => { + try { + new Assert().AbaRoutingNumber().validate(choice); + + should.fail(); + } catch (e) { + e.should.be.instanceOf(Violation); + e.violation.value.should.equal('must_be_a_string'); + } + }); + }); + + it('should throw an error if the input value is not a valid ABA routing number', () => { + try { + new Assert().AbaRoutingNumber().validate('foobar'); + + should.fail(); + } catch (e) { + e.should.be.instanceOf(Violation); + e.show().value.should.equal('foobar'); + } + }); + + it('should expose `assert` equal to `AbaRoutingNumber`', () => { + try { + new Assert().AbaRoutingNumber().validate(123); + + should.fail(); + } catch (e) { + e.show().assert.should.equal('AbaRoutingNumber'); + } + }); + + it('should accept a valid ABA routing number', () => { + new Assert().AbaRoutingNumber().validate('123123123'); + }); +}); diff --git a/test/index_test.js b/test/index_test.js index 8fcffcb..c6aaaa9 100644 --- a/test/index_test.js +++ b/test/index_test.js @@ -12,6 +12,7 @@ import asserts from '../src'; describe('validator.js-asserts', () => { it('should export all asserts', () => { Object.keys(asserts).should.eql([ + 'AbaRoutingNumber', 'BankIdentifierCode', 'BigNumber', 'BigNumberGreaterThan',