Skip to content

Commit

Permalink
fix(uuid): validation from string (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanArhancet authored Jan 25, 2024
1 parent 8be45e6 commit 758bc51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/validators/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pyo3::prelude::*;
use pyo3::sync::GILOnceCell;
use pyo3::types::{PyDict, PyType};
use uuid::Uuid;
use uuid::Variant;

use crate::build_tools::is_strict;
use crate::errors::{ErrorType, ErrorTypeDefaults, ValError, ValResult};
Expand Down Expand Up @@ -125,6 +126,20 @@ impl Validator for UuidValidator {
state.floor_exactness(Exactness::Lax);
}
let uuid = self.get_uuid(input)?;
// This block checks if the UUID version matches the expected version and
// if the UUID variant conforms to RFC 4122. When dealing with Python inputs,
// UUIDs must adhere to RFC 4122 standards.
if let Some(expected_version) = self.version {
if uuid.get_version_num() != expected_version || uuid.get_variant() != Variant::RFC4122 {
return Err(ValError::new(
ErrorType::UuidVersion {
expected_version,
context: None,
},
input,
));
}
}
self.create_py_uuid(py, class, &uuid)
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/validators/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
('886313e1-3b8a-5372-9b90-0c9aee199e5d', UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')),
('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11', UUID('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11')),
('00000000-8000-4000-8000-000000000000', UUID('00000000-8000-4000-8000-000000000000')),
('00000000-0000-4000-0000-000000000000', UUID('00000000-0000-4000-0000-000000000000')),
(b'\x12\x34\x56\x78' * 4, UUID('12345678-1234-5678-1234-567812345678')),
(b'\x00\x00\x00\x00' * 4, UUID('00000000-0000-0000-0000-000000000000')),
(b'ebcdab58-6eb8-46fb-a190-d07a33e9eac8', UUID('ebcdab58-6eb8-46fb-a190-d07a33e9eac8')),
Expand Down Expand Up @@ -123,6 +124,8 @@ def test_uuid_strict(input_value, expected):
# `UUID.version` makes sense for RFC 4122 UUIDs only. For non RFC 4122 UUIDs Python uses `UUID.version=None`
('00000000-8000-4000-8000-000000000000', 4, UUID('00000000-8000-4000-8000-000000000000')),
(UUID('00000000-8000-4000-8000-000000000000'), 4, UUID('00000000-8000-4000-8000-000000000000')),
('00000000-0000-4000-0000-000000000000', None, UUID('00000000-0000-4000-0000-000000000000')),
(UUID('00000000-0000-4000-0000-000000000000'), None, UUID('00000000-0000-4000-0000-000000000000')),
('00000000-7fff-4000-7fff-000000000000', None, UUID('00000000-7fff-4000-7fff-000000000000')),
(UUID('00000000-7fff-4000-7fff-000000000000'), None, UUID('00000000-7fff-4000-7fff-000000000000')),
(UUID('00000000-7fff-4000-7fff-000000000000'), 4, Err('UUID version 4 expected')),
Expand All @@ -138,6 +141,8 @@ def test_uuid_strict(input_value, expected):
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 3, Err('UUID version 3 expected')),
('08ed0736-fb95-5cc5-85ed-37e4f3df9b29', 1, Err('UUID version 1 expected')),
(UUID('08ed0736-fb95-5cc5-85ed-37e4f3df9b29'), 1, Err('UUID version 1 expected')),
('00000000-0000-4000-0000-000000000000', 4, Err('UUID version 4 expected')),
(UUID('00000000-0000-4000-0000-000000000000'), 4, Err('UUID version 4 expected')),
],
)
def test_uuid_version(input_value, version, expected):
Expand Down

0 comments on commit 758bc51

Please sign in to comment.