Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def always_error(request):
def always_fail(value):
raise ValidationError("something went wrong")
argmap = {"text": fields.Str(validate=always_fail)}
try:
return parser.parse(argmap, request)
except ValidationError as err:
return json_response(err.messages, status=err.status_code)
def test_validation_errors_in_validator_are_passed_to_handle_error(parser, web_request):
def validate(value):
raise ValidationError("Something went wrong.")
args = {"name": fields.Field(validate=validate, location="json")}
web_request.json = {"name": "invalid"}
with pytest.raises(ValidationError) as excinfo:
parser.parse(args, web_request)
exc = excinfo.value
assert isinstance(exc, ValidationError)
errors = exc.args[0]
assert errors["name"] == ["Something went wrong."]
def always_fail(value):
raise ValidationError("something went wrong", status_code=400)
def validate1(args):
if args["a"] > args["b"]:
raise ValidationError("b must be > a")
def handle_error(
self, error, req, schema, error_status_code=None, error_headers=None
):
assert isinstance(error, ValidationError)
assert isinstance(schema, Schema)
raise MockHTTPError(error_status_code, error_headers)
def test_bool_arg_invalid(app, parser):
arg = utils.make_bool_arg()
request = app.test_request_context('/?test_bool=nope').request
with pytest.raises(ValidationError):
parser.parse_arg('test_bool', arg, request)
def test_handle_error_reraises_errors(web_request):
p = Parser()
with pytest.raises(ValidationError):
p.handle_error(ValidationError("error raised"), web_request, Schema())
def always_fail(value):
raise ValidationError("something went wrong")
# -*- coding: utf-8 -*-
from marshmallow import ValidationError as MarshmallowValidationError
try:
from webargs import ValidationError as WebargsValidationError
except ImportError:
HAS_WEBARGS = False
else:
HAS_WEBARGS = True
if HAS_WEBARGS:
class ValidationError(WebargsValidationError, MarshmallowValidationError):
"""Raised when a validation fails. Inherits from both
webargs' ``ValidationError`` (if webargs is installed) and marshmallow's
``ValidationError`` so that the same validation functions can be used in either library.
"""
def __init__(self, message, *args, **kwargs):
status_code = kwargs.pop('status_code', 400)
WebargsValidationError.__init__(self, message, status_code=status_code)
MarshmallowValidationError.__init__(self, message, *args, **kwargs)
else:
class ValidationError(MarshmallowValidationError):
"""Raised when a validation fails. Inherits from both
webargs' ``ValidationError`` (if webargs is installed) and marshmallow's
``ValidationError`` so that the same validation functions can be used in either library.
"""
def __init__(self, message, field=None, **_):
def plate_in_range(val):
if int(val) < 6500:
raise ValidationError('Plateid must be > 6500')