joi-phone-number
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
const Joi = require('joi'); | ||
@@ -39,2 +41,56 @@ const JoiPhoneNumber = require('../index.js'); | ||
}); | ||
it('formats', () => { | ||
const joi = Joi.extend(JoiPhoneNumber); | ||
let schema = joi.string().phoneNumber({ | ||
defaultCountry: 'BE', | ||
format: 'e164' | ||
}); | ||
expect(schema.validate('494322456').value).toBe('+32494322456'); | ||
schema = joi.string().phoneNumber({ | ||
defaultCountry: 'BE', | ||
format: 'international' | ||
}); | ||
expect(schema.validate('494322456').value).toBe('+32 494 32 24 56'); | ||
schema = joi.string().phoneNumber({ | ||
defaultCountry: 'BE', | ||
format: 'national' | ||
}); | ||
expect(schema.validate('494322456').value).toBe('0494 32 24 56'); | ||
schema = joi.string().phoneNumber({ | ||
defaultCountry: 'BE', | ||
format: 'rfc3966' | ||
}); | ||
expect(schema.validate('494322456').value).toBe('tel:+32-494-32-24-56'); | ||
schema = joi.string().phoneNumber({ | ||
defaultCountry: 'BE', | ||
format: 'rfc3966' | ||
}); | ||
expect(schema.validate('494322456', {convert: false}).value).toBe('494322456'); | ||
schema = joi.string().phoneNumber({ | ||
defaultCountry: 'BE', | ||
format: 'rfc3966' | ||
}); | ||
expect(schema.validate('494322456', {convert: false}).value).toBe('494322456'); | ||
}); | ||
it('errors on wrong format options', () => { | ||
const joi = Joi.extend(JoiPhoneNumber); | ||
expect(() => { | ||
joi.string().phoneNumber({ | ||
format: 'ppp' | ||
}); | ||
}).toThrow('"format" must be one of [e164, international, national, rfc3966]'); | ||
expect(() => { | ||
joi.string().phoneNumber({}); | ||
}).toThrow('must have at least 1 children'); | ||
}); | ||
}); |
'use strict'; | ||
const PhoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance(); | ||
const libphonenumber = require('google-libphonenumber'); | ||
const PhoneUtil = libphonenumber.PhoneNumberUtil.getInstance(); | ||
const PhoneNumberFormat = libphonenumber.PhoneNumberFormat; | ||
@@ -16,13 +18,22 @@ /** | ||
name: 'phoneNumber', | ||
params: { | ||
opts: joi.object().keys({ | ||
/** | ||
* We will use specified country code (phoneNumber('BE')) or 'US' if no | ||
* country code provided in phone number (0494...). Numbers with country | ||
* code (+3249...) will use the data from the number and not the default. | ||
*/ | ||
defaultCountry: joi.string(), | ||
format: joi.only('e164', 'international', 'national', 'rfc3966') | ||
}).default({defaultCountry: 'US'}).min(1) | ||
}, | ||
validate(params, value, state, options) { | ||
try { | ||
/** | ||
* FUTURE | ||
* If no country code is provided (0494...) assume it is a US number | ||
* Which is not super correct but we don't know the country of the user atm | ||
* Numbers with country code (+3249...) will use the data from the number and not the 'US' default | ||
*/ | ||
PhoneUtil.parse(value, 'US'); | ||
const proto = PhoneUtil.parse(value, params.opts.defaultCountry); | ||
if (!options.convert || !params.opts.format) { | ||
return value; | ||
} | ||
return value; | ||
const format = PhoneNumberFormat[params.opts.format.toUpperCase()]; | ||
return PhoneUtil.format(proto, format); | ||
} catch (err) { | ||
@@ -29,0 +40,0 @@ // Generate an error, state and options need to be passed |
{ | ||
"name": "joi-phone-number", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Phone number validation rule for Joi", | ||
@@ -35,3 +35,3 @@ "homepage": "https://github.com/Salesflare/joi-phone-number", | ||
"testEnvironment": "node", | ||
"notify": true, | ||
"notify": false, | ||
"collectCoverage": true, | ||
@@ -59,5 +59,5 @@ "collectCoverageFrom": [ | ||
"jest-cli": "^21.0.0", | ||
"joi": "^11.0.0", | ||
"joi": "^12.0.0", | ||
"nsp": "^2.8.0" | ||
} | ||
} |
@@ -21,2 +21,9 @@ # joi-phone-number | ||
myCustomJoi.string().phoneNumber().validate('+32494567324'); | ||
// The phone number can be transformed to a custom format | ||
// Note that this follows the `convert` option | ||
myCustomJoi.string().phoneNumber({ defaultCountry: 'BE', format: 'e164' }).validate('494322456'); // '+32494322456' | ||
myCustomJoi.string().phoneNumber({ defaultCountry: 'BE', format: 'international' }).validate('494322456'); // '+32 494 32 24 56' | ||
myCustomJoi.string().phoneNumber({ defaultCountry: 'BE', format: 'national' }).validate('494322456'); // '0494 32 24 56' | ||
myCustomJoi.string().phoneNumber({ defaultCountry: 'BE', format: 'rfc3966' }).validate('494322456'); // 'tel:+32-494-32-24-56' | ||
``` |
7474
119
29