Simplify E164 checks in storage service
This commit is contained in:
parent
dff924c0c5
commit
b6c395fac1
1 changed files with 5 additions and 16 deletions
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
import type { ValidateConversationType } from '../model-types.d';
|
import type { ValidateConversationType } from '../model-types.d';
|
||||||
import { isDirectConversation } from './whatTypeOfConversation';
|
import { isDirectConversation } from './whatTypeOfConversation';
|
||||||
import { parseNumber } from './libphonenumberUtil';
|
|
||||||
import { isValidUuid } from '../types/UUID';
|
import { isValidUuid } from '../types/UUID';
|
||||||
|
|
||||||
export function validateConversation(
|
export function validateConversation(
|
||||||
|
@ -27,23 +26,13 @@ export function validateConversation(
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateNumber(attributes: ValidateConversationType): string | null {
|
function validateNumber(attributes: ValidateConversationType): string | null {
|
||||||
if (isDirectConversation(attributes) && attributes.e164) {
|
const { e164 } = attributes;
|
||||||
const regionCode = window.storage.get('regionCode');
|
if (isDirectConversation(attributes) && e164) {
|
||||||
if (!regionCode) {
|
if (!/^\+[0-9]{1,19}$/.test(e164)) {
|
||||||
throw new Error('No region code');
|
return 'Invalid E164';
|
||||||
}
|
|
||||||
const number = parseNumber(attributes.e164, regionCode);
|
|
||||||
if (number.isValidNumber) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let errorMessage: undefined | string;
|
return null;
|
||||||
if (number.error instanceof Error) {
|
|
||||||
errorMessage = number.error.message;
|
|
||||||
} else if (typeof number.error === 'string') {
|
|
||||||
errorMessage = number.error;
|
|
||||||
}
|
|
||||||
return errorMessage || 'Invalid phone number';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue