Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support allowing specific phone types #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/asserts/phone-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
*/

import { Validator, Violation } from 'validator.js';
import { includes, intersection, isArray, keys } from 'lodash';

/**
* Export `Phone`.
*/

export default function phoneAssert({ countryCode } = {}) {
export default function phoneAssert({ countryCode, allowedTypes } = {}) {
/**
* Peer dependency `google-libphonenumber`.
*/

const PhoneNumberUtil = require('google-libphonenumber').PhoneNumberUtil;
const { PhoneNumberType, PhoneNumberUtil } = require('google-libphonenumber');

/**
* Phone util instance.
Expand All @@ -36,6 +37,18 @@ export default function phoneAssert({ countryCode } = {}) {

this.countryCode = countryCode;

/**
* The allowed phone number types.
*/

if (allowedTypes) {
if (!isArray(allowedTypes) || intersection(allowedTypes, keys(PhoneNumberType)).length !== allowedTypes.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.keys?

throw new Error('Phone types specified are not valid.');
}

this.allowedTypes = allowedTypes.map(type => PhoneNumberType[type]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .map but not Array.isArray?

}

/**
* Validation algorithm.
*/
Expand All @@ -55,6 +68,10 @@ export default function phoneAssert({ countryCode } = {}) {
if (this.countryCode && !phoneUtil.isValidNumberForRegion(phone, this.countryCode)) {
throw new Error(`Phone does not belong to country "${this.countryCode}"`);
}

if (this.allowedTypes && !includes(this.allowedTypes, phoneUtil.getNumberType(phone))) {
throw new Error(`Phone type is not allowed`);
}
} catch (e) {
throw new Violation(this, value);
}
Expand Down
24 changes: 24 additions & 0 deletions test/asserts/phone-assert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ const Assert = BaseAssert.extend({
*/

describe('Phone', () => {
it('should throw an error if the allowed types are invalid', () => {
['foobar', ['foobar']].forEach(types => {
try {
new Assert().Phone({ allowedTypes: types }).validate('+1 415 555 2671');

should.fail();
} catch (e) {
e.should.be.instanceOf(Error);
e.message.should.equal('Phone types specified are not valid.');
}
});
});

it('should throw an error if the input value is not a string', () => {
[{}, [], 123].forEach(choice => {
try {
Expand Down Expand Up @@ -55,6 +68,17 @@ describe('Phone', () => {
}
});

it('should throw an error if the phone does not have one of the given allowed types', () => {
try {
new Assert().Phone({ allowedTypes: ['FIXED_LINE', 'MOBILE'] }).validate('+1 415 555 2671');

should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('Phone');
}
});

it('should accept a valid phone in the e164 format', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test where a number belonging to an allowed type is accepted.

new Assert().Phone().validate('+1 415 555 2671');
});
Expand Down