8000 [Validator] Allow single integer for the `versions` option of the `Uu… · symfony/symfony@4e568c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e568c1

Browse files
[Validator] Allow single integer for the versions option of the Uuid constraint
1 parent 2979e1e commit 4e568c1

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Allow single integer for the `versions` option of the `Uuid` constraint
8+
49
6.3
510
---
611

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ class Uuid extends Constraint
8989
public $strict = true;
9090

9191
/**
92-
* Array of allowed versions (see version constants above).
92+
* Specific version or array of allowed versions (see version constants above).
9393
*
9494
* All UUID versions are allowed by default
9595
*
96-
* @var int[]
96+
* @var int[]|int
9797
*/
9898
public $versions = self::ALL_VERSIONS;
9999

105105
public function __construct(
106106
array $options = null,
107107
string $message = null,
108-
array $versions = null,
108+
array|int $versions = null,
109109
bool $strict = null,
110110
callable $normalizer = null,
111111
array $groups = null,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private function validateStrict(string $value, Uuid $constraint): void
237237
}
238238

239239
// Check version
240-
if (!\in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
240+
if (!\in_array($value[self::STRICT_VERSION_POSITION], (array) $constraint->versions)) {
241241
$code = Uuid::TIME_BASED_VERSIONS === $constraint->versions ? Uuid::INVALID_TIME_BASED_VERSION_ERROR : Uuid::INVALID_VERSION_ERROR;
242242

243243
$this->context->buildViolation($constraint->message)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,13 @@ public static function getUuidForTimeBasedAssertions(): \Generator
296296
yield Uuid::V7_MONOTONIC => ['0184c292-b133-7e10-a3b4-d49c1ab49b2a', true];
297297
yield Uuid::V8_CUSTOM => ['00112233-4455-8677-8899-aabbccddeeff', false];
298298
}
299+
300+
public function testAcceptsSingleIntegerAsVersion()
301+
{
302+
$constraint = new Uuid(versions: 7);
303+
304+
$this->validator->validate('0184c292-b133-7e10-a3b4-d49c1ab49b2a', $constraint);
305+
306+
$this->assertNoViolation();
307+
}
299308
}

0 commit comments

Comments
 (0)
0