Skip to content

Commit

Permalink
Add Canada Postal Code assert
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamind51 committed Dec 16, 2024
1 parent 328d659 commit 56736fb
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The following set of extra asserts are provided by this package:
- [BigNumberLessThanOrEqualTo](#bignumberlessthanorequalto) (requires `bignumber.js`)
- [Boolean](#boolean)
- [Callback](#callback) (requires `callback`)
- [CanadaPostalCode](#canadapostalcode)
- [CpfNumber](#cpfnumber) (requires `cpf`)
- [CreditCard](#creditcard) (requires `creditcard`)
- [CurpNumber](#curpnumber) (requires `curp`)
Expand Down Expand Up @@ -110,6 +111,10 @@ Allows you to add custom rules by giving a callback function and a custom class.
- `callback` (required) - the callback function.
- `customClass` (required) - the name of the class.

### CanadaPostalCode
Tests if the value is valid Canada postal code.
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).

### CpfNumber
Tests if the value is valid CPF number.

Expand Down
43 changes: 43 additions & 0 deletions src/asserts/canada-postal-code-assert.js
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;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const BigNumberLessThan = require('./asserts/big-number-less-than-assert.js');
const BigNumberLessThanOrEqualTo = require('./asserts/big-number-less-than-or-equal-to-assert.js');
const Boolean = require('./asserts/boolean-assert.js');
const Callback = require('./asserts/callback-assert');
const CanadaPostalCode = require('./asserts/canada-postal-code-assert.js');
const CpfNumber = require('./asserts/cpf-number-assert');
const CreditCard = require('./asserts/credit-card-assert.js');
const CurpNumber = require('./asserts/curp-number-assert.js');
Expand Down Expand Up @@ -60,6 +61,7 @@ module.exports = {
BigNumberLessThanOrEqualTo,
Boolean,
Callback,
CanadaPostalCode,
CpfNumber,
CreditCard,
CurpNumber,
Expand Down
58 changes: 58 additions & 0 deletions test/asserts/canada-postal-code-assert.test.js
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);
});
});
});
3 changes: 2 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('validator.js-asserts', () => {
it('should export all asserts', () => {
const assertNames = Object.keys(asserts);

expect(assertNames).toHaveLength(40);
expect(assertNames).toHaveLength(41);
expect(assertNames).toEqual(
expect.arrayContaining([
'AbaRoutingNumber',
Expand All @@ -27,6 +27,7 @@ describe('validator.js-asserts', () => {
'BigNumberLessThanOrEqualTo',
'Boolean',
'Callback',
'CanadaPostalCode',
'CpfNumber',
'CreditCard',
'CurpNumber',
Expand Down

0 comments on commit 56736fb

Please sign in to comment.