Import - Improve phone validation (#12901)
Context : - Phones import is a bit complex if not all subfields are provided. - Phones subfield validation are absent or different from BE validation. Solution : - Normalize callingCode and countryCode validation (BE/FE) - Ease phone import if only phoneNumber is provided
This commit is contained in:
@ -22,6 +22,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@sniptt/guards": "^0.2.0",
|
||||
"libphonenumber-js": "^1.10.26",
|
||||
"zod": "3.23.8"
|
||||
},
|
||||
"preconstruct": {
|
||||
|
||||
@ -33,3 +33,5 @@ export { isValidLocale } from './validation/isValidLocale';
|
||||
export { isValidUuid } from './validation/isValidUuid';
|
||||
export { isValidVariable } from './validation/isValidVariable';
|
||||
export { normalizeLocale } from './validation/normalizeLocale';
|
||||
export { getCountryCodesForCallingCode } from './validation/phones-value/getCountryCodesForCallingCode';
|
||||
export { isValidCountryCode } from './validation/phones-value/isValidCountryCode';
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
import { getCountries, getCountryCallingCode } from 'libphonenumber-js';
|
||||
|
||||
const ALL_COUNTRIES_CODE = getCountries();
|
||||
|
||||
export const getCountryCodesForCallingCode = (callingCode: string) => {
|
||||
const cleanCallingCode = callingCode.startsWith('+')
|
||||
? callingCode.slice(1)
|
||||
: callingCode;
|
||||
|
||||
return ALL_COUNTRIES_CODE.filter((country) => {
|
||||
const countryCallingCode = getCountryCallingCode(country);
|
||||
|
||||
return countryCallingCode === cleanCallingCode;
|
||||
});
|
||||
};
|
||||
@ -0,0 +1 @@
|
||||
export * from './isValidCountryCode';
|
||||
@ -0,0 +1,7 @@
|
||||
import { CountryCode, getCountries } from 'libphonenumber-js';
|
||||
|
||||
const ALL_COUNTRIES_CODE = getCountries();
|
||||
|
||||
export const isValidCountryCode = (input: string): input is CountryCode => {
|
||||
return ALL_COUNTRIES_CODE.includes(input as unknown as CountryCode);
|
||||
};
|
||||
Reference in New Issue
Block a user