joi-phone-number
Advanced tools
Comparing version 4.1.2 to 5.0.0
'use strict'; | ||
const Joi = require('@hapi/joi'); | ||
const Joi = require('joi'); | ||
const JoiPhoneNumber = require('../index.js'); | ||
@@ -58,2 +58,24 @@ | ||
it('formats with reference', () => { | ||
const joi = Joi.extend(JoiPhoneNumber); | ||
let schema = joi.string().phoneNumber({defaultCountry: Joi.ref('$country'), format: 'e164'}); | ||
expect(schema.validate('494322456', {context: {country: 'BE'}}).value).toBe('+32494322456'); | ||
schema = joi.string().phoneNumber({defaultCountry: Joi.ref('$country'), format: 'international'}); | ||
expect(schema.validate('494322456', {context: {country: 'BE'}}).value).toBe('+32 494 32 24 56'); | ||
schema = joi.string().phoneNumber({defaultCountry: Joi.ref('$country'), format: 'national'}); | ||
expect(schema.validate('494322456', {context: {country: 'BE'}}).value).toBe('0494 32 24 56'); | ||
schema = joi.string().phoneNumber({defaultCountry: Joi.ref('$country'), format: 'rfc3966'}); | ||
expect(schema.validate('494322456', {context: {country: 'BE'}}).value).toBe('tel:+32-494-32-24-56'); | ||
schema = joi.string().phoneNumber({defaultCountry: Joi.ref('$country'), format: 'rfc3966'}); | ||
expect(schema.validate('494322456', {convert: false, context: {country: 'BE'}}).value).toBe('494322456'); | ||
schema = joi.string().phoneNumber({defaultCountry: Joi.ref('$country'), format: 'rfc3966'}); | ||
expect(schema.validate('494322456', {convert: false, context: {country: 'BE'}}).value).toBe('494322456'); | ||
}); | ||
it('strict validates', () => { | ||
@@ -99,3 +121,3 @@ const joi = Joi.extend(JoiPhoneNumber); | ||
schema.validate('011 999 7083'); | ||
}).toThrow('"defaultCountry" must be a string'); | ||
}).toThrow('"defaultCountry" must be one of [array, string] or reference'); | ||
}); | ||
@@ -109,4 +131,11 @@ | ||
schema.validate('011 999 7083'); | ||
}).toThrow('"format" must be one of [e164, international, national, rfc3966]'); | ||
}).toThrow('"format" must be one of [e164, international, national, rfc3966] or reference'); | ||
}); | ||
it('errors on wrong format options as reference', () => { | ||
const joi = Joi.extend(JoiPhoneNumber); | ||
const schema = joi.string().phoneNumber({format: Joi.ref('$format')}); | ||
expect(schema.validate('011 999 7083', {context: {format: 'qqq'}}).error).toBeInstanceOf(Error); | ||
}); | ||
}); |
@@ -17,13 +17,2 @@ 'use strict'; | ||
module.exports = joi => { | ||
const opts = joi.object().keys({ | ||
/** | ||
* We will use the specified country code or 'US', with fallback 'BE', 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.array().items(joi.string()).single(), | ||
strict: joi.boolean(), | ||
format: joi.string().valid('e164', 'international', 'national', 'rfc3966') | ||
}).default({defaultCountry: ['US', 'BE']}).min(1); | ||
return { | ||
@@ -37,20 +26,43 @@ base: joi.string(), | ||
phoneNumber: { | ||
method(options) { | ||
return this.$_addRule({name: 'phoneNumber', args: {options}}); | ||
/** | ||
* We will use the specified country code or 'US', with fallback 'BE', 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. | ||
*/ | ||
method({defaultCountry, strict, format} = {defaultCountry: ['US', 'BE']}) { | ||
return this.$_addRule({name: 'phoneNumber', args: {defaultCountry, strict, format}}); | ||
}, | ||
args: [ | ||
{ | ||
name: 'defaultCountry', | ||
ref: true, | ||
assert: joi.alternatives().try(joi.array().items(joi.string()), joi.string()) | ||
}, | ||
{ | ||
name: 'strict', | ||
ref: true, | ||
assert: joi.boolean() | ||
}, | ||
{ | ||
name: 'format', | ||
ref: true, | ||
assert: joi.valid('e164', 'international', 'national', 'rfc3966') | ||
} | ||
], | ||
validate(value, {prefs, error}, args) { | ||
const options = joi.attempt(args.options, opts); | ||
try { | ||
const proto = internals.parse(value, options.defaultCountry); | ||
// It seems that a `.single` is not doing a convert when used in `args.assert` so we do it here manually | ||
const countries = (Array.isArray(args.defaultCountry) || !args.defaultCountry) ? args.defaultCountry : [args.defaultCountry]; | ||
if (options.strict && !PhoneUtil.isValidNumber(proto)) { | ||
const proto = internals.parse(value, countries); | ||
if (args.strict && !PhoneUtil.isValidNumber(proto)) { | ||
throw new Error('StrictPhoneNumber'); | ||
} | ||
if (!prefs.convert || !options.format) { | ||
if (!prefs.convert || !args.format) { | ||
return value; | ||
} | ||
const format = PhoneNumberFormat[options.format.toUpperCase()]; | ||
const format = PhoneNumberFormat[args.format.toUpperCase()]; | ||
value = PhoneUtil.format(proto, format); | ||
@@ -57,0 +69,0 @@ return value; |
{ | ||
"name": "joi-phone-number", | ||
"version": "4.1.2", | ||
"version": "5.0.0", | ||
"description": "Phone number validation rule for Joi", | ||
@@ -49,11 +49,11 @@ "homepage": "https://github.com/Salesflare/joi-phone-number", | ||
"dependencies": { | ||
"google-libphonenumber": "3.2.11" | ||
"google-libphonenumber": "3.2.13" | ||
}, | ||
"devDependencies": { | ||
"@hapi/joi": "^17.1.1", | ||
"coveralls": "^3.1.0", | ||
"eslint": "^7.6.0", | ||
"eslint": "^7.9.0", | ||
"eslint-config-xo-space": "^0.25.0", | ||
"jest": "^26.4.0", | ||
"jest-cli": "^26.4.0" | ||
"jest": "^26.4.2", | ||
"jest-cli": "^26.4.2", | ||
"joi": "^17.2.1" | ||
}, | ||
@@ -60,0 +60,0 @@ "engines": { |
14246
199
+ Addedgoogle-libphonenumber@3.2.13(transitive)
- Removedgoogle-libphonenumber@3.2.11(transitive)
Updatedgoogle-libphonenumber@3.2.13