mirror of
https://github.com/appwrite/appwrite
synced 2026-05-23 00:49:02 +00:00
Merge pull request #9246 from appwrite/fix-phone-validation
fix: phone number parsing exception handling
This commit is contained in:
commit
c4053b62e4
2 changed files with 11 additions and 0 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue