Merge pull request #9246 from appwrite/fix-phone-validation

fix: phone number parsing exception handling
This commit is contained in:
Christy Jacob 2025-01-21 12:42:56 +05:30 committed by GitHub
commit c4053b62e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View file

@ -2,6 +2,8 @@
namespace Appwrite\Auth\Validator;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberUtil;
use Utopia\Validator;
/**
@ -12,10 +14,12 @@ use Utopia\Validator;
class Phone extends Validator
{
protected bool $allowEmpty;
protected PhoneNumberUtil $helper;
public function __construct(bool $allowEmpty = false)
{
$this->allowEmpty = $allowEmpty;
$this->helper = PhoneNumberUtil::getInstance();
}
/**
@ -47,6 +51,12 @@ class Phone extends Validator
return true;
}
try {
$this->helper->parse($value);
} catch (NumberParseException $e) {
return false;
}
return !!\preg_match('/^\+[1-9]\d{6,14}$/', $value);
}

View file

@ -31,6 +31,7 @@ class PhoneTest extends TestCase
$this->assertEquals($this->object->isValid('+0415553452342'), false);
$this->assertEquals($this->object->isValid('+14 155 5524564'), false);
$this->assertEquals($this->object->isValid('+1415555245634543'), false);
$this->assertEquals($this->object->isValid('+8020000000'), false); // when country code is not present
$this->assertEquals($this->object->isValid(+14155552456), false);
$this->assertEquals($this->object->isValid('+1415555'), true);