Skip to content

Commit 05f9f45

Browse files
committed
support Stringable values in all constraints
1 parent 7269a3d commit 05f9f45

File tree

11 files changed

+69
-16
lines changed

11 files changed

+69
-16
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.1
55
---
66

7+
* Add support for `Stringable` values when using the `Cidr`, `CssColor`, `ExpressionSyntax` and `PasswordStrength` constraints
78
* Add `MacAddress` constraint
89
* Add `list` and `associative_array` types to `Type` constraint
910
* Add the `Charset` constraint

src/Symfony/Component/Validator/Constraints/CidrValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function validate($value, Constraint $constraint): void
2828
return;
2929
}
3030

31-
if (!\is_string($value)) {
31+
if (!\is_string($value) && !$value instanceof \Stringable) {
3232
throw new UnexpectedValueException($value, 'string');
3333
}
3434

src/Symfony/Component/Validator/Constraints/CssColorValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function validate($value, Constraint $constraint): void
6262
return;
6363
}
6464

65-
if (!\is_string($value)) {
65+
if (!\is_string($value) && !$value instanceof \Stringable) {
6666
throw new UnexpectedValueException($value, 'string');
6767
}
6868

@@ -76,7 +76,7 @@ public function validate($value, Constraint $constraint): void
7676
}
7777

7878
$this->context->buildViolation($constraint->message)
79-
->setParameter('{{ value }}', $this->formatValue($value))
79+
->setParameter('{{ value }}', $this->formatValue((string) $value))
8080
->setCode(CssColor::INVALID_FORMAT_ERROR)
8181
->addViolation();
8282
}

src/Symfony/Component/Validator/Constraints/ExpressionSyntaxValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function validate(mixed $expression, Constraint $constraint): void
4040
return;
4141
}
4242

43-
if (!\is_string($expression)) {
43+
if (!\is_string($expression) && !$expression instanceof \Stringable) {
4444
throw new UnexpectedValueException($expression, 'string');
4545
}
4646

src/Symfony/Component/Validator/Constraints/PasswordStrengthValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public function validate(#[\SensitiveParameter] mixed $value, Constraint $constr
3636
return;
3737
}
3838

39-
if (!\is_string($value)) {
39+
if (!\is_string($value) && !$value instanceof \Stringable) {
4040
throw new UnexpectedValueException($value, 'string');
4141
}
4242
$passwordStrengthEstimator = $this->passwordStrengthEstimator ?? self::estimateStrength(...);
43-
$strength = $passwordStrengthEstimator($value);
43+
$strength = $passwordStrengthEstimator((string) $value);
4444

4545
if ($strength < $constraint->minScore) {
4646
$this->context->buildViolation($constraint->message)

src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\CharsetValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1717
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;
1819

1920
class CharsetValidatorTest extends ConstraintValidatorTestCase
2021
{
@@ -66,12 +67,7 @@ public static function provideValidValues()
6667
yield ['my ûtf 8', ['ASCII', 'UTF-8']];
6768
yield ['my ûtf 8', ['UTF-8']];
6869
yield ['string', ['ISO-8859-1']];
69-
yield [new class() implements \Stringable {
70-
public function __toString(): string
71-
{
72-
return 'my ûtf 8';
73-
}
74-
}, ['UTF-8']];
70+
yield [new StringableValue('my ûtf 8'), ['UTF-8']];
7571
}
7672

7773
public static function provideInvalidValues()

src/Symfony/Component/Validator/Tests/Constraints/CidrValidatorTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1919
use Symfony\Component\Validator\Exception\UnexpectedValueException;
2020
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
21+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;
2122

2223
class CidrValidatorTest extends ConstraintValidatorTestCase
2324
{
@@ -83,7 +84,7 @@ public function testInvalidIpValue(string $cidr)
8384
/**
8485
* @dataProvider getValid
8586
*/
86-
public function testValidCidr(string $cidr, string $version)
87+
public function testValidCidr(string|\Stringable $cidr, string $version)
8788
{
8889
$this->validator->validate($cidr, new Cidr(['version' => $version]));
8990

@@ -93,7 +94,7 @@ public function testValidCidr(string $cidr, string $version)
9394
/**
9495
* @dataProvider getWithInvalidMasksAndIps
9596
*/
96-
public function testInvalidIpAddressAndNetmask(string $cidr)
97+
public function testInvalidIpAddressAndNetmask(string|\Stringable $cidr)
9798
{
9899
$this->validator->validate($cidr, new Cidr());
99100
$this
@@ -195,6 +196,7 @@ public static function getValid(): array
195196
['::255.255.255.255/32', Ip::V6],
196197
['::123.45.67.178/120', Ip::V6],
197198
['::123.45.67.178/120', Ip::ALL],
199+
[new StringableValue('::123.45.67.178/120'), Ip::ALL],
198200
];
199201
}
200202

@@ -233,6 +235,7 @@ public static function getWithInvalidMasksAndIps(): array
233235
['::0.0.0/a/'],
234236
['::256.0.0.0/-1aa'],
235237
['::0.256.0.0/1b'],
238+
[new StringableValue('::0.256.0.0/1b')],
236239
];
237240
}
238241

src/Symfony/Component/Validator/Tests/Constraints/CssColorValidatorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\CssColorValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1717
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;
1819

1920
final class CssColorValidatorTest extends ConstraintValidatorTestCase
2021
{
@@ -67,6 +68,7 @@ public static function getValidAnyColor(): array
6768
['rgba(255, 255, 255, 0.3)'],
6869
['hsl(0, 0%, 20%)'],
6970
['hsla(0, 0%, 20%, 0.4)'],
71+
[new StringableValue('hsla(0, 0%, 20%, 0.4)')],
7072
];
7173
}
7274

@@ -323,7 +325,7 @@ public function testInvalidNamedColors($cssColor)
323325

324326
public static function getInvalidNamedColors(): array
325327
{
326-
return [['fabpot'], ['ngrekas'], ['symfony'], ['FABPOT'], ['NGREKAS'], ['SYMFONY']];
328+
return [['fabpot'], ['ngrekas'], ['symfony'], ['FABPOT'], ['NGREKAS'], ['SYMFONY'], [new StringableValue('SYMFONY')]];
327329
}
328330

329331
/**

src/Symfony/Component/Validator/Tests/Constraints/ExpressionSyntaxValidatorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\ExpressionSyntax;
1616
use Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator;
1717
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;
1819

1920
class ExpressionSyntaxValidatorTest extends ConstraintValidatorTestCase
2021
{
@@ -47,6 +48,16 @@ public function testExpressionValid()
4748
$this->assertNoViolation();
4849
}
4950

51+
public function testStringableExpressionValid()
52+
{
53+
$this->validator->validate(new StringableValue('1 + 1'), new ExpressionSyntax([
54+
'message' => 'myMessage',
55+
'allowedVariables' => [],
56+
]));
57+
58+
$this->assertNoViolation();
59+
}
60+
5061
public function testExpressionWithoutNames()
5162
{
5263
$this->validator->validate('1 + 1', new ExpressionSyntax([
@@ -79,4 +90,18 @@ public function testExpressionIsNotValid()
7990
->setCode(ExpressionSyntax::EXPRESSION_SYNTAX_ERROR)
8091
->assertRaised();
8192
}
93+
94+
public function testStringableExpressionIsNotValid()
95+
{
96+
$this->validator->validate(new StringableValue('a + 1'), new ExpressionSyntax([
97+
'message' => 'myMessage',
98+
'allowedVariables' => [],
99+
]));
100+
101+
$this->buildViolation('myMessage')
102+
->setParameter('{{ syntax_error }}', '"Variable "a" is not valid around position 1 for expression `a + 1`."')
103+
->setInvalidValue('a + 1')
104+
->setCode(ExpressionSyntax::EXPRESSION_SYNTAX_ERROR)
105+
->assertRaised();
106+
}
82107
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints\Fixtures;
13+
14+
class StringableValue implements \Stringable
15+
{
16+
public function __construct(private string $value)
17+
{
18+
}
19+
20+
public function __toString(): string
21+
{
22+
return $this->value;
23+
}
24+
}

0 commit comments

Comments
 (0)