-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from seegno/enhancement/add-routing-number-assert
Add ABA Routing Number assert
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters