@@ -158,7 +158,20 @@ def get_validators(self):
158
158
"""
159
159
return self .default_validators [:]
160
160
161
- def fail (self , key , ** kwargs ):
161
+ def fail (self , detail = None , status = 400 ):
162
+ """
163
+ We throw a normal error if custom validation error.
164
+
165
+ :param Union[str, dict] detail: Detail validation error
166
+ :param int status: Code for response
167
+
168
+ :raise AssertionError: If you have not found the key in the `self.error_messages`.
169
+ :raise ValidationError: If you find the key in the `self.error_messages`.
170
+
171
+ """
172
+ raise ValidationError (detail = detail , status = status )
173
+
174
+ def fail_field_validation (self , key , ** kwargs ):
162
175
"""
163
176
We throw a normal error if something went wrong during data processing.
164
177
@@ -440,7 +453,7 @@ def run_validation(self, data=None):
440
453
"""
441
454
if data == '' or (self .trim_whitespace and six .text_type (data ).strip () == '' ):
442
455
if not self .allow_blank :
443
- self .fail ('blank' )
456
+ self .fail_field_validation ('blank' )
444
457
return ''
445
458
return super (CharField , self ).run_validation (data )
446
459
@@ -458,7 +471,7 @@ def to_internal_value(self, data):
458
471
"""
459
472
# We skip numbers as strings, but bool as strings seems to be a developer error.
460
473
if isinstance (data , bool ) or not isinstance (data , six .string_types + six .integer_types + (float ,)):
461
- self .fail ('invalid' )
474
+ self .fail_field_validation ('invalid' )
462
475
463
476
val = six .text_type (data )
464
477
@@ -536,12 +549,12 @@ def to_internal_value(self, data):
536
549
"""
537
550
# We look, do not want us to score a memory?
538
551
if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
539
- self .fail ('max_string_length' )
552
+ self .fail_field_validation ('max_string_length' )
540
553
541
554
try :
542
555
data = int (self .re_decimal .sub ('' , str (data )))
543
556
except (ValueError , TypeError ):
544
- self .fail ('invalid' )
557
+ self .fail_field_validation ('invalid' )
545
558
return data
546
559
547
560
def to_representation (self , value ):
@@ -612,12 +625,12 @@ def to_internal_value(self, data):
612
625
"""
613
626
# We look, do not want us to score a memory?
614
627
if isinstance (data , six .text_type ) and len (data ) > self .MAX_STRING_LENGTH :
615
- self .fail ('max_string_length' )
628
+ self .fail_field_validation ('max_string_length' )
616
629
617
630
try :
618
631
return float (data )
619
632
except (TypeError , ValueError ):
620
- self .fail ('invalid' )
633
+ self .fail_field_validation ('invalid' )
621
634
622
635
def to_representation (self , value ):
623
636
"""
@@ -676,7 +689,7 @@ def to_internal_value(self, data):
676
689
return False
677
690
except TypeError : # If the non-hash type came.
678
691
pass
679
- self .fail ('invalid' , input = data )
692
+ self .fail_field_validation ('invalid' , input = data )
680
693
681
694
def to_representation (self , value ):
682
695
"""
@@ -744,7 +757,7 @@ def to_internal_value(self, data):
744
757
return None
745
758
except TypeError : # If the non-hash type came.
746
759
pass
747
- self .fail ('invalid' , input = data )
760
+ self .fail_field_validation ('invalid' , input = data )
748
761
749
762
def to_representation (self , value ):
750
763
"""
@@ -874,10 +887,10 @@ def to_internal_value(self, data):
874
887
data = html .parse_html_list (data )
875
888
876
889
if any ((isinstance (data , type ('' )), isinstance (data , Mapping ), not hasattr (data , '__iter__' ))):
877
- self .fail ('not_a_list' , input_type = type (data ).__name__ )
890
+ self .fail_field_validation ('not_a_list' , input_type = type (data ).__name__ )
878
891
879
892
if not self .allow_empty and len (data ) == 0 :
880
- self .fail ('empty' )
893
+ self .fail_field_validation ('empty' )
881
894
882
895
return [self .child .run_validation (item ) for item in data ]
883
896
@@ -946,7 +959,7 @@ def to_internal_value(self, data):
946
959
947
960
# Check on the datetime object.
948
961
if isinstance (data , datetime .datetime ):
949
- self .fail ('datetime' )
962
+ self .fail_field_validation ('datetime' )
950
963
951
964
# Check on the date object.
952
965
if isinstance (data , datetime .date ):
@@ -962,7 +975,7 @@ def to_internal_value(self, data):
962
975
return parsed .date ()
963
976
964
977
# Throw exception.
965
- self .fail ('invalid' , format = data )
978
+ self .fail_field_validation ('invalid' , format = data )
966
979
967
980
def to_representation (self , value ):
968
981
"""
@@ -1058,7 +1071,7 @@ def to_internal_value(self, data):
1058
1071
return parsed .time ()
1059
1072
1060
1073
# Throw exception.
1061
- self .fail ('invalid' , format = data )
1074
+ self .fail_field_validation ('invalid' , format = data )
1062
1075
1063
1076
def to_representation (self , value ):
1064
1077
"""
@@ -1145,7 +1158,7 @@ def to_internal_value(self, data):
1145
1158
# Check ot the data ot datetime object.
1146
1159
if isinstance (data , datetime .date ):
1147
1160
if not isinstance (data , datetime .datetime ):
1148
- self .fail ('date' )
1161
+ self .fail_field_validation ('date' )
1149
1162
else :
1150
1163
return data
1151
1164
@@ -1156,7 +1169,7 @@ def to_internal_value(self, data):
1156
1169
pass
1157
1170
1158
1171
# Throw error.
1159
- self .fail ('invalid' , format = data )
1172
+ self .fail_field_validation ('invalid' , format = data )
1160
1173
1161
1174
def to_representation (self , value ):
1162
1175
"""
@@ -1207,7 +1220,7 @@ def to_internal_value(self, data):
1207
1220
try :
1208
1221
return json .loads (data , encoding = 'utf8' )
1209
1222
except (JSONDecodeError , TypeError , ValueError ):
1210
- self .fail ('invalid' )
1223
+ self .fail_field_validation ('invalid' )
1211
1224
1212
1225
def to_representation (self , value ):
1213
1226
"""
@@ -1222,7 +1235,7 @@ def to_representation(self, value):
1222
1235
try :
1223
1236
return json .dumps (value )
1224
1237
except (TypeError , ValueError ):
1225
- self .fail ('invalid' )
1238
+ self .fail_field_validation ('invalid' )
1226
1239
1227
1240
1228
1241
class DictField (JsonField ):
@@ -1275,7 +1288,7 @@ def to_internal_value(self, data):
1275
1288
data = html .parse_html_dict (data )
1276
1289
1277
1290
if not isinstance (data , dict ):
1278
- self .fail ('not_a_dict' , input_type = type (data ).__name__ )
1291
+ self .fail_field_validation ('not_a_dict' , input_type = type (data ).__name__ )
1279
1292
1280
1293
return {
1281
1294
six .text_type (key ): self .child .run_validation (value )
0 commit comments