Skip to content

Commit d7a1ed0

Browse files
committed
fix detecting missing required options
1 parent b415afc commit d7a1ed0

File tree

13 files changed

+97
-12
lines changed

13 files changed

+97
-12
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617

1718
/**
1819
* When applied to an array (or Traversable object), this constraint allows you to apply
@@ -32,6 +33,10 @@ class All extends Composite
3233
#[HasNamedArguments]
3334
public function __construct(mixed $constraints = null, ?array $groups = null, mixed $payload = null)
3435
{
36+
if (null === $constraints || [] === $constraints) {
37+
throw new MissingOptionsException(\sprintf('The options "constraints" must be set for constraint "%s".', self::class), ['constraints']);
38+
}
39+
3540
if (!$constraints instanceof Constraint && !\is_array($constraints) || \is_array($constraints) && !array_is_list($constraints)) {
3641
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
3742

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617

1718
/**
1819
* Checks that at least one of the given constraint is satisfied.
@@ -43,6 +44,10 @@ class AtLeastOneOf extends Composite
4344
#[HasNamedArguments]
4445
public function __construct(mixed $constraints = null, ?array $groups = null, mixed $payload = null, ?string $message = null, ?string $messageCollection = null, ?bool $includeInternalMessages = null)
4546
{
47+
if (null === $constraints || [] === $constraints) {
48+
throw new MissingOptionsException(\sprintf('The options "constraints" must be set for constraint "%s".', self::class), ['constraints']);
49+
}
50+
4651
if (!$constraints instanceof Constraint && !\is_array($constraints) || \is_array($constraints) && !array_is_list($constraints)) {
4752
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
4853
$options = $constraints;

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617

1718
/**
1819
* Validates a credit card number for a given credit card company.
@@ -55,18 +56,19 @@ class CardScheme extends Constraint
5556
#[HasNamedArguments]
5657
public function __construct(array|string|null $schemes, ?string $message = null, ?array $groups = null, mixed $payload = null, ?array $options = null)
5758
{
59+
if (null === $schemes && !isset($options['schemes'])) {
60+
throw new MissingOptionsException(\sprintf('The options "schemes" must be set for constraint "%s".', self::class), ['schemes']);
61+
}
62+
5863
if (\is_array($schemes) && \is_string(key($schemes))) {
5964
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
6065

6166
$options = array_merge($schemes, $options ?? []);
67+
$schemes = null;
6268
} else {
6369
if (\is_array($options)) {
6470
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
6571
}
66-
67-
if (null !== $schemes) {
68-
$options['value'] = $schemes;
69-
}
7072
}
7173

7274
parent::__construct($options, $groups, $payload);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1717
use Symfony\Component\Validator\Constraint;
1818
use Symfony\Component\Validator\Exception\LogicException;
19+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1920

2021
/**
2122
* Validates a value using an expression from the Expression Language component.
@@ -60,6 +61,10 @@ public function __construct(
6061
throw new LogicException(\sprintf('The "symfony/expression-language" component is required to use the "%s" constraint. Try running "composer require symfony/expression-language".', __CLASS__));
6162
}
6263

64+
if (null === $expression && !isset($options['expression'])) {
65+
throw new MissingOptionsException(\sprintf('The options "expression" must be set for constraint "%s".', self::class), ['expression']);
66+
}
67+
6368
if (\is_array($expression)) {
6469
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
6570

@@ -69,10 +74,6 @@ public function __construct(
6974
if (\is_array($options)) {
7075
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
7176
}
72-
73-
if (null !== $expression) {
74-
$options['value'] = $expression;
75-
}
7677
}
7778

7879
parent::__construct($options, $groups, $payload);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\Exception\InvalidArgumentException;
17+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1718

1819
/**
1920
* Validates that a value matches a regular expression.
@@ -54,15 +55,17 @@ public function __construct(
5455
mixed $payload = null,
5556
?array $options = null,
5657
) {
58+
if (null === $pattern && !isset($options['pattern'])) {
59+
throw new MissingOptionsException(\sprintf('The options "pattern" must be set for constraint "%s".', self::class), ['pattern']);
60+
}
61+
5762
if (\is_array($pattern)) {
5863
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
5964

6065
$options = array_merge($pattern, $options ?? []);
6166
$pattern = $options['pattern'] ?? null;
62-
} elseif (null !== $pattern) {
63-
if (\is_array($options)) {
64-
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
65-
}
67+
} elseif (\is_array($options)) {
68+
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
6669
}
6770

6871
parent::__construct($options, $groups, $payload);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617

1718
/**
1819
* Use this constraint to sequentially validate nested constraints.
@@ -32,6 +33,10 @@ class Sequentially extends Composite
3233
#[HasNamedArguments]
3334
public function __construct(mixed $constraints = null, ?array $groups = null, mixed $payload = null)
3435
{
36+
if (null === $constraints || [] === $constraints) {
37+
throw new MissingOptionsException(\sprintf('The options "constraints" must be set for constraint "%s".', self::class), ['constraints']);
38+
}
39+
3540
if (!$constraints instanceof Constraint && !\is_array($constraints) || \is_array($constraints) && !array_is_list($constraints)) {
3641
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
3742
$options = $constraints;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617

1718
/**
1819
* Validates that a value is of a specific data type.
@@ -39,6 +40,10 @@ class Type extends Constraint
3940
#[HasNamedArguments]
4041
public function __construct(string|array|null $type, ?string $message = null, ?array $groups = null, mixed $payload = null, ?array $options = null)
4142
{
43+
if (null === $type && !isset($options['type'])) {
44+
throw new MissingOptionsException(\sprintf('The options "type" must be set for constraint "%s".', self::class), ['type']);
45+
}
46+
4247
if (\is_array($type) && \is_string(key($type))) {
4348
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
4449

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1717
use Symfony\Component\Validator\Constraint;
1818
use Symfony\Component\Validator\Exception\LogicException;
19+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1920

2021
/**
2122
* Conditionally apply validation constraints based on an expression using the ExpressionLanguage syntax.
@@ -59,6 +60,10 @@ public function __construct(string|Expression|array|\Closure $expression, array|
5960
}
6061
$options['otherwise'] = $otherwise;
6162
} else {
63+
if (null === $constraints) {
64+
throw new MissingOptionsException(\sprintf('The options "constraints" must be set for constraint "%s".', self::class), ['constraints']);
65+
}
66+
6267
$this->expression = $expression;
6368
$this->constraints = $constraints;
6469
$this->otherwise = $otherwise;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public function testMissingSchemes()
4646

4747
new CardScheme(null);
4848
}
49+
50+
/**
51+
* @group legacy
52+
*/
53+
public function testSchemesInOptionsArray()
54+
{
55+
$constraint = new CardScheme(null, options: ['schemes' => [CardScheme::MASTERCARD]]);
56+
57+
$this->assertSame([CardScheme::MASTERCARD], $constraint->schemes);
58+
}
4959
}
5060

5161
class CardSchemeDummy

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ public function testInitializeWithOptionsArray()
7373

7474
$this->assertSame('!this.getParent().get("field2").getData()', $constraint->expression);
7575
}
76+
77+
/**
78+
* @group legacy
79+
*/
80+
public function testExpressionInOptionsArray()
81+
{
82+
$constraint = new Expression(null, options: ['expression' => '!this.getParent().get("field2").getData()']);
83+
84+
$this->assertSame('!this.getParent().get("field2").getData()', $constraint->expression);
85+
}
7686
}
7787

7888
class ExpressionDummy

0 commit comments

Comments
 (0)