Skip to content

Commit

Permalink
uuid: allow str subclass as input
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed May 18, 2024
1 parent b777774 commit 8f4bc95
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/validators/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::errors::{ErrorType, ErrorTypeDefaults, ValError, ValResult};
use crate::input::input_as_python_instance;
use crate::input::Input;
use crate::input::InputType;
use crate::input::ValidationMatch;
use crate::tools::SchemaDict;

use super::model::create_class;
Expand Down Expand Up @@ -152,7 +153,7 @@ impl Validator for UuidValidator {

impl UuidValidator {
fn get_uuid<'py>(&self, input: &(impl Input<'py> + ?Sized)) -> ValResult<Uuid> {
let uuid = match input.exact_str().ok() {
let uuid = match input.validate_str(true, false).ok().map(ValidationMatch::into_inner) {
Some(either_string) => {
let cow = either_string.as_cow()?;
let uuid_str = cow.as_ref();
Expand Down
4 changes: 4 additions & 0 deletions tests/validators/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from ..conftest import Err, PyAndJson


class MyStr(str): ...


@pytest.mark.parametrize(
'input_value,expected',
[
Expand All @@ -25,6 +28,7 @@
('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')),
(MyStr('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

0 comments on commit 8f4bc95

Please sign in to comment.