Skip to content

Changed Assert::email validation by egulias email RFCValidation #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"require": {
"php": ">=5.6",
"egulias/email-validator": "^2.0",
"sendgrid/php-http-client": "~3.10",
"starkbank/ecdsa": "0.*",
"ext-curl": "*",
Expand Down
8 changes: 3 additions & 5 deletions lib/helper/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace SendGrid\Helper;

use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Validation\RFCValidation;
use SendGrid\Mail\TypeException;

class Assert
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions test/unit/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]', 'test');
}

public function testEmailThrowExceptionWithCustomMessage()
{
$this->expectException(TypeException::class);
Expand Down