Skip to content
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

Add FILTER_FLAG_EMAIL_UNICODE to support email addresses with unicode characters #160

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion src/Constraint/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function validationError($format, $data)
case self::IRI:
return Iri::validationError($data);
case self::EMAIL:
return filter_var($data, FILTER_VALIDATE_EMAIL) ? null : 'Invalid email';
return filter_var($data, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE) ? null : 'Invalid email';
case self::IDN_EMAIL:
return count(explode('@', $data, 3)) === 2 ? null : 'Invalid email';
case self::IPV4:
Expand Down
78 changes: 78 additions & 0 deletions tests/src/PHPUnit/Suite/Issue159Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Swaggest\JsonSchema\Tests\PHPUnit\Suite;

use Swaggest\JsonSchema\Exception\StringException;
use Swaggest\JsonSchema\InvalidValue;
use Swaggest\JsonSchema\Schema;

class Issue159Test extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider emailsProvider
*/
public function testIssue($expected, $email)
{
$schemaData = json_decode('{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Root",
"type": "string",
"format": "email"
}');

$schema = Schema::import($schemaData);

try {
$schema->in($email);
$this->assertTrue(true);
} catch (InvalidValue $e) {
$this->assertEquals('Invalid email', $e->getMessage());
$this->assertFalse($expected);
}
}

private function emailsProvider()
{
return [
[true, '[email protected]'],
[true, '[email protected]'],
[true, 'ser+mailbox/[email protected]'],
[true, '#$%&\'*+-/=?^_`.{|}[email protected]'],
[true, '"Abc@def"@example.com'],
[true, '"Fred\ Bloggs"@example.com'],
[true, '"Joe.\\Blow"@example.com'],
[false, 'коля@пример.рф'], // valid but FILTER_FLAG_EMAIL_UNICODE supports only unicode in the email address local part. See https://www.php.net/manual/en/filter.constants.php#constant.filter-flag-email-unicode
[false, '用户@例子.广告'], // valid but FILTER_FLAG_EMAIL_UNICODE supports only unicode in the email address local part. See https://www.php.net/manual/en/filter.constants.php#constant.filter-flag-email-unicode
[false, 'ಬೆಂಬಲ@ಡೇಟಾಮೇಲ್.ಭಾರತ'], // valid but FILTER_FLAG_EMAIL_UNICODE supports only unicode in the email address local part. See https://www.php.net/manual/en/filter.constants.php#constant.filter-flag-email-unicode
[false, 'अजय@डाटा.भारत'], // valid but FILTER_FLAG_EMAIL_UNICODE supports only unicode in the email address local part. See https://www.php.net/manual/en/filter.constants.php#constant.filter-flag-email-unicode
[false, 'квіточка@пошта.укр'], // valid but FILTER_FLAG_EMAIL_UNICODE supports only unicode in the email address local part. See https://www.php.net/manual/en/filter.constants.php#constant.filter-flag-email-unicode
[false, 'χρήστης@παράδειγμα.ελ'], // valid but FILTER_FLAG_EMAIL_UNICODE supports only unicode in the email address local part. See https://www.php.net/manual/en/filter.constants.php#constant.filter-flag-email-unicode
[false, 'Dörte@Sörensen.example.com'], // valid but FILTER_FLAG_EMAIL_UNICODE supports only unicode in the email address local part. See https://www.php.net/manual/en/filter.constants.php#constant.filter-flag-email-unicode
[true, 'коля@example.com'],
[true, '用户@example.com'],
[false, 'ಬೆಂಬಲ@example.com'], // valid and should be supported but it's not.
[true, 'अजय@example.com'],
[true, 'квіточка@example.com'],
[true, 'χρήστης@example.com'],
[true, 'Dö[email protected]'],
[true, '[email protected]'],
[true, '[email protected]'],
[true, '[email protected]'],
[true, '[email protected]'],
[true, '[email protected]'],
[true, '[email protected]'],
[true, 'name/[email protected]'],
[false, 'admin@example'], // local domain name with no TLD, although ICANN highly discourages dotless email addresses. See https://en.wikipedia.org/wiki/Email_address#cite_note-29)
[true, '[email protected]'],
[false, '" "@example.org'], // valid. See https://en.wikipedia.org/wiki/Email_address#Internationalization
[true, '"john..doe"@example.org'],
[true, '[email protected]'],
[true, '"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com'],
[true, 'user%[email protected]'],
[true, '[email protected]'],
[true, 'postmaster@[123.123.123.123]'],
[true, 'postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]'],
[true, '_test@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]'],
];
}
}