Commit ede1eab8 authored by Alexei Kornienko's avatar Alexei Kornienko
Browse files

Merge branch 'vitalii/fix_import_data' into 'master'

Check for rogue fields in imported data

Closes #9

See merge request !26
parents 5b4c38bf 5d07c8b4
......@@ -156,6 +156,11 @@ class Model(metaclass=ModelMeta):
:param kwargs:
:return:
"""
errors = {}
rogue_fields = set(raw_data.keys()) - set(self.fields.keys())
if rogue_fields:
errors.update({x: 'Unknown field' for x in rogue_fields})
raise DataError(errors)
return convert(self.__class__, self, raw_data, role=role, **kwargs)
def to_native(self, role=None, app_data=None, **kwargs):
......
......@@ -5,7 +5,7 @@ import pytest
from schematics.common import PY2
from schematics.models import Model
from schematics.transforms import whitelist, blacklist
from schematics.types import StringType, IntType, ListType, ModelType
from schematics.types import StringType, IntType, ListType, ModelType, DictType
from schematics.exceptions import *
......@@ -781,3 +781,33 @@ def test_append_field_to_model():
m = M(input_data)
assert m.b == 'b'
assert m.serialize() == input_data
def test_import_data_errors_simple_case():
class M(Model):
a = StringType()
b = StringType()
import_data = {'a': 'string', 'b': 'string', 'e': 'string'}
m = M({'a': 'string', 'b': 'string'})
with pytest.raises(DataError) as e:
m.import_data(import_data)
assert e == {"e": "Unknown field"}
def test_import_data_errors_nested_case():
class A(Model):
items, values, get, keys = IntType(), IntType(), IntType(), IntType()
class M(Model):
keys = ModelType(A)
b = StringType()
import_data = {"keys": {"items": 1, "values": 1, "get": 1, "keys": 1, 'c': 1}, 'b': 'string'}
m = M({"keys": {"items": 1, "values": 1, "get": 1, "keys": 1}, 'b': 'string'})
with pytest.raises(DataError) as e:
m.import_data(import_data)
assert e == {"c": "Unknown field"}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment