From c46261a9337a62de0b8cef78d1442193c17d8f8d Mon Sep 17 00:00:00 2001 From: Maxime Le Duc Date: Wed, 27 Nov 2024 16:47:58 +0100 Subject: [PATCH] Stop using "as" --- .../phoneValidation/helpers/isPhoneNumberValid.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/features/identityCheck/pages/phoneValidation/helpers/isPhoneNumberValid.ts b/src/features/identityCheck/pages/phoneValidation/helpers/isPhoneNumberValid.ts index d7968108504..2d058c0190a 100644 --- a/src/features/identityCheck/pages/phoneValidation/helpers/isPhoneNumberValid.ts +++ b/src/features/identityCheck/pages/phoneValidation/helpers/isPhoneNumberValid.ts @@ -1,6 +1,13 @@ -import parsePhoneNumberFromString, { CountryCode } from 'libphonenumber-js' +import parsePhoneNumberFromString, { CountryCode, getCountries } from 'libphonenumber-js' + +function isCountryCode(code: string): code is CountryCode { + const countries: string[] = getCountries() + return countries.includes(code) +} export function isPhoneNumberValid(number: string, country: string) { - const phoneNumber = parsePhoneNumberFromString(number, country as CountryCode) + if (!isCountryCode(country)) return false + + const phoneNumber = parsePhoneNumberFromString(number, country) return phoneNumber?.isValid() ?? false }