From f757322ec6721b1b552798cfb99380210e318267 Mon Sep 17 00:00:00 2001 From: Miguel Martins Date: Thu, 8 Sep 2016 16:26:50 +0100 Subject: [PATCH] Support allowing specific phone types --- src/asserts/phone-assert.js | 21 +++++++++++++++++++-- test/asserts/phone-assert_test.js | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/asserts/phone-assert.js b/src/asserts/phone-assert.js index 763e1e2..821715d 100644 --- a/src/asserts/phone-assert.js +++ b/src/asserts/phone-assert.js @@ -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. @@ -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) { + throw new Error('Phone types specified are not valid.'); + } + + this.allowedTypes = allowedTypes.map(type => PhoneNumberType[type]); + } + /** * Validation algorithm. */ @@ -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); } diff --git a/test/asserts/phone-assert_test.js b/test/asserts/phone-assert_test.js index 1458cd1..c9b3f88 100644 --- a/test/asserts/phone-assert_test.js +++ b/test/asserts/phone-assert_test.js @@ -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 { @@ -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', () => { new Assert().Phone().validate('+1 415 555 2671'); });