Skip to content

[Validator] prepare for changing the default value of the requireTld option #54588

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

Merged
merged 1 commit into from
Apr 17, 2024
Merged
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 UPGRADE-7.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ TwigBundle
Validator
---------

* Deprecate not passing a value for the `requireTld` option to the `Url` constraint (the default value will become `true` in 8.0)
* Deprecate `Bic::INVALID_BANK_CODE_ERROR`

Workflow
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.1
---

* Deprecate not passing a value for the `requireTld` option to the `Url` constraint (the default value will become `true` in 8.0)
* Add the calculated strength to violations in `PasswordStrengthValidator`
* Add support for `Stringable` values when using the `Cidr`, `CssColor`, `ExpressionSyntax` and `PasswordStrength` constraints
* Add `MacAddress` constraint
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function __construct(
) {
parent::__construct($options, $groups, $payload);

if (null === ($options['requireTld'] ?? $requireTld)) {
trigger_deprecation('symfony/validator', '7.1', 'Not passing a value for the "requireTld" option to the Url constraint is deprecated. Its default value will change to "true".');
}

$this->message = $message ?? $this->message;
$this->protocols = $protocols ?? $this->protocols;
$this->relativeProtocol = $relativeProtocol ?? $this->relativeProtocol;
Expand Down
22 changes: 16 additions & 6 deletions src/Symfony/Component/Validator/Tests/Constraints/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class UrlTest extends TestCase
{
public function testNormalizerCanBeSet()
{
$url = new Url(['normalizer' => 'trim']);
$url = new Url(['normalizer' => 'trim', 'requireTld' => true]);

$this->assertEquals('trim', $url->normalizer);
}
Expand All @@ -33,14 +33,14 @@ public function testInvalidNormalizerThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("string" given).');
new Url(['normalizer' => 'Unknown Callable']);
new Url(['normalizer' => 'Unknown Callable', 'requireTld' => true]);
}

public function testInvalidNormalizerObjectThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
new Url(['normalizer' => new \stdClass()]);
new Url(['normalizer' => new \stdClass(), 'requireTld' => true]);
}

public function testAttributes()
Expand Down Expand Up @@ -73,17 +73,27 @@ public function testAttributes()
self::assertNull($aConstraint->normalizer);
self::assertTrue($dConstraint->requireTld);
}

/**
* @group legacy
*/
public function testRequireTldDefaultsToFalse()
{
$constraint = new Url();

$this->assertFalse($constraint->requireTld);
}
}

class UrlDummy
{
#[Url]
#[Url(requireTld: false)]
private $a;

#[Url(message: 'myMessage', protocols: ['ftp', 'gopher'], normalizer: 'trim')]
#[Url(message: 'myMessage', protocols: ['ftp', 'gopher'], normalizer: 'trim', requireTld: false)]
private $b;

#[Url(relativeProtocol: true, groups: ['my_group'], payload: 'some attached data')]
#[Url(relativeProtocol: true, groups: ['my_group'], payload: 'some attached data', requireTld: false)]
private $c;

#[Url(requireTld: true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ protected function createValidator(): UrlValidator

public function testNullIsValid()
{
$this->validator->validate(null, new Url());
$this->validator->validate(null, new Url(requireTld: true));

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new Url());
$this->validator->validate('', new Url(requireTld: true));

$this->assertNoViolation();
}

public function testEmptyStringFromObjectIsValid()
{
$this->validator->validate(new EmailProvider(), new Url());
$this->validator->validate(new EmailProvider(), new Url(requireTld: true));

$this->assertNoViolation();
}

public function testExpectsStringCompatibleType()
{
$this->expectException(UnexpectedValueException::class);
$this->validator->validate(new \stdClass(), new Url());
$this->validator->validate(new \stdClass(), new Url(requireTld: true));
}

/**
* @dataProvider getValidUrls
*/
public function testValidUrls($url)
{
$this->validator->validate($url, new Url());
$this->validator->validate($url, new Url(requireTld: false));

$this->assertNoViolation();
}
Expand All @@ -65,7 +65,10 @@ public function testValidUrls($url)
*/
public function testValidUrlsWithWhitespaces($url)
{
$this->validator->validate($url, new Url(['normalizer' => 'trim']));
$this->validator->validate($url, new Url([
'normalizer' => 'trim',
'requireTld' => true,
]));

$this->assertNoViolation();
}
Expand All @@ -78,6 +81,7 @@ public function testValidRelativeUrl($url)
{
$constraint = new Url([
'relativeProtocol' => true,
'requireTld' => false,
]);

$this->validator->validate($url, $constraint);
Expand Down Expand Up @@ -196,6 +200,7 @@ public function testInvalidUrls($url)
{
$constraint = new Url([
'message' => 'myMessage',
'requireTld' => false,
]);

$this->validator->validate($url, $constraint);
Expand All @@ -215,6 +220,7 @@ public function testInvalidRelativeUrl($url)
$constraint = new Url([
'message' => 'myMessage',
'relativeProtocol' => true,
'requireTld' => false,
]);

$this->validator->validate($url, $constraint);
Expand Down Expand Up @@ -292,10 +298,11 @@ public static function getInvalidUrls()
/**
* @dataProvider getValidCustomUrls
*/
public function testCustomProtocolIsValid($url)
public function testCustomProtocolIsValid($url, $requireTld)
{
$constraint = new Url([
'protocols' => ['ftp', 'file', 'git'],
'requireTld' => $requireTld,
]);

$this->validator->validate($url, $constraint);
Expand All @@ -306,9 +313,9 @@ public function testCustomProtocolIsValid($url)
public static function getValidCustomUrls()
{
return [
['ftp://example.com'],
['file://127.0.0.1'],
['git://[::1]/'],
['ftp://example.com', true],
['file://127.0.0.1', false],
['git://[::1]/', false],
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php83": "^1.27",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/translation-contracts": "^2.5|^3"
},
"require-dev": {
Expand Down