Skip to content

Commit

Permalink
Merge pull request #52 from seegno/enhancement/add-routing-number-assert
Browse files Browse the repository at this point in the history
Add ABA Routing Number assert
  • Loading branch information
ruimarinho committed Jan 8, 2016
2 parents 73c2e40 + 3e43655 commit 4fe432a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
38 changes: 38 additions & 0 deletions src/asserts/aba-routing-number-assert.js
Original file line number Diff line number Diff line change
@@ -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;
}
60 changes: 60 additions & 0 deletions test/asserts/aba-routing-number-assert_test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
1 change: 1 addition & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4fe432a

Please sign in to comment.