Skip to content

Commit 6b432e9

Browse files
Megamind51vitorcoxta
authored andcommitted
Add Canada zip code assert
1 parent c7580b0 commit 6b432e9

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The following set of extra asserts are provided by this package:
3030
- [BigNumberLessThanOrEqualTo](#bignumberlessthanorequalto) (requires `bignumber.js`)
3131
- [Boolean](#boolean)
3232
- [Callback](#callback) (requires `callback`)
33+
- [CaZipCode](#cazipcode)
3334
- [CpfNumber](#cpfnumber) (requires `cpf`)
3435
- [CreditCard](#creditcard) (requires `creditcard`)
3536
- [CurpNumber](#curpnumber) (requires `curp`)
@@ -110,6 +111,10 @@ Allows you to add custom rules by giving a callback function and a custom class.
110111
- `callback` (required) - the callback function.
111112
- `customClass` (required) - the name of the class.
112113

114+
### CaZipCode
115+
Tests if the value is valid Canada zip code.
116+
We only allow initial characters from the list on the [site](https://www.canadapost-postescanada.ca/cpc/en/support/articles/addressing-guidelines/postal-codes.page).
117+
113118
### CpfNumber
114119
Tests if the value is valid CPF number.
115120

src/asserts/ca-zip-code-assert.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const { Validator, Violation } = require('validator.js');
8+
9+
/**
10+
* Canada zip code regular expression.
11+
*/
12+
13+
const regexp = new RegExp(/^[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z][ -]?\d[A-Za-z]\d$/);
14+
15+
/**
16+
* Export `CaZipCodeAssert`.
17+
*/
18+
19+
module.exports = function caZipCodeAssert() {
20+
/**
21+
* Class name.
22+
*/
23+
24+
this.__class__ = 'CaZipCode';
25+
26+
/**
27+
* Validation algorithm.
28+
*/
29+
30+
this.validate = function (value) {
31+
if (typeof value !== 'string') {
32+
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
33+
}
34+
35+
if (!regexp.test(value)) {
36+
throw new Violation(this, value);
37+
}
38+
39+
return true;
40+
};
41+
42+
return this;
43+
};

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const BigNumberLessThan = require('./asserts/big-number-less-than-assert.js');
1414
const BigNumberLessThanOrEqualTo = require('./asserts/big-number-less-than-or-equal-to-assert.js');
1515
const Boolean = require('./asserts/boolean-assert.js');
1616
const Callback = require('./asserts/callback-assert');
17+
const CaZipCode = require('./asserts/ca-zip-code-assert.js');
1718
const CpfNumber = require('./asserts/cpf-number-assert');
1819
const CreditCard = require('./asserts/credit-card-assert.js');
1920
const CurpNumber = require('./asserts/curp-number-assert.js');
@@ -59,6 +60,7 @@ module.exports = {
5960
BigNumberLessThan,
6061
BigNumberLessThanOrEqualTo,
6162
Boolean,
63+
CaZipCode,
6264
Callback,
6365
CpfNumber,
6466
CreditCard,
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const { Assert: BaseAssert, Validator, Violation } = require('validator.js');
8+
const CaZipCodeAssert = require('../../src/asserts/ca-zip-code-assert');
9+
10+
/**
11+
* Extend `Assert` with `CaZipCodeAssert`.
12+
*/
13+
14+
const Assert = BaseAssert.extend({
15+
CaZipCode: CaZipCodeAssert
16+
});
17+
18+
/**
19+
* Test `CaZipCodeAssert`.
20+
*/
21+
22+
describe('CaZipCodeAssert', () => {
23+
it('should throw an error if the input value is not a string', () => {
24+
const choices = [[], {}, 123];
25+
26+
choices.forEach(choice => {
27+
try {
28+
Assert.caZipCode().validate(choice);
29+
30+
fail();
31+
} catch (e) {
32+
expect(e).toBeInstanceOf(Violation);
33+
expect(e.violation.value).toBe(Validator.errorCode.must_be_a_string);
34+
}
35+
});
36+
});
37+
38+
it('should throw an error if `value` is invalid', () => {
39+
const choices = ['#', '1A1A9B', 'A1AA1A', 'D1A9B9'];
40+
41+
choices.forEach(choice => {
42+
try {
43+
Assert.caZipCode().validate(choice);
44+
45+
fail();
46+
} catch (e) {
47+
expect(e).toBeInstanceOf(Violation);
48+
expect(e.show().assert).toBe('CaZipCode');
49+
}
50+
});
51+
});
52+
53+
it('should accept a valid zip code', () => {
54+
['A1A1A1', 'A1A9B9', 'A1A9b9', 'A1a9B9', 'C9A2M6', 'C9A 2M6', 'T7A-2D3', 'a1A9B9'].forEach(choice => {
55+
Assert.caZipCode().validate(choice);
56+
});
57+
});
58+
});

test/index.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('validator.js-asserts', () => {
1414
it('should export all asserts', () => {
1515
const assertNames = Object.keys(asserts);
1616

17-
expect(assertNames).toHaveLength(40);
17+
expect(assertNames).toHaveLength(41);
1818
expect(assertNames).toEqual(
1919
expect.arrayContaining([
2020
'AbaRoutingNumber',
@@ -26,6 +26,7 @@ describe('validator.js-asserts', () => {
2626
'BigNumberLessThan',
2727
'BigNumberLessThanOrEqualTo',
2828
'Boolean',
29+
'CaZipCode',
2930
'Callback',
3031
'CpfNumber',
3132
'CreditCard',

0 commit comments

Comments
 (0)