Skip to content

Commit

Permalink
Merge pull request flask-restful#386 from g----/master
Browse files Browse the repository at this point in the history
flask-restful#384 Modified validation error message to form: [ARG_NAME]: (ARG_HELP) E...
  • Loading branch information
joshfriend committed Jan 22, 2015
2 parents cb32058 + 269dc0b commit 231da4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
11 changes: 5 additions & 6 deletions flask_restful/reqparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Argument(object):
iterator. The last item listed takes precedence in the result set.
:param choices: A container of the allowable values for the argument.
:param help: A brief description of the argument, returned in the
response when the argument is invalid. This takes precedence over
response when the argument is invalid with the name of the argument and
the message passed to a ValidationError raised by a type converter.
:param bool case_sensitive: Whether the arguments in the request are
case sensitive or not
Expand Down Expand Up @@ -130,7 +130,8 @@ def handle_validation_error(self, error):
:param error: the error that was raised
"""
msg = self.help if self.help is not None else str(error)
help_str = '(%s) ' % self.help if self.help else ''
msg = '[%s]: %s%s' % (self.name, help_str, str(error))
flask_restful.abort(400, message=msg)

def parse(self, request):
Expand Down Expand Up @@ -184,15 +185,13 @@ def parse(self, request):

if not results and self.required:
if isinstance(self.location, six.string_types):
error_msg = u"Missing required parameter {0} in {1}".format(
self.name,
error_msg = u"Missing required parameter in {0}".format(
_friendly_location.get(self.location, self.location)
)
else:
friendly_locations = [_friendly_location.get(loc, loc)
for loc in self.location]
error_msg = u"Missing required parameter {0} in {1}".format(
self.name,
error_msg = u"Missing required parameter in {0}".format(
' or '.join(friendly_locations)
)
self.handle_validation_error(ValueError(error_msg))
Expand Down
13 changes: 6 additions & 7 deletions tests/test_reqparse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import unittest
from mock import Mock, patch, NonCallableMock
from mock import Mock, patch
from flask import Flask
from werkzeug import exceptions, MultiDict
from werkzeug.wrappers import Request
Expand All @@ -19,18 +19,17 @@ def test_default_help(self):

@patch('flask_restful.abort')
def test_help(self, abort):
from werkzeug.datastructures import MultiDict
parser = RequestParser()
parser.add_argument('foo', choices=['one', 'two'], help='Bad choice')
req = Mock(['values'])
req.values = MultiDict([('foo', 'three')])
parser.parse_args(req)
abort.assert_called_with(400, message='Bad choice')
expected = '[foo]: (Bad choice) three is not a valid choice'
abort.assert_called_with(400, message=expected)

@patch('flask_restful.abort', side_effect=exceptions.BadRequest('Bad Request'))
def test_no_help(self, abort):
def bad_choice():
from werkzeug.datastructures import MultiDict
parser = RequestParser()
parser.add_argument('foo', choices=['one', 'two'])
req = Mock(['values'])
Expand Down Expand Up @@ -363,7 +362,7 @@ def test_parse_required(self):
except exceptions.BadRequest as e:
message = e.data['message']

self.assertEquals(message, (u'Missing required parameter foo in the '
self.assertEquals(message, (u'[foo]: Missing required parameter in the '
'post body or the query string'))

parser = RequestParser()
Expand All @@ -374,7 +373,7 @@ def test_parse_required(self):
except exceptions.BadRequest as e:
message = e.data['message']

self.assertEquals(message, (u"Missing required parameter bar in the "
self.assertEquals(message, (u"[bar]: Missing required parameter in the "
"post body or the query string or the "
"request's cookies"))

Expand Down Expand Up @@ -683,7 +682,7 @@ def test_not_json_location_and_content_type_json(self):

with app.test_request_context('/bubble', method='get',
content_type='application/json'):
parser.parse_args() # Should not raise a 400: BadRequest
parser.parse_args() # Should not raise a 400: BadRequest

def test_request_parser_remove_argument(self):
req = Request.from_values("/bubble?foo=baz")
Expand Down

0 comments on commit 231da4b

Please sign in to comment.