views: improve jsonchema validation error message (#38338)

This commit is contained in:
Valentin Deniaud 2020-02-06 18:07:46 +01:00 committed by Thomas NOËL
parent bcaff2677a
commit 01293dea36
4 changed files with 11 additions and 8 deletions

View File

@ -363,7 +363,10 @@ class GenericEndpointView(GenericConnectorMixin, SingleObjectMixin, View):
try:
validate(data, json_schema)
except ValidationError as e:
raise APIError(e.message, http_status=400)
error_msg = e.message
if e.path:
error_msg = '%s: %s' % ('/'.join(map(str, e.path)), error_msg)
raise APIError(error_msg, http_status=400)
d['post_data'] = data
return d

View File

@ -189,7 +189,7 @@ def test_upload(app, connector, monkeypatch):
'data': None,
'err': 1,
'err_class': 'passerelle.utils.jsonresponse.APIError',
'err_desc': "'content' is a required property"
'err_desc': "file: 'content' is a required property"
}
# no file

View File

@ -103,7 +103,7 @@ def test_uploadfile_error_if_non_string_file_name(app, setup):
expect_errors=True)
assert response.status_code == 400
assert response.json['err'] == 1
assert response.json['err_desc'] == "1 is not of type 'string'"
assert response.json['err_desc'] == "file/filename: 1 is not of type 'string'"
response = app.post_json(
'/cmis/slug-cmis/uploadfile',
@ -114,7 +114,7 @@ def test_uploadfile_error_if_non_string_file_name(app, setup):
expect_errors=True)
assert response.status_code == 400
assert response.json['err'] == 1
assert response.json['err_desc'] == "1 is not of type 'string'"
assert response.json['err_desc'] == "filename: 1 is not of type 'string'"
def test_uploadfile_error_if_non_valid_file_name(app, setup):
@ -160,7 +160,7 @@ def test_uploadfile_error_if_non_string_path(app, setup):
expect_errors=True)
assert response.status_code == 400
assert response.json['err'] == 1
assert response.json['err_desc'] == "1 is not of type 'string'"
assert response.json['err_desc'] == "path: 1 is not of type 'string'"
def test_uploadfile_error_if_no_regular_path(app, setup):
@ -183,7 +183,7 @@ def test_uploadfile_error_if_no_file_content(app, setup):
expect_errors=True)
assert response.status_code == 400
assert response.json['err'] == 1
assert response.json['err_desc'] == "'content' is a required property"
assert response.json['err_desc'] == "file: 'content' is a required property"
def test_uploadfile_error_if_non_string_file_content(app, setup):
@ -194,7 +194,7 @@ def test_uploadfile_error_if_non_string_file_content(app, setup):
expect_errors=True)
assert response.status_code == 400
assert response.json['err'] == 1
assert response.json['err_desc'] == "1 is not of type 'string'"
assert response.json['err_desc'] == "file/content: 1 is not of type 'string'"
def test_uploadfile_error_if_no_proper_base64_encoding(app, setup):

View File

@ -380,7 +380,7 @@ def test_endpoint_decorator_pre_process(db, app):
with patch_init, patch_object:
resp = app.post_json(url_foo, params=payload, status=400)
assert resp.json['err'] == 1
assert resp.json['err_desc'] == "None is not of type %s" % repr(u'integer')
assert resp.json['err_desc'] == "foo/1/id: None is not of type %s" % repr(u'integer')
with patch_init, patch_object:
resp = app.post_json(url_bar, params=payload)
assert resp.json['err'] == 0