-
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.
- Loading branch information
1 parent
b37a5d9
commit 6e04f4d
Showing
5 changed files
with
111 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
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,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); | ||
} | ||
); | ||
}); |
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