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

Skip to content

Commit c5fca8c

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

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,36 @@ public function testInvalidNonStrictUuidNamed()
263263
->setCode(Uuid::INVALID_CHARACTERS_ERROR)
264264
->assertRaised();
265265
}
266+
267+
/**
268+
* @dataProvider provideUidForTimeBasedAssertion
269+
*/
270+
public function testTimeBasedUuid(string $uid, bool $expectedTimeBased)
271+
{
272+
$constraint = new Uuid([
273+
'versions' => Uuid::TIME_BASED_VERSIONS,
274+
]);
275+
276+
$this->validator->validate($uid, $constraint);
277+
278+
if ($expectedTimeBased) {
279+
$this->assertNoViolation();
280+
} else {
281+
$this->buildViolation('This is not a valid UUID.')
282+
->setParameter('{{ value }}', '"'.$uid.'"')
283+
->setCode(Uuid::INVALID_TIME_BASED_VERSION_ERROR)
284+
->assertRaised();
285+
}
286+
}
287+
288+
public function provideUidForTimeBasedAssertion(): \Generator
289+
{
290+
yield ['95ab107e-6fc2-11ed-9d6b-01bfd6e71dbd', true]; // V1
291+
yield ['5d5b5ae1-5857-3531-9431-e8ac73c3e61d', false]; // V3
292+
yield ['ba6479dd-a5ea-403c-96ae-5964d0582e81', false]; // V4
293+
yield ['fc1cc19d-cb3c-5f6a-a0f6-706424f68e3a', false]; // V5
294+
yield ['1ed6fc29-5ab1-6b6e-8aad-cfa821180d14', true]; // V6
295+
yield ['0184c292-b133-7e10-a3b4-d49c1ab49b2a', true]; // V7
296+
yield ['00112233-4455-8677-8899-aabbccddeeff', false]; // V8
297+
}
266298
}

0 commit comments

Comments
 (0)
0