Skip to content

Commit e8b7ce1

Browse files
committed
clean up version check
1 parent 93103c1 commit e8b7ce1

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

src/uuid.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ impl PyUuid {
4646

4747
#[getter]
4848
pub fn urn(&self) -> &str {
49-
// self.lib_uuid.urn().encode_lower(&mut Uuid::encode_buffer())
49+
let mut buffer = Uuid::encode_buffer();
50+
self.lib_uuid.urn().encode_lower(&mut buffer);
5051
"foo"
5152
}
5253

src/validators/uuid.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::build_tools::SchemaDict;
88
use crate::errors::{ErrorType, ValError, ValResult};
99
use crate::input::Input;
1010
use crate::recursion_guard::RecursionGuard;
11+
use crate::uuid::PyUuid;
1112

1213
use super::{BuildValidator, CombinedValidator, Definitions, DefinitionsBuilder, Extra, Validator};
1314

@@ -41,8 +42,10 @@ impl Validator for UuidValidator {
4142
_definitions: &'data Definitions<CombinedValidator>,
4243
_recursion_guard: &'s mut RecursionGuard,
4344
) -> ValResult<'data, PyObject> {
44-
// Ok(input.clone().into_py(py))
45-
Ok(input.to_object(py))
45+
match self.get_uuid(input) {
46+
Ok(lib_uuid) => Ok(PyUuid::new(lib_uuid).into_py(py)),
47+
Err(error_type) => Err(error_type),
48+
}
4649
}
4750

4851
fn different_strict_behavior(
@@ -64,7 +67,7 @@ impl Validator for UuidValidator {
6467

6568
#[allow(dead_code)]
6669
impl UuidValidator {
67-
fn get_uuid<'s, 'data>(&'s self, input: &'data impl Input<'data>, _strict: bool) -> ValResult<'data, Uuid> {
70+
fn get_uuid<'s, 'data>(&'s self, input: &'data impl Input<'data>) -> ValResult<'data, Uuid> {
6871
if let Some(py_uuid) = input.input_as_uuid() {
6972
let lib_uuid = py_uuid.into_uuid();
7073
self.check_version(input, lib_uuid)?;
@@ -76,33 +79,19 @@ impl UuidValidator {
7679

7780
fn check_version<'s, 'data>(&self, input: &'data impl Input<'data>, uuid: Uuid) -> ValResult<'data, ()> {
7881
if let Some(schema_version) = self.version {
79-
match uuid.get_version() {
80-
Some(_version) => {
81-
// TODO(martinabeleda): need to map `uuid::Version` enum to usize
82-
let version = 4;
83-
if version == schema_version {
84-
return Ok(());
85-
} else {
86-
return Err(ValError::new(
87-
ErrorType::UuidVersionMismatch {
88-
version,
89-
schema_version,
90-
},
91-
input,
92-
));
93-
}
94-
}
95-
None => {
96-
return Err(ValError::new(
97-
ErrorType::UuidParsing {
98-
error: "Could not find version for uuid".to_string(),
99-
},
100-
input,
101-
));
102-
}
82+
let version = uuid.get_version_num();
83+
if schema_version == version {
84+
return Ok(());
85+
} else {
86+
return Err(ValError::new(
87+
ErrorType::UuidVersionMismatch {
88+
version,
89+
schema_version,
90+
},
91+
input,
92+
));
10393
}
10494
}
105-
10695
Ok(())
10796
}
10897
}

tests/test_errors.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ def f(input_value, info):
280280
('url_syntax_violation', 'Input violated strict URL syntax rules, Foobar', {'error': 'Foobar'}),
281281
('url_too_long', 'URL should have at most 42 characters', {'max_length': 42}),
282282
('url_scheme', 'URL scheme should be "foo", "bar" or "spam"', {'expected_schemes': '"foo", "bar" or "spam"'}),
283+
('uuid_type', 'Input should be a string', None),
284+
('uuid_parsing', 'Input should be a valid UUID, Foobar', {'error': 'Foobar'}),
285+
('uuid_version_mismatch', 'UUID version 2 does not match expected version: 4', {'version': 2, 'schema_version': 4}),
283286
]
284287

285288

@@ -294,11 +297,6 @@ def test_error_type(error_type, message, context):
294297
def test_all_errors_covered():
295298
listed_types = set(error_type for error_type, *_ in all_errors)
296299
actual_types = {e['type'] for e in list_all_errors()}
297-
298-
# TODO(martinabeleda): cover these
299-
actual_types.remove('uuid_type')
300-
actual_types.remove('uuid_parsing')
301-
actual_types.remove('uuid_version_mismatch')
302300
assert actual_types == listed_types
303301

304302

tests/validators/test_uuid.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pydantic_core import Uuid, core_schema
2+
3+
from ..conftest import PyAndJson
4+
5+
6+
def test_uuid_ok(py_and_json: PyAndJson):
7+
v = py_and_json(core_schema.uuid_schema())
8+
uuid = v.validate_test('12345678-1234-5678-1234-567812345678')
9+
10+
assert isinstance(uuid, Uuid)
11+
assert str(uuid) == '12345678-1234-5678-1234-567812345678'
12+
assert repr(uuid) == "Uuid('12345678-1234-5678-1234-567812345678')"
13+
assert uuid.version == 4
14+
assert uuid.urn == 'foo'
15+
assert uuid.variant == 'bar'
16+
17+
18+
def test_uuid_from_constructor_ok():
19+
uuid = Uuid('12345678-1234-5678-1234-567812345678')
20+
21+
assert isinstance(uuid, Uuid)
22+
assert str(uuid) == '12345678-1234-5678-1234-567812345678'
23+
assert repr(uuid) == "Uuid('12345678-1234-5678-1234-567812345678')"
24+
assert uuid.version == 4
25+
assert uuid.urn == 'foo'
26+
assert uuid.variant == 'bar'

0 commit comments

Comments
 (0)