How to use the webargs.ValidationError function in webargs

To help you get started, we’ve selected a few webargs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github marshmallow-code / webargs / tests / apps / django_app / echo / views.py View on Github external
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)
github marshmallow-code / webargs / tests / test_core.py View on Github external
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."]
github marshmallow-code / webargs / tests / apps / flask_app.py View on Github external
def always_fail(value):
        raise ValidationError("something went wrong", status_code=400)
github marshmallow-code / webargs / tests / test_core.py View on Github external
def validate1(args):
        if args["a"] > args["b"]:
            raise ValidationError("b must be > a")
github marshmallow-code / webargs / tests / test_core.py View on Github external
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)
github jmcarp / neurotrends / tests / test_api / test_utils.py View on Github external
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)
github marshmallow-code / webargs / tests / test_core.py View on Github external
def test_handle_error_reraises_errors(web_request):
    p = Parser()
    with pytest.raises(ValidationError):
        p.handle_error(ValidationError("error raised"), web_request, Schema())
github marshmallow-code / webargs / tests / apps / pyramid_app.py View on Github external
def always_fail(value):
        raise ValidationError("something went wrong")
github marshmallow-code / apispec / smore / validate / core.py View on Github external
# -*- 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, **_):
github sdss / marvin / python / marvin / api / __init__.py View on Github external
def plate_in_range(val):
    if int(val) < 6500:
        raise ValidationError('Plateid must be > 6500')

webargs

Declarative parsing and validation of HTTP request objects, with built-in support for popular web frameworks, including Flask, Django, Bottle, Tornado, Pyramid, Falcon, and aiohttp.

MIT
Latest version published 2 months ago

Package Health Score

94 / 100
Full package analysis

Similar packages