joi-phone-number
Advanced tools
Comparing version 3.0.3 to 3.1.0
@@ -73,2 +73,14 @@ 'use strict'; | ||
it('strict validates', () => { | ||
const joi = Joi.extend(JoiPhoneNumber); | ||
const strict = joi.string().phoneNumber({defaultCountry: 'US', strict: true}); | ||
const notStrict = joi.string().phoneNumber({defaultCountry: 'US'}); | ||
const numbers = ['7777777777', '1234567890']; | ||
for (const number of numbers) { | ||
expect(strict.validate(number).error.name).toEqual('ValidationError'); | ||
expect(notStrict.validate(number).error).toEqual(null); | ||
} | ||
}); | ||
it('validates and formats without a country', () => { | ||
@@ -75,0 +87,0 @@ const joi = Joi.extend(JoiPhoneNumber); |
@@ -31,4 +31,5 @@ 'use strict'; | ||
defaultCountry: joi.array().items(joi.string()).single(), | ||
format: joi.string().only('e164', 'international', 'national', 'rfc3966') | ||
}).default({defaultCountry: ['US', 'BE']}).min(1) | ||
format: joi.string().only('e164', 'international', 'national', 'rfc3966'), | ||
strict: joi.boolean() | ||
}).default({defaultCountry: ['US', 'BE'], strict: false}).min(1) | ||
}, | ||
@@ -38,2 +39,7 @@ validate(params, value, state, options) { | ||
const proto = internals.parse(value, params.opts.defaultCountry); | ||
if (params.opts.strict && !PhoneUtil.isValidNumber(proto)) { | ||
throw new Error('StrictPhoneNumber'); | ||
} | ||
if (!options.convert || !params.opts.format) { | ||
@@ -46,7 +52,7 @@ return value; | ||
} catch (err) { | ||
const knownErrors = ['Invalid country calling code', 'The string supplied did not seem to be a phone number']; | ||
const knownErrors = ['Invalid country calling code', 'The string supplied did not seem to be a phone number', 'StrictPhoneNumber']; | ||
// We ignore the next line for line coverage since we should always hit it but if we have a regression in our code we still want to surface that instead of just returning the default error | ||
/* istanbul ignore next */ | ||
if (knownErrors.includes(err.message)) { | ||
// Generate an error, state and options need to be passed | ||
// Generate an error, state and options need to be passed | ||
return this.createError('string.phonenumber', {value}, state, options); | ||
@@ -53,0 +59,0 @@ } |
{ | ||
"name": "joi-phone-number", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "Phone number validation rule for Joi", | ||
@@ -50,3 +50,3 @@ "homepage": "https://github.com/Salesflare/joi-phone-number", | ||
"dependencies": { | ||
"google-libphonenumber": "3.2.4" | ||
"google-libphonenumber": "3.2.5" | ||
}, | ||
@@ -56,3 +56,3 @@ "devDependencies": { | ||
"coveralls": "^3.0.6", | ||
"eslint": "^6.2.2", | ||
"eslint": "^6.4.0", | ||
"eslint-config-xo-space": "^0.21.0", | ||
@@ -59,0 +59,0 @@ "jest": "^24.9.0", |
@@ -29,2 +29,4 @@ # joi-phone-number | ||
myCustomJoi.string().phoneNumber({ defaultCountry: 'BE', format: 'rfc3966' }).validate('494322456'); // 'tel:+32-494-32-24-56' | ||
myCustomJoi.string().phoneNumber({ defaultCountry: 'US', strict: true }).validate('7777777777'); // validation error | ||
myCustomJoi.string().phoneNumber({ defaultCountry: 'US'}).validate('7777777777'); // 7777777777 | ||
``` |
11227
170
32
+ Addedgoogle-libphonenumber@3.2.5(transitive)
- Removedgoogle-libphonenumber@3.2.4(transitive)
Updatedgoogle-libphonenumber@3.2.5