@@ -687,6 +687,48 @@ class CoreModel:
687
687
}
688
688
)
689
689
690
+ @pytest .fixture (scope = 'class' )
691
+ def validator (self ):
692
+ return SchemaValidator ({'type' : 'uuid' })
693
+
694
+ @pytest .fixture (scope = 'class' )
695
+ def pydantic_validator (self ):
696
+ def to_UUID (v : Any ) -> UUID :
697
+ try :
698
+ return UUID (v )
699
+ except Exception as e :
700
+ raise PydanticCustomError ('uuid_parsing' , 'Input should be a valid uuid' ) from e
701
+
702
+ json_schema = core_schema .no_info_after_validator_function (
703
+ to_UUID , core_schema .str_schema (strict = True , strip_whitespace = True )
704
+ )
705
+ schema = core_schema .json_or_python_schema (
706
+ json_schema = json_schema ,
707
+ python_schema = core_schema .lax_or_strict_schema (
708
+ lax_schema = core_schema .union_schema ([core_schema .is_instance_schema (UUID ), json_schema ]),
709
+ strict_schema = core_schema .is_instance_schema (UUID ),
710
+ ),
711
+ serialization = core_schema .to_string_ser_schema (when_used = 'json' ),
712
+ )
713
+
714
+ return SchemaValidator (schema )
715
+
716
+ @pytest .mark .benchmark (group = 'uuid from str' )
717
+ def test_uuid_from_string_core (self , benchmark , validator ):
718
+ benchmark (validator .validate_python , '12345678-1234-5678-1234-567812345678' )
719
+
720
+ @pytest .mark .benchmark (group = 'uuid from str' )
721
+ def test_uuid_from_string_pyd (self , benchmark , pydantic_validator ):
722
+ benchmark (pydantic_validator .validate_python , '12345678-1234-5678-1234-567812345678' )
723
+
724
+ @pytest .mark .benchmark (group = 'uuid from UUID' )
725
+ def test_uuid_from_uuid_core (self , benchmark , validator ):
726
+ benchmark (validator .validate_python , UUID ('12345678-1234-5678-1234-567812345678' ))
727
+
728
+ @pytest .mark .benchmark (group = 'uuid from UUID' )
729
+ def test_uuid_from_uuid_pyd (self , benchmark , pydantic_validator ):
730
+ benchmark (pydantic_validator .validate_python , UUID ('12345678-1234-5678-1234-567812345678' ))
731
+
690
732
@pytest .fixture (scope = 'class' )
691
733
def uuid_raw (self ):
692
734
return UUID ('12345678-1234-5678-1234-567812345678' )
0 commit comments