Skip to content

Commit a0e60bd

Browse files
authored
Add support for UUIDv6 and UUIDv8 validation (#1636)
1 parent 741961c commit a0e60bd

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

python/pydantic_core/core_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ class UuidSchema(TypedDict, total=False):
14461446

14471447
def uuid_schema(
14481448
*,
1449-
version: Literal[1, 3, 4, 5, 7] | None = None,
1449+
version: Literal[1, 3, 4, 5, 6, 7, 8] | None = None,
14501450
strict: bool | None = None,
14511451
ref: str | None = None,
14521452
metadata: dict[str, Any] | None = None,

src/validators/uuid.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ enum Version {
4242
UUIDv3 = 3,
4343
UUIDv4 = 4,
4444
UUIDv5 = 5,
45+
UUIDv6 = 6,
4546
UUIDv7 = 7,
47+
UUIDv8 = 8,
4648
}
4749

4850
impl From<Version> for usize {
@@ -58,7 +60,9 @@ impl From<u8> for Version {
5860
3 => Version::UUIDv3,
5961
4 => Version::UUIDv4,
6062
5 => Version::UUIDv5,
63+
6 => Version::UUIDv6,
6164
7 => Version::UUIDv7,
65+
8 => Version::UUIDv8,
6266
_ => unreachable!(),
6367
}
6468
}
@@ -136,8 +140,8 @@ impl Validator for UuidValidator {
136140
}
137141
let uuid = self.get_uuid(input)?;
138142
// This block checks if the UUID version matches the expected version and
139-
// if the UUID variant conforms to RFC 4122. When dealing with Python inputs,
140-
// UUIDs must adhere to RFC 4122 standards.
143+
// if the UUID variant conforms to RFC 9562 (superseding RFC 4122).
144+
// When dealing with Python inputs, UUIDs must adhere to RFC 9562 standards.
141145
if let Some(expected_version) = self.version {
142146
if uuid.get_version_num() != expected_version || uuid.get_variant() != Variant::RFC4122 {
143147
return Err(ValError::new(

tests/validators/test_uuid.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class MyStr(str): ...
2626
('6ba7b810-9dad-11d1-80b4-00c04fd430c8', UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')),
2727
('886313e1-3b8a-5372-9b90-0c9aee199e5d', UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')),
2828
('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11', UUID('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11')),
29+
('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05', UUID('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05')),
2930
('0194fdc2-5d6a-733c-97f9-2feeb9d2a609', UUID('0194fdc2-5d6a-733c-97f9-2feeb9d2a609')),
31+
('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11', UUID('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11')),
3032
('00000000-8000-4000-8000-000000000000', UUID('00000000-8000-4000-8000-000000000000')),
3133
('00000000-0000-4000-0000-000000000000', UUID('00000000-0000-4000-0000-000000000000')),
3234
(MyStr('00000000-0000-4000-0000-000000000000'), UUID('00000000-0000-4000-0000-000000000000')),
@@ -126,6 +128,11 @@ def test_uuid_strict(input_value, expected):
126128
('0e7ac198-9acd-4c0c-b4b4-761974bf71d7', 4, UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7')),
127129
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 4, UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7')),
128130
('0194fdc2-5d6a-733c-97f9-2feeb9d2a609', 7, UUID('0194fdc2-5d6a-733c-97f9-2feeb9d2a609')),
131+
(UUID('0194fdc2-5d6a-733c-97f9-2feeb9d2a609'), 7, UUID('0194fdc2-5d6a-733c-97f9-2feeb9d2a609')),
132+
('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05', 6, UUID('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05')),
133+
(UUID('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05'), 6, UUID('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05')),
134+
('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11', 8, UUID('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11')),
135+
(UUID('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11'), 8, UUID('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11')),
129136
# Cases from pydantic#7355 and pydantic#7537
130137
# `UUID.version` makes sense for RFC 4122 UUIDs only. For non RFC 4122 UUIDs Python uses `UUID.version=None`
131138
('00000000-8000-4000-8000-000000000000', 4, UUID('00000000-8000-4000-8000-000000000000')),
@@ -139,6 +146,10 @@ def test_uuid_strict(input_value, expected):
139146
(UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3'), None, UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3')),
140147
(UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3'), 4, Err('UUID version 4 expected')),
141148
# Invalid UUIDs
149+
('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05', 8, Err('UUID version 8 expected')),
150+
(UUID('1efea93d-7bb8-6ea0-afdc-e76cbc0c8e05'), 8, Err('UUID version 8 expected')),
151+
('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11', 6, Err('UUID version 6 expected')),
152+
(UUID('c0a8f9a8-aa5e-882b-a067-9cb3a51f5c11'), 6, Err('UUID version 6 expected')),
142153
('a6cc5730-2261-11ee-9c43-2eb5a363657c', 7, Err('UUID version 7 expected')),
143154
(UUID('a6cc5730-2261-11ee-9c43-2eb5a363657c'), 7, Err('UUID version 7 expected')),
144155
('a6cc5730-2261-11ee-9c43-2eb5a363657c', 5, Err('UUID version 5 expected')),

0 commit comments

Comments
 (0)