-
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
328d659
commit 56736fb
Showing
5 changed files
with
110 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const { Validator, Violation } = require('validator.js'); | ||
|
||
/** | ||
* Canada postal code regular expression. | ||
*/ | ||
|
||
const regexp = new RegExp(/^[ABCEGHJKLMNPRSTVXY]\d[A-Z][ -]?\d[A-Z]\d$/); | ||
|
||
/** | ||
* Export `CanadaPostalCodeAssert`. | ||
*/ | ||
|
||
module.exports = function canadaPostalCodeAssert() { | ||
/** | ||
* Class name. | ||
*/ | ||
|
||
this.__class__ = 'CanadaPostalCode'; | ||
|
||
/** | ||
* Validation algorithm. | ||
*/ | ||
|
||
this.validate = function (value) { | ||
if (typeof value !== 'string') { | ||
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string }); | ||
} | ||
|
||
if (!regexp.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,58 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const { Assert: BaseAssert, Validator, Violation } = require('validator.js'); | ||
const CanadaPostalCodeAssert = require('../../src/asserts/canada-postal-code-assert'); | ||
|
||
/** | ||
* Extend `Assert` with `CanadaPostalCodeAssert`. | ||
*/ | ||
|
||
const Assert = BaseAssert.extend({ | ||
CanadaPostalCode: CanadaPostalCodeAssert | ||
}); | ||
|
||
/** | ||
* Test `CanadaPostalCodeAssert`. | ||
*/ | ||
|
||
describe('CanadaPostalCodeAssert', () => { | ||
it('should throw an error if the input value is not a string', () => { | ||
const choices = [[], {}, 123]; | ||
|
||
choices.forEach(choice => { | ||
try { | ||
Assert.canadaPostalCode().validate(choice); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Violation); | ||
expect(e.violation.value).toBe(Validator.errorCode.must_be_a_string); | ||
} | ||
}); | ||
}); | ||
|
||
it('should throw an error if `value` is invalid', () => { | ||
const choices = ['#', '1A1A9B', 'A1AA1A', 'D1A9B9', 'a1A9B9', 'A1a9B9', 'A1A9b9']; | ||
|
||
choices.forEach(choice => { | ||
try { | ||
Assert.canadaPostalCode().validate(choice); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Violation); | ||
expect(e.show().assert).toBe('CanadaPostalCode'); | ||
} | ||
}); | ||
}); | ||
|
||
it('should accept a valid postal code', () => { | ||
['A1A1A1', 'A1A9B9', 'C9A2M6', 'C9A 2M6', 'T7A-2D3'].forEach(choice => { | ||
Assert.canadaPostalCode().validate(choice); | ||
}); | ||
}); | ||
}); |
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