How to use webargs - 10 common examples

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 / pyramid_app.py View on Github external
def error400(request):
    def always_fail(value):
        raise ValidationError("something went wrong", status_code=400)

    argmap = {"text": fields.Str(validate=always_fail)}
    return parser.parse(argmap, request)
github marshmallow-code / webargs / tests / test_core.py View on Github external
def test_nested_many(web_request, parser):
    web_request.json = {"pets": [{"name": "Pips"}, {"name": "Zula"}]}
    args = {"pets": fields.Nested({"name": fields.Str()}, required=True, many=True)}
    parsed = parser.parse(args, web_request)
    assert parsed == {"pets": [{"name": "Pips"}, {"name": "Zula"}]}
    web_request.json = {}
    with pytest.raises(ValidationError):
        parser.parse(args, web_request)
github marshmallow-code / webargs / tests / apps / bottle_app.py View on Github external
def always_error():
    def always_fail(value):
        raise ma.ValidationError("something went wrong")

    args = {"text": fields.Str(validate=always_fail)}
    return parser.parse(args)
github marshmallow-code / webargs / tests / apps / flask_app.py View on Github external
import json
from flask import Flask, jsonify as J, Response, request
from flask.views import MethodView

import marshmallow as ma
from webargs import fields, ValidationError, missing
from webargs.flaskparser import parser, use_args, use_kwargs
from webargs.core import MARSHMALLOW_VERSION_INFO


class TestAppConfig:
    TESTING = True


hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}


class HelloSchema(ma.Schema):
    name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)


strict_kwargs = {"strict": True} if MARSHMALLOW_VERSION_INFO[0] < 3 else {}
hello_many_schema = HelloSchema(many=True, **strict_kwargs)

app = Flask(__name__)
app.config.from_object(TestAppConfig)


@app.route("/echo", methods=["GET", "POST"])
def echo():
github jmcarp / filteralchemy / tests / test_filters.py View on Github external
def ModelFilterSet(self, session, models):
        class ModelFilterSet(FilterSet):
            class Meta:
                model = models.Album
                query = session.query(models.Album)
                operators = (operators.Equal, operators.In)
                parser = parser
            name__like = Filter('name', fields.Str(), operator=operators.Like)
        return ModelFilterSet
github marshmallow-code / webargs / tests / apps / falcon_app.py View on Github external
import json

import falcon
import marshmallow as ma
from webargs import fields, ValidationError
from webargs.falconparser import parser, use_args, use_kwargs
from webargs.core import MARSHMALLOW_VERSION_INFO

hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}


class HelloSchema(ma.Schema):
    name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)


strict_kwargs = {"strict": True} if MARSHMALLOW_VERSION_INFO[0] < 3 else {}
hello_many_schema = HelloSchema(many=True, **strict_kwargs)


class Echo(object):
    def on_get(self, req, resp):
        parsed = parser.parse(hello_args, req)
        resp.body = json.dumps(parsed)
github marshmallow-code / webargs / tests / test_tornadoparser.py View on Github external
def test_it_should_parse_json_arguments(self):
        attrs = {"string": fields.Str(), "integer": fields.List(fields.Int())}

        request = make_json_request({"string": "value", "integer": [1, 2]})

        parsed = parser.parse(attrs, request)

        assert parsed["integer"] == [1, 2]
        assert parsed["string"] == value
github marshmallow-code / webargs / tests / test_tornadoparser.py View on Github external
def test_it_should_parse_multiple_arg_required(self):
        args = {"foo": fields.List(fields.Int(), required=True)}
        request = make_json_request({})
        msg = "Missing data for required field."
        with pytest.raises(tornado.web.HTTPError, match=msg):
            parser.parse(args, request)
github theotherp / nzbhydra / tests / mock / mockServer.py View on Github external
@use_args(api_args)
def apidog(args):
    sleep(2)
    return handle_request("dognzb", args)
github marshmallow-code / webargs / tests / test_core.py View on Github external
def test_parse_list_allow_none(parser, web_request):
    web_request.json = {"foo": None}
    args = {"foo": fields.List(fields.Field(allow_none=True), allow_none=True)}
    assert parser.parse(args, web_request) == {"foo": None}

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 4 months ago

Package Health Score

91 / 100
Full package analysis

Similar packages