From dd930747d48f52e95f3f28480a138d593138fbce Mon Sep 17 00:00:00 2001 From: florin Date: Thu, 16 Dec 2021 15:53:44 +0200 Subject: [PATCH] -Validate was not quite a validate --- src/decorator/string/IsPhoneNumber.ts | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/src/decorator/string/IsPhoneNumber.ts b/src/decorator/string/IsPhoneNumber.ts index 77661512c9..419d31e6d9 100644 --- a/src/decorator/string/IsPhoneNumber.ts +++ b/src/decorator/string/IsPhoneNumber.ts @@ -1,28 +1,9 @@ import { ValidationOptions } from '../ValidationOptions'; import { buildMessage, ValidateBy } from '../common/ValidateBy'; -import { parsePhoneNumberFromString, CountryCode } from 'libphonenumber-js'; +import { isValidPhoneNumber, CountryCode } from 'libphonenumber-js'; export const IS_PHONE_NUMBER = 'isPhoneNumber'; -/** - * Checks if the string is a valid phone number. To successfully validate any phone number the text must include - * the intl. calling code, if the calling code wont be provided then the region must be set. - * - * @param value the potential phone number string to test - * @param region 2 characters uppercase country code (e.g. DE, US, CH) for country specific validation. - * If text doesn't start with the international calling code (e.g. +41), then you must set this parameter. - */ -export function isPhoneNumber(value: string, region?: CountryCode): boolean { - try { - const phoneNum = parsePhoneNumberFromString(value, region); - const result = phoneNum?.isValid(); - return !!result; - } catch (error) { - // logging? - return false; - } -} - /** * Checks if the string is a valid phone number. To successfully validate any phone number the text must include * the intl. calling code, if the calling code wont be provided then the region must be set. @@ -36,7 +17,7 @@ export function IsPhoneNumber(region?: CountryCode, validationOptions?: Validati name: IS_PHONE_NUMBER, constraints: [region], validator: { - validate: (value, args): boolean => isPhoneNumber(value, args.constraints[0]), + validate: (value, args): boolean => isValidPhoneNumber(value, args.constraints[0]), defaultMessage: buildMessage( eachPrefix => eachPrefix + '$property must be a valid phone number', validationOptions