Skip to content

Commit

Permalink
Improve error message when field is set to null.
Browse files Browse the repository at this point in the history
  • Loading branch information
tisto committed Mar 13, 2018
1 parent b5426cd commit 5e9a037
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/plone/restapi/deserializer/dxcontent.py
Expand Up @@ -62,15 +62,19 @@ def __call__(self, validate_all=False, data=None): # noqa: ignore=C901
if not self.check_permission(write_permissions.get(name)):
continue

# set the field to missing_value if we receive None/null
# set the field to missing_value if we receive null
if data[name] is None:
if not field.required:
dm.set(field.missing_value)
else:
errors.append({
'field': field.__name__,
'message': ('field is required. null is not '
'allowed'),
'message': (
'{} is a required field.'.format(
field.__name__
),
'Setting it to null is not allowed.'
),
'error': None})
continue

Expand Down
10 changes: 8 additions & 2 deletions src/plone/restapi/tests/test_content_patch.py
Expand Up @@ -71,8 +71,14 @@ def test_patch_document_will_not_delete_value_with_null_if_required(self):
# null will set field.missing_value which is u'' for the field
self.assertEqual(400, response.status_code)
self.assertTrue("\'field\': \'title\'" in response.text)
self.assertTrue('field is required. null is not allowed' in
response.text)
self.assertTrue(
'title is a required field.'
in response.text
)
self.assertTrue(
'Setting it to null is not allowed.'
in response.text
)

def test_patch_document_with_representation(self):
response = requests.patch(
Expand Down
9 changes: 7 additions & 2 deletions src/plone/restapi/tests/test_dxcontent_deserializer.py
Expand Up @@ -176,8 +176,13 @@ def test_deserializer_sets_missing_value_on_required_field(self):
body='{"test_missing_value_required_field": null}')
self.assertEquals(u'valid value',
self.portal.doc1.test_missing_value_required_field)
self.assertEquals(u'field is required. null is not allowed',
cm.exception.message[0]['message'])
self.assertEquals(
(
'test_missing_value_required_field is a required field.',
'Setting it to null is not allowed.'
),
cm.exception.message[0]['message']
)
self.assertEquals(u'test_missing_value_required_field',
cm.exception.message[0]['field'])

Expand Down

0 comments on commit 5e9a037

Please sign in to comment.