Skip to content

Commit

Permalink
#384 Modified validation error message to form: [ARG_NAME]: (ARG_HELP…
Browse files Browse the repository at this point in the history
…) ERROR_MESSAGE
  • Loading branch information
mh--- committed Jan 12, 2015
1 parent bf983cf commit b2deac2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
5 changes: 3 additions & 2 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 @@ -125,7 +125,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
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 foo 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 bar in the "
"post body or the query string or the "
"request's cookies"))

Expand Down Expand Up @@ -661,7 +660,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 b2deac2

Please sign in to comment.