@@ -4,7 +4,7 @@ use pyo3::types::PyDict;
4
4
5
5
use uuid:: Uuid ;
6
6
7
- use crate :: build_tools:: SchemaDict ;
7
+ use crate :: build_tools:: { is_strict , SchemaDict } ;
8
8
use crate :: errors:: { ErrorType , ValError , ValResult } ;
9
9
use crate :: input:: Input ;
10
10
use crate :: recursion_guard:: RecursionGuard ;
@@ -15,6 +15,7 @@ use super::{BuildValidator, CombinedValidator, Definitions, DefinitionsBuilder,
15
15
#[ derive( Debug , Clone ) ]
16
16
#[ allow( dead_code) ]
17
17
pub struct UuidValidator {
18
+ strict : bool ,
18
19
version : Option < usize > ,
19
20
}
20
21
@@ -23,10 +24,11 @@ impl BuildValidator for UuidValidator {
23
24
24
25
fn build (
25
26
schema : & PyDict ,
26
- _config : Option < & PyDict > ,
27
+ config : Option < & PyDict > ,
27
28
_definitions : & mut DefinitionsBuilder < CombinedValidator > ,
28
29
) -> PyResult < CombinedValidator > {
29
30
Ok ( Self {
31
+ strict : is_strict ( schema, config) ?,
30
32
version : schema. get_as ( intern ! ( schema. py( ) , "version" ) ) ?,
31
33
}
32
34
. into ( ) )
@@ -38,14 +40,12 @@ impl Validator for UuidValidator {
38
40
& ' s self ,
39
41
py : Python < ' data > ,
40
42
input : & ' data impl Input < ' data > ,
41
- _extra : & Extra ,
43
+ extra : & Extra ,
42
44
_definitions : & ' data Definitions < CombinedValidator > ,
43
45
_recursion_guard : & ' s mut RecursionGuard ,
44
46
) -> ValResult < ' data , PyObject > {
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
- }
47
+ let lib_uuid = self . get_uuid ( input, extra. strict . unwrap_or ( self . strict ) ) ?;
48
+ Ok ( PyUuid :: new ( lib_uuid) . into_py ( py) )
49
49
}
50
50
51
51
fn different_strict_behavior (
@@ -67,13 +67,25 @@ impl Validator for UuidValidator {
67
67
68
68
#[ allow( dead_code) ]
69
69
impl UuidValidator {
70
- fn get_uuid < ' s , ' data > ( & ' s self , input : & ' data impl Input < ' data > ) -> ValResult < ' data , Uuid > {
71
- if let Some ( py_uuid) = input. input_as_uuid ( ) {
72
- let lib_uuid = py_uuid. into_uuid ( ) ;
73
- self . check_version ( input, lib_uuid) ?;
74
- Ok ( lib_uuid)
75
- } else {
76
- Err ( ValError :: new ( ErrorType :: UuidType , input) )
70
+ fn get_uuid < ' s , ' data > ( & ' s self , input : & ' data impl Input < ' data > , strict : bool ) -> ValResult < ' data , Uuid > {
71
+ match input. validate_str ( strict) {
72
+ Ok ( either_uuid) => {
73
+ let cow = either_uuid. as_cow ( ) ?;
74
+ let uuid_str = cow. as_ref ( ) ;
75
+ match Uuid :: parse_str ( uuid_str) {
76
+ Ok ( lib_uuid) => Ok ( lib_uuid) ,
77
+ Err ( e) => Err ( ValError :: new ( ErrorType :: UuidParsing { error : e. to_string ( ) } , input) ) ,
78
+ }
79
+ }
80
+ Err ( _) => {
81
+ if let Some ( py_uuid) = input. input_as_uuid ( ) {
82
+ let lib_uuid = py_uuid. into_uuid ( ) ;
83
+ self . check_version ( input, lib_uuid) ?;
84
+ Ok ( lib_uuid)
85
+ } else {
86
+ Err ( ValError :: new ( ErrorType :: UuidType , input) )
87
+ }
88
+ }
77
89
}
78
90
}
79
91
0 commit comments