File tree 3 files changed +56
-0
lines changed
3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -1628,6 +1628,11 @@ def check(self):
1628
1628
* _ , validation_error = validate_model (self .__class__ , self .__dict__ )
1629
1629
if validation_error :
1630
1630
raise validation_error
1631
+ else :
1632
+ from pydantic import TypeAdapter
1633
+
1634
+ adapter = TypeAdapter (self .__class__ )
1635
+ adapter .validate_python (self .__dict__ )
1631
1636
1632
1637
1633
1638
class HashModel (RedisModel , abc .ABC ):
Original file line number Diff line number Diff line change @@ -875,3 +875,22 @@ async def test_xfix_queries(members, m):
875
875
876
876
result = await m .Member .find (m .Member .bio % "*eat*" ).first ()
877
877
assert result .first_name == "Andrew"
878
+
879
+
880
+ @py_test_mark_asyncio
881
+ async def test_update_validation ():
882
+ class TestUpdate (HashModel ):
883
+ name : str
884
+ age : int
885
+
886
+ await Migrator ().run ()
887
+ t = TestUpdate (name = "steve" , age = 34 )
888
+ await t .save ()
889
+ update_dict = dict ()
890
+ update_dict ["age" ] = "cat"
891
+
892
+ with pytest .raises (ValidationError ):
893
+ await t .update (** update_dict )
894
+
895
+ rematerialized = await TestUpdate .find (TestUpdate .pk == t .pk ).first ()
896
+ assert rematerialized .age == 34
Original file line number Diff line number Diff line change @@ -971,3 +971,35 @@ async def test_xfix_queries(m):
971
971
972
972
result = await m .Member .find (m .Member .bio % "*ack*" ).first ()
973
973
assert result .first_name == "Steve"
974
+
975
+
976
+ @py_test_mark_asyncio
977
+ async def test_update_validation ():
978
+
979
+ class Embedded (EmbeddedJsonModel ):
980
+ price : float
981
+ name : str = Field (index = True )
982
+
983
+ class TestUpdatesClass (JsonModel ):
984
+ name : str
985
+ age : int
986
+ embedded : Embedded
987
+
988
+ await Migrator ().run ()
989
+ embedded = Embedded (price = 3.14 , name = "foo" )
990
+ t = TestUpdatesClass (name = "str" , age = 42 , embedded = embedded )
991
+ await t .save ()
992
+
993
+ update_dict = dict ()
994
+ update_dict ["age" ] = "foo"
995
+ with pytest .raises (ValidationError ):
996
+ await t .update (** update_dict )
997
+
998
+ t .age = 42
999
+ update_dict .clear ()
1000
+ update_dict ["embedded" ] = "hello"
1001
+ with pytest .raises (ValidationError ):
1002
+ await t .update (** update_dict )
1003
+
1004
+ rematerialized = await TestUpdatesClass .find (TestUpdatesClass .pk == t .pk ).first ()
1005
+ assert rematerialized .age == 42
You can’t perform that action at this time.
0 commit comments