8000 [Validator] Add `Uuid::TIME_BASED_VERSIONS` to match that a UUID bein… · symfony/symfony@c08e12f · GitHub
[go: up one dir, main page]

Skip to content

Commit c08e12f

Browse files
[Validator] Add Uuid::TIME_BASED_VERSIONS to match that a UUID being validated embeds a timestamp
1 parent 69f46f2 commit c08e12f

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
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.3
5+
---
6+
7+
* Add `Uuid::TIME_BASED_VERSIONS` to match that a UUID being validated embeds a timestamp
8+
49
6.2
510
---
611

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Uuid extends Constraint
2828
public const INVALID_CHARACTERS_ERROR = '51120b12-a2bc-41bf-aa53-cd73daf330d0';
2929
public const INVALID_HYPHEN_PLACEMENT_ERROR = '98469c83-0309-4f5d-bf95-a496dcaa869c';
3030
public const INVALID_VERSION_ERROR = '21ba13b4-b185-4882-ac6f-d147355987eb';
31+
public const INVALID_TIME_BASED_VERSION_ERROR = '484081ca-6fbd-11ed-ade8-a3bdfd0fcf2f';
3132
public const INVALID_VARIANT_ERROR = '164ef693-2b9d-46de-ad7f-836201f0c2db';
3233

3334
protected const ERROR_NAMES = [
@@ -65,6 +66,12 @@ class Uuid extends Constraint
6566
self::V8_CUSTOM,
6667
];
6768

69+
public const TIME_BASED_VERSIONS = [
70+
self::V1_MAC,
71+
self::V6_SORTABLE,
72+
self::V7_MONOTONIC,
73+
];
74+
6875
/**
6976
* Message to display when validation fails.
7077
*

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,11 @@ private function validateStrict(string $value, Uuid $constraint)
235235

236236
// Check version
237237
if (!\in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
238+
$code = Uuid::TIME_BASED_VERSIONS === $constraint->versions ? Uuid::INVALID_TIME_BASED_VERSION_ERROR : Uuid::INVALID_VERSION_ERROR;
239+
238240
$this->context->buildViolation($constraint->message)
239241
->setParameter('{{ value }}', $this->formatValue($value))
240-
->setCode(Uuid::INVALID_VERSION_ERROR)
242+
->setCode($code)
241243
->addViolation();
242244
}
243245

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Uid\AbstractUid;
15+
use Symfony\Component\Uid\Uuid as UuidComponentObject;
1416
use Symfony\Component\Validator\Constraint;
1517
use Symfony\Component\Validator\Constraints\Uuid;
1618
use Symfony\Component\Validator\Constraints\UuidValidator;
@@ -263,4 +265,36 @@ public function testInvalidNonStrictUuidNamed()
263265
->setCode(Uuid::INVALID_CHARACTERS_ERROR)
264266
->assertRaised();
265267
}
268+
269+
/**
270+
* @dataProvider provideUidForTimeBasedAssertion
271+
*/
272+
public function testTimeBasedUuid(AbstractUid $uid, bool $expectedTimeBased)
273+
{
274+
$constraint = new Uuid([
275+
'versions' => Uuid::TIME_BASED_VERSIONS,
276+
]);
277+
278+
$this->validator->validate($uid, $constraint);
279+
280+
if ($expectedTimeBased) {
281+
$this->assertNoViolation();
282+
} else {
283+
$this->buildViolation('This is not a valid UUID.')
284+
->setParameter('{{ value }}', '"'.$uid->toRfc4122().'"')
285+
->setCode(Uuid::INVALID_TIME_BASED_VERSION_ERROR)
286+
->assertRaised();
287+
}
288+
}
289+
290+
public function provideUidForTimeBasedAssertion(): \Generator
291+
{
292+
yield [UuidComponentObject::v1(), true];
293+
yield [UuidComponentObject::v3(UuidComponentObject::v1(), 'foo'), false];
294+
yield [UuidComponentObject::v4(), false];
295+
yield [UuidComponentObject::v5(UuidComponentObject::v1(), 'foo'), false];
296+
yield [UuidComponentObject::v6(), true];
297+
yield [UuidComponentObject::v7(), true];
298+
yield [UuidComponentObject::v8('00112233-4455-8677-8899-aabbccddeeff'), false];
299+
}
266300
}

0 commit comments

Comments
 (0)
0