Skip to content

Commit

Permalink
Fix: allow_unknown failure on mapping fields
Browse files Browse the repository at this point in the history
Closes #1163
  • Loading branch information
nicolaiarocci committed Jun 12, 2018
1 parent 2e4b08b commit 0360bf9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Unreleased

Fixed
~~~~~
- ``allow_unknown`` validation rule fails with nested dict fields (`#1163`_)
- Updating a field with a nullable data relation fails when value is null (`#1159`_)
- ``cerberus.schema.SchemaError`` when ``VALIDATE_FILTERS = True``. (`#1154`_)
- Serializers fails when array of types is in schema. (`#1112`_)
Expand Down Expand Up @@ -54,6 +55,7 @@ Docs
.. _`#1157`: https://github.com/pyeve/eve/issues/1157
.. _`#1158`: https://github.com/pyeve/eve/issues/1158
.. _`#1159`: https://github.com/pyeve/eve/issues/1159
.. _`#1163`: https://github.com/pyeve/eve/issues/1163

Version 0.8
-----------
Expand Down
4 changes: 3 additions & 1 deletion eve/methods/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def patch_internal(

resource_def = app.config["DOMAIN"][resource]
schema = resource_def["schema"]
validator = app.validator(schema, resource=resource)
validator = app.validator(
schema, resource=resource, allow_unknown=resource_def["allow_unknown"]
)

object_id = original[resource_def["id_field"]]
last_modified = None
Expand Down
9 changes: 8 additions & 1 deletion eve/methods/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ def post_internal(resource, payl=None, skip_validation=False):
date_utc = datetime.utcnow().replace(microsecond=0)
resource_def = app.config["DOMAIN"][resource]
schema = resource_def["schema"]
validator = None if skip_validation else app.validator(schema, resource=resource)
validator = (
None
if skip_validation
else app.validator(
schema, resource=resource, allow_unknown=resource_def["allow_unknown"]
)
)

documents = []
results = []
failures = 0
Expand Down
4 changes: 3 additions & 1 deletion eve/methods/put.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def put_internal(
"""
resource_def = app.config["DOMAIN"][resource]
schema = resource_def["schema"]
validator = app.validator(schema, resource=resource)
validator = app.validator(
schema, resource=resource, allow_unknown=resource_def["allow_unknown"]
)

if payload is None:
payload = payload_()
Expand Down
15 changes: 15 additions & 0 deletions eve/tests/methods/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,21 @@ def test_post_allow_unknown(self):
self.assertTrue("unknown" in r_data)
self.assertEqual("unknown", r_data["unknown"])

def test_post_mapping_allow_unknown_allowed(self):
schema = {
"data": {
"type": "dict",
"allow_unknown": True,
"schema": {"prop": {"type": "string"}},
}
}
settings = {"RESOURCE_METHODS": ["GET", "POST", "DELETE"], "schema": schema}
self.app.register_resource("endpoint", settings)

data = {"data": {"prop": "test prop", "test": "test"}}
r, status = self.post("endpoint", data=data)
self.assert201(status)

def test_post_with_content_type_charset(self):
test_field = "ref"
test_value = "1234567890123456789054321"
Expand Down
4 changes: 0 additions & 4 deletions eve/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def __init__(self, *args, **kwargs):
if not config.VALIDATION_ERROR_AS_LIST:
kwargs["error_handler"] = SingleErrorAsStringErrorHandler

resource = kwargs.get("resource", None)
if resource:
resource_def = config.DOMAIN[resource]
kwargs["allow_unknown"] = resource_def["allow_unknown"]
super(Validator, self).__init__(*args, **kwargs)

def validate_update(self, document, document_id, persisted_document=None):
Expand Down

0 comments on commit 0360bf9

Please sign in to comment.