From e82751daa768abc8786c7a4bb0c510c9afc48c3f Mon Sep 17 00:00:00 2001 From: juansola Date: Fri, 10 Dec 2021 12:32:17 +0100 Subject: [PATCH 1/2] Changed Assert::email validation by eguilias email RFCValidation used in Laravel --- composer.json | 3 ++- lib/helper/Assert.php | 8 +++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index f9e25e347..f58a1d371 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "ext-openssl": "*" + "ext-openssl": "*", + "egulias/email-validator": "^2.0" }, "require-dev": { "phpunit/phpunit": "^5.7.9 || ^6.4.3", diff --git a/lib/helper/Assert.php b/lib/helper/Assert.php index f1da609a7..633371081 100644 --- a/lib/helper/Assert.php +++ b/lib/helper/Assert.php @@ -8,6 +8,8 @@ namespace SendGrid\Helper; +use Egulias\EmailValidator\EmailLexer; +use Egulias\EmailValidator\Validation\RFCValidation; use SendGrid\Mail\TypeException; class Assert @@ -47,11 +49,7 @@ public static function email($value, $property, $message = null) { static::string($value, $property, $message); - // Define additional flags for filter_var to verify unicode characters on local part - // Constant FILTER_FLAG_EMAIL_UNICODE is available since PHP 7.1 - $flags = (defined('FILTER_FLAG_EMAIL_UNICODE')) ? FILTER_FLAG_EMAIL_UNICODE : null; - - if (filter_var($value, FILTER_VALIDATE_EMAIL, $flags) === false) { + if (!(new RFCValidation())->isValid($value, new EmailLexer())) { $message = sprintf( $message ?: '"$%s" must be a valid email address. Got: %s', $property, From 900108d54472b87aade8204fd1a22d1c0aeeddf4 Mon Sep 17 00:00:00 2001 From: juansola Date: Fri, 10 Dec 2021 13:13:06 +0100 Subject: [PATCH 2/2] Changed Assert::email validation by eguilias email RFCValidation used in Laravel --- composer.json | 3 ++- lib/helper/Assert.php | 8 +++----- test/unit/AssertTest.php | 8 ++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index f9e25e347..d02a04cb6 100644 --- a/composer.json +++ b/composer.json @@ -14,12 +14,13 @@ ], "require": { "php": ">=5.6", + "egulias/email-validator": "^2.0", "sendgrid/php-http-client": "~3.10", "starkbank/ecdsa": "0.*", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "ext-openssl": "*" + "ext-openssl": "*" }, "require-dev": { "phpunit/phpunit": "^5.7.9 || ^6.4.3", diff --git a/lib/helper/Assert.php b/lib/helper/Assert.php index f1da609a7..b823ac1d1 100644 --- a/lib/helper/Assert.php +++ b/lib/helper/Assert.php @@ -8,6 +8,8 @@ namespace SendGrid\Helper; +use Egulias\EmailValidator\EmailLexer; +use Egulias\EmailValidator\Validation\RFCValidation; use SendGrid\Mail\TypeException; class Assert @@ -47,11 +49,7 @@ public static function email($value, $property, $message = null) { static::string($value, $property, $message); - // Define additional flags for filter_var to verify unicode characters on local part - // Constant FILTER_FLAG_EMAIL_UNICODE is available since PHP 7.1 - $flags = (defined('FILTER_FLAG_EMAIL_UNICODE')) ? FILTER_FLAG_EMAIL_UNICODE : null; - - if (filter_var($value, FILTER_VALIDATE_EMAIL, $flags) === false) { + if ((new RFCValidation())->isValid($value, new EmailLexer()) === false) { $message = sprintf( $message ?: '"$%s" must be a valid email address. Got: %s', $property, diff --git a/test/unit/AssertTest.php b/test/unit/AssertTest.php index 313f325c3..4faca0c82 100644 --- a/test/unit/AssertTest.php +++ b/test/unit/AssertTest.php @@ -41,6 +41,14 @@ public function testEmailThrowExceptionWithDefaultMessage() Assert::email('test', 'test'); } + public function testEmailThrowExceptionWithNonRFCFormatWithDefaultMessage() + { + $this->expectException(TypeException::class); + $this->expectExceptionMessage('"$test" must be a valid email address. Got: test'); + + Assert::email('test sd@test.es', 'test'); + } + public function testEmailThrowExceptionWithCustomMessage() { $this->expectException(TypeException::class);