Skip to content

Commit 758bc51

Browse files
authored
fix(uuid): validation from string (#1172)
1 parent 8be45e6 commit 758bc51

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/validators/uuid.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use pyo3::prelude::*;
55
use pyo3::sync::GILOnceCell;
66
use pyo3::types::{PyDict, PyType};
77
use uuid::Uuid;
8+
use uuid::Variant;
89

910
use crate::build_tools::is_strict;
1011
use crate::errors::{ErrorType, ErrorTypeDefaults, ValError, ValResult};
@@ -125,6 +126,20 @@ impl Validator for UuidValidator {
125126
state.floor_exactness(Exactness::Lax);
126127
}
127128
let uuid = self.get_uuid(input)?;
129+
// This block checks if the UUID version matches the expected version and
130+
// if the UUID variant conforms to RFC 4122. When dealing with Python inputs,
131+
// UUIDs must adhere to RFC 4122 standards.
132+
if let Some(expected_version) = self.version {
133+
if uuid.get_version_num() != expected_version || uuid.get_variant() != Variant::RFC4122 {
134+
return Err(ValError::new(
135+
ErrorType::UuidVersion {
136+
expected_version,
137+
context: None,
138+
},
139+
input,
140+
));
141+
}
142+
}
128143
self.create_py_uuid(py, class, &uuid)
129144
}
130145
}

tests/validators/test_uuid.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
('886313e1-3b8a-5372-9b90-0c9aee199e5d', UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')),
2525
('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11', UUID('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11')),
2626
('00000000-8000-4000-8000-000000000000', UUID('00000000-8000-4000-8000-000000000000')),
27+
('00000000-0000-4000-0000-000000000000', UUID('00000000-0000-4000-0000-000000000000')),
2728
(b'\x12\x34\x56\x78' * 4, UUID('12345678-1234-5678-1234-567812345678')),
2829
(b'\x00\x00\x00\x00' * 4, UUID('00000000-0000-0000-0000-000000000000')),
2930
(b'ebcdab58-6eb8-46fb-a190-d07a33e9eac8', UUID('ebcdab58-6eb8-46fb-a190-d07a33e9eac8')),
@@ -123,6 +124,8 @@ def test_uuid_strict(input_value, expected):
123124
# `UUID.version` makes sense for RFC 4122 UUIDs only. For non RFC 4122 UUIDs Python uses `UUID.version=None`
124125
('00000000-8000-4000-8000-000000000000', 4, UUID('00000000-8000-4000-8000-000000000000')),
125126
(UUID('00000000-8000-4000-8000-000000000000'), 4, UUID('00000000-8000-4000-8000-000000000000')),
127+
('00000000-0000-4000-0000-000000000000', None, UUID('00000000-0000-4000-0000-000000000000')),
128+
(UUID('00000000-0000-4000-0000-000000000000'), None, UUID('00000000-0000-4000-0000-000000000000')),
126129
('00000000-7fff-4000-7fff-000000000000', None, UUID('00000000-7fff-4000-7fff-000000000000')),
127130
(UUID('00000000-7fff-4000-7fff-000000000000'), None, UUID('00000000-7fff-4000-7fff-000000000000')),
128131
(UUID('00000000-7fff-4000-7fff-000000000000'), 4, Err('UUID version 4 expected')),
@@ -138,6 +141,8 @@ def test_uuid_strict(input_value, expected):
138141
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 3, Err('UUID version 3 expected')),
139142
('08ed0736-fb95-5cc5-85ed-37e4f3df9b29', 1, Err('UUID version 1 expected')),
140143
(UUID('08ed0736-fb95-5cc5-85ed-37e4f3df9b29'), 1, Err('UUID version 1 expected')),
144+
('00000000-0000-4000-0000-000000000000', 4, Err('UUID version 4 expected')),
145+
(UUID('00000000-0000-4000-0000-000000000000'), 4, Err('UUID version 4 expected')),
141146
],
142147
)
143148
def test_uuid_version(input_value, version, expected):

0 commit comments

Comments
 (0)