Add E.164 input validation for phone numbers (FRE-4506)
- Extract phone validation to reusable utility (src/utils/phone-validation.ts) - Use libphonenumber-js for strict E.164 format validation - Normalize accepted numbers to canonical E.164 format - Add 22 comprehensive validation tests covering valid/invalid formats - Update existing tests to use valid E.164 test numbers - All 56 tests passing Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
25
services/spamshield/src/utils/phone-validation.ts
Normal file
25
services/spamshield/src/utils/phone-validation.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { isValidPhoneNumber, parsePhoneNumber } from 'libphonenumber-js';
|
||||
|
||||
export class PhoneNumberValidationError extends Error {
|
||||
constructor(public readonly originalInput: string) {
|
||||
super(
|
||||
`Invalid E.164 phone number format: ${originalInput}. Expected format: +[country code][number] (e.g., +14155552671)`
|
||||
);
|
||||
this.name = 'PhoneNumberValidationError';
|
||||
}
|
||||
}
|
||||
|
||||
export function validatePhoneNumber(phoneNumber: string): string {
|
||||
const trimmed = phoneNumber.trim();
|
||||
|
||||
if (!isValidPhoneNumber(trimmed)) {
|
||||
throw new PhoneNumberValidationError(phoneNumber);
|
||||
}
|
||||
|
||||
const parsed = parsePhoneNumber(trimmed);
|
||||
if (!parsed || !parsed.number) {
|
||||
throw new PhoneNumberValidationError(phoneNumber);
|
||||
}
|
||||
|
||||
return parsed.number;
|
||||
}
|
||||
Reference in New Issue
Block a user