40
40
"IPv4Field" ,
41
41
"IPv6Field" ,
42
42
"IPv46Field" ,
43
+ "ListField" ,
43
44
"UUIDField" ,
44
45
"DictField" ,
45
46
"ObjectField" ,
@@ -76,6 +77,7 @@ class Field(BaseField):
76
77
"required" : "This field is required." ,
77
78
}
78
79
data_type_name = None
80
+ empty_values = EMPTY_VALUES
79
81
80
82
def __init__ (
81
83
self ,
@@ -170,7 +172,7 @@ def to_python(self, value):
170
172
raise NotImplementedError ()
171
173
172
174
def run_validators (self , value ):
173
- if value in EMPTY_VALUES :
175
+ if value in self . empty_values :
174
176
return
175
177
176
178
errors = []
@@ -186,7 +188,7 @@ def run_validators(self, value):
186
188
def validate (self , value ):
187
189
if (
188
190
self .choice_values
189
- and (value not in EMPTY_VALUES )
191
+ and (value not in self . empty_values )
190
192
and (value not in self .choice_values )
191
193
):
192
194
msg = self .error_messages ["invalid_choice" ] % value
@@ -315,7 +317,7 @@ def __init__(self, min_value=None, max_value=None, **options):
315
317
self .validators .append (MaxValueValidator (max_value ))
316
318
317
319
def to_python (self , value ):
318
- if value in EMPTY_VALUES :
320
+ if value in self . empty_values :
319
321
return
320
322
try :
321
323
return self .scalar_type (value )
@@ -366,7 +368,7 @@ class DateField(_IsoFormatMixin, Field):
366
368
data_type_name = "ISO-8601 Date"
367
369
368
370
def to_python (self , value ):
369
- if value in EMPTY_VALUES :
371
+ if value in self . empty_values :
370
372
return
371
373
if isinstance (value , datetime .datetime ):
372
374
return value .date ()
@@ -403,7 +405,7 @@ def __init__(self, assume_local=False, **options):
403
405
self .assume_local = assume_local
404
406
405
407
def to_python (self , value ):
406
- if value in EMPTY_VALUES :
408
+ if value in self . empty_values :
407
409
return
408
410
if isinstance (value , datetime .time ):
409
411
return value
@@ -440,7 +442,7 @@ def __init__(self, ignore_timezone=False, **options):
440
442
self .ignore_timezone = ignore_timezone
441
443
442
444
def to_python (self , value ):
443
- if value in EMPTY_VALUES :
445
+ if value in self . empty_values :
444
446
return
445
447
if isinstance (value , datetime .time ):
446
448
if value .tzinfo and self .ignore_timezone :
@@ -491,7 +493,7 @@ def __init__(self, assume_local=False, **options):
491
493
self .assume_local = assume_local
492
494
493
495
def to_python (self , value ):
494
- if value in EMPTY_VALUES :
496
+ if value in self . empty_values :
495
497
return
496
498
if isinstance (value , datetime .datetime ):
497
499
return value
@@ -528,7 +530,7 @@ def __init__(self, ignore_timezone=False, **options):
528
530
self .ignore_timezone = ignore_timezone
529
531
530
532
def to_python (self , value ):
531
- if value in EMPTY_VALUES :
533
+ if value in self . empty_values :
532
534
return
533
535
if isinstance (value , datetime .datetime ):
534
536
if value .tzinfo and self .ignore_timezone :
@@ -570,7 +572,7 @@ class HttpDateTimeField(Field):
570
572
data_type_name = "ISO-1123 DateTime"
571
573
572
574
def to_python (self , value ):
573
- if value in EMPTY_VALUES :
575
+ if value in self . empty_values :
574
576
return
575
577
if isinstance (value , datetime .datetime ):
576
578
return value
@@ -607,7 +609,7 @@ class TimeStampField(Field):
607
609
data_type_name = "Integer"
608
610
609
611
def to_python (self , value ):
610
- if value in EMPTY_VALUES :
612
+ if value in self . empty_values :
611
613
return
612
614
if isinstance (value , datetime .datetime ):
613
615
return value
@@ -619,7 +621,7 @@ def to_python(self, value):
619
621
raise exceptions .ValidationError (msg )
620
622
621
623
def prepare (self , value ):
622
- if value in EMPTY_VALUES :
624
+ if value in self . empty_values :
623
625
return
624
626
if isinstance (value , six .integer_types ):
625
627
return long (value )
@@ -632,6 +634,7 @@ class DictField(Field):
632
634
"invalid" : "Must be a dict." ,
633
635
}
634
636
data_type_name = "Dict"
637
+ empty_values = (None , "" , [], ())
635
638
636
639
def __init__ (self , ** options ):
637
640
options .setdefault ("default" , dict )
@@ -656,6 +659,7 @@ class ListField(Field):
656
659
"invalid" : "Must be an array." ,
657
660
}
658
661
data_type_name = "List"
662
+ empty_values = (None , "" , {}, ())
659
663
660
664
def __init__ (self , ** options ):
661
665
options .setdefault ("default" , list )
@@ -701,9 +705,10 @@ def to_python(self, value):
701
705
702
706
value_list = []
703
707
errors = {}
708
+ field_to_python = self .field .to_python
704
709
for idx , item in enumerate (value ):
705
710
try :
706
- value_list .append (self . field . to_python (item ))
711
+ value_list .append (field_to_python (item ))
707
712
except exceptions .ValidationError as ve :
708
713
errors [idx ] = ve .error_messages
709
714
@@ -712,6 +717,46 @@ def to_python(self, value):
712
717
713
718
return value_list
714
719
720
+ def validate (self , value ):
721
+ """
722
+ Validate each item against field
723
+ """
724
+ super (TypedListField , self ).validate (value )
725
+ if value :
726
+ field_validate = self .field .validate
727
+
728
+ errors = {}
729
+ for idx , item in enumerate (value ):
730
+ try :
731
+ field_validate (item )
732
+ except exceptions .ValidationError as ve :
733
+ errors [idx ] = ve .error_messages
734
+
735
+ if errors :
736
+ raise exceptions .ValidationError (errors )
737
+
738
+ return value
739
+
740
+ def run_validators (self , value ):
741
+ """
742
+ Run validators against each item in the field
743
+ """
744
+ super (TypedListField , self ).run_validators (value )
745
+ if value :
746
+ field_run_validators = self .field .run_validators
747
+
748
+ errors = {}
749
+ for idx , item in enumerate (value ):
750
+ try :
751
+ field_run_validators (item )
752
+ except exceptions .ValidationError as ve :
753
+ errors [idx ] = ve .error_messages
754
+
755
+ if errors :
756
+ raise exceptions .ValidationError (errors )
757
+
758
+ return value
759
+
715
760
def prepare (self , value ):
716
761
if isinstance (value , (tuple , list )):
717
762
prepare = self .field .prepare
@@ -783,7 +828,7 @@ def to_python(self, value):
783
828
def validate (self , value ):
784
829
super (TypedDictField , self ).validate (value )
785
830
786
- if value in EMPTY_VALUES :
831
+ if value in self . empty_values :
787
832
return
788
833
789
834
key_errors = []
@@ -811,7 +856,7 @@ def validate(self, value):
811
856
def run_validators (self , value ):
812
857
super (TypedDictField , self ).run_validators (value )
813
858
814
- if value in EMPTY_VALUES :
859
+ if value in self . empty_values :
815
860
return
816
861
817
862
key_errors = []
0 commit comments