from ..field import Field
from ..exceptions import FieldValidationError, InvalidDictModelError


class Dict(Field):

    model = None

    def __init__(self, model=None, *args, **kwargs):
        self.model = model
        if self.model is None:
            self.model = {}
        self._validate_model()
        super().__init__(*args, **kwargs)

    def _validate_model(self):
        for key, field in self.model.items():
            if not isinstance(field, Field):
                raise InvalidDictModelError()

    def is_empty(self, value):
        return value is None or value == {}

    def empty_value(self):
        return {}

    def submit(self, data):
        data, errors = super().submit(data)
        if errors is None:
            errors = {}
        return data, errors

    def validate(self, data):
        new_data = {}
        errors = {}
        for key, field in self.model.items():
            value, error = field.submit(data.get(key))
            if error:
                errors[key] = error
            else:
                new_data[key] = value
        if errors:
            raise FieldValidationError(errors)
        return new_data

@@ -114,26 +114,8 @@
 ):%0A%0A
-    model = None%0A%0A
     
@@ -133,21 +133,22 @@
 _(self, 
-model
+fields
 =None, *
@@ -181,88 +181,16 @@
 elf.
-model = model%0A        if self.model is None:%0A            self.model = %7B%7D%0A       
+fields =
  sel
@@ -201,22 +201,29 @@
 alidate_
-model(
+fields(fields
 )%0A      
@@ -281,20 +281,80 @@
 ate_
-model(self):
+fields(self, fields):%0A        if fields is None:%0A            fields = %7B%7D
 %0A   
@@ -372,34 +372,30 @@
 y, field in 
-self.model
+fields
 .items():%0A  
@@ -487,129 +487,29 @@
 r()%0A
-%0A    def is_empty(self, value):%0A        return value is None or value == %7B%7D%0A%0A    def empty_value(self):%0A        return %7B%7D
+        return fields
 %0A%0A  
@@ -658,16 +658,138 @@
 errors%0A%0A
+    def is_empty(self, value):%0A        return value is None or value == %7B%7D%0A%0A    def empty_value(self):%0A        return %7B%7D%0A%0A
     def 
@@ -887,13 +887,14 @@
 elf.
-model
+fields
 .ite

Refactor dict field