{"id":13705557,"url":"https://github.com/EndurantDevs/webargs-sanic","last_synced_at":"2025-05-05T16:33:09.355Z","repository":{"id":32998205,"uuid":"149121495","full_name":"EndurantDevs/webargs-sanic","owner":"EndurantDevs","description":"Sanic integration with Webargs","archived":false,"fork":false,"pushed_at":"2023-10-02T23:48:34.000Z","size":64,"stargazers_count":14,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-05T22:21:49.619Z","etag":null,"topics":["python","request-validation","sanic","sanic-framework","validation","webargs"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EndurantDevs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-09-17T12:25:31.000Z","updated_at":"2023-11-15T13:12:44.000Z","dependencies_parsed_at":"2024-01-14T21:09:53.227Z","dependency_job_id":"4baf46eb-6c10-4d05-ac2d-b1eca15c9061","html_url":"https://github.com/EndurantDevs/webargs-sanic","commit_stats":{"total_commits":57,"total_committers":4,"mean_commits":14.25,"dds":0.1578947368421053,"last_synced_commit":"5db5a409b3f01420c33e88157aa1456c6db523a6"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndurantDevs%2Fwebargs-sanic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndurantDevs%2Fwebargs-sanic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndurantDevs%2Fwebargs-sanic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndurantDevs%2Fwebargs-sanic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EndurantDevs","download_url":"https://codeload.github.com/EndurantDevs/webargs-sanic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224455887,"owners_count":17314200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","request-validation","sanic","sanic-framework","validation","webargs"],"created_at":"2024-08-02T22:00:43.594Z","updated_at":"2024-11-13T13:30:39.333Z","avatar_url":"https://github.com/EndurantDevs.png","language":"Python","funding_links":[],"categories":["Extensions"],"sub_categories":["Requests and Responses"],"readme":"# webargs-sanic\n[Sanic](https://github.com/huge-success/sanic) integration with [Webargs](https://github.com/sloria/webargs). \n\nParsing and validating request arguments: headers, arguments, cookies, files, json, etc.\n\nIMPORTANT: From version 2.0.0 webargs-sanic requires you to have webargs \u003e=7.0.1. Please be aware of changes happened in version of webargs \u003e 6.0.0. If you need support of webargs 5.x with no location definition, please use previous version(1.5.0) of this module from pypi. \n\n[![Build Status](https://img.shields.io/travis/EndurantDevs/webargs-sanic.svg?logo=travis)](https://app.travis-ci.com/EndurantDevs/webargs-sanic) [![Latest Version](https://img.shields.io/pypi/v/webargs-sanic.svg)](https://pypi.python.org/pypi/webargs-sanic/) [![Python Versions](https://img.shields.io/pypi/pyversions/webargs-sanic.svg)](https://github.com/EndurantDevs/webargs-sanic/blob/master/setup.py) [![Tests Coverage](https://img.shields.io/codecov/c/github/EndurantDevs/webargs-sanic/master.svg)](https://codecov.io/gh/EndurantDevs/webargs-sanic)\n\n[webargs](https://github.com/sloria/webargs) is a Python library for parsing and validating HTTP request arguments, with built-in support for popular web frameworks. webargs-sanic allows you to use it for [Sanic](https://github.com/huge-success/sanic) apps. To read more about webargs usage, please check [Quickstart](https://webargs.readthedocs.io/en/latest/quickstart.html)\n\n## Example Code ##\n\n### Simple Application ###\n```python\nfrom sanic import Sanic\nfrom sanic.response import text\n\nfrom webargs import fields\nfrom webargs_sanic.sanicparser import use_args\n\n\napp = Sanic(__name__)\n\nhello_args = {\n    'name': fields.Str(required=True)\n}\n\n@app.route('/')\n@use_args(hello_args, location=\"query\")\nasync def index(request, args):\n    return text('Hello ' + args['name'])\n\n\n```\n\n### Class-based Sanic app and args/kwargs ###\n\n```python\nfrom sanic import Sanic\nfrom sanic.views import HTTPMethodView\nfrom sanic.response import json\n\nfrom webargs import fields\nfrom webargs_sanic.sanicparser import use_args, use_kwargs\n\n\napp = Sanic(__name__)\n\nclass EchoMethodViewUseArgs(HTTPMethodView):\n    @use_args({\"val\": fields.Int()}, location=\"form\")\n    async def post(self, request, args):\n        return json(args)\n\n\napp.add_route(EchoMethodViewUseArgs.as_view(), \"/echo_method_view_use_args\")\n\n\nclass EchoMethodViewUseKwargs(HTTPMethodView):\n    @use_kwargs({\"val\": fields.Int()}, location=\"query\")\n    async def post(self, request, val):\n        return json({\"val\": val})\n\n\napp.add_route(EchoMethodViewUseKwargs.as_view(), \"/echo_method_view_use_kwargs\")\n```\n\n### Parser without decorator with returning errors as JSON ###\n```python\nfrom sanic import Sanic\nfrom sanic.response import json\n\nfrom webargs import fields\nfrom webargs_sanic.sanicparser import parser, HandleValidationError\n\napp = Sanic(__name__)\n\n@app.route(\"/echo_view_args_validated/\u003cvalue\u003e\", methods=[\"GET\"])\nasync def echo_use_args_validated(request, args):\n    parsed = await parser.parse(\n        {\"value\": fields.Int(required=True, validate=lambda args: args[\"value\"] \u003e 42)}, request, location=\"view_args\"\n    )\n    return json(parsed)\n\n\n# Return validation errors as JSON\n@app.exception(HandleValidationError)\nasync def handle_validation_error(request, err):\n    return json({\"errors\": err.exc.messages}, status=422)\n```\n\n### More complicated custom example ###\n```python\nfrom sanic import Sanic\nfrom sanic import response\nfrom sanic import Blueprint\n\nfrom webargs_sanic.sanicparser import use_kwargs\n\nfrom some_CUSTOM_storage import InMemory\n\nfrom webargs import fields\nfrom webargs import validate\n\nimport marshmallow.fields\nfrom validate_email import validate_email\n\n#usually this should not be here, better to import ;)\n#please check examples for more info\nclass Email(marshmallow.fields.Field):\n\n    def __init__(self, *args, **kwargs):\n        super(Email, self).__init__(*args, **kwargs)\n\n    def _deserialize(self, value, attr, obj):\n        value = value.strip().lower()\n        if not validate_email(value):\n            self.fail('validator_failed')\n        return value\n\nuser_update = {\n    'user_data': fields.Nested({\n        'email': Email(),\n        'password': fields.Str(validate=lambda value: len(value)\u003e=8),\n        'first_name': fields.Str(validate=lambda value: len(value)\u003e=1),\n        'last_name': fields.Str(validate=lambda value: len(value)\u003e=1),\n        'middle_name': fields.Str(),\n        'gender': fields.Str(validate=validate.OneOf([\"M\", \"F\"])),\n        'birth_date': fields.Date(),\n    }),\n    'user_id': fields.Str(required=True, validate=lambda x:len(x)==32),\n}\n\n\nblueprint = Blueprint('app')\nstorage = InMemory()\n\n\n@blueprint.put('/user/')\n@use_kwargs(user_update, location=\"json_or_form\")\nasync def update_user(request, user_id, user_data):\n    storage.update_or_404(user_id, user_data)\n    return response.text('', status=204)\n\napp = Sanic(__name__)\napp.blueprint(blueprint, url_prefix='/')\n\n```\n\nFor more examples and checking how to use custom validations (phones, emails, etc.) please check apps in [Examples](https://github.com/EndurantDevs/webargs-sanic/tree/master/examples/)\n\n## Installing ##\n\nIt is easy to do from `pip`\n\n```\npip install webargs-sanic\n```\n\nor from sources\n\n```\ngit clone git@github.com:EndurantDevs/webtest-sanic.git\ncd webtest-sanic\npython setup.py install\n```\n\n## Running the tests\n\nProject uses common tests from webargs package. Thanks to [Steven Loria](https://github.com/sloria) for [sharing tests in webargs v4.1.0](https://github.com/sloria/webargs/pull/287#issuecomment-422232384). \nMost of tests are run by webtest via [webtest-sanic](https://github.com/EndurantDevs/webtest-sanic). \nSome own tests get run via Sanic's TestClient.\n\nTo be sure everything is fine before installation from sources, just run:\n```bash\npip -r requirements.txt\n```\nand then\n```bash\npython setup.py test\n```\nOr\n```bash\npytest tests/\n```\n\n\n## Authors\n[\u003cimg src=\"https://github.com/EndurantDevs/botstat-seo/raw/master/docs/img/EndurantDevs-big.png\" alt=\"Endurant Developers Python Team\" width=\"150\"\u003e](https://www.EndurantDev.com)\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEndurantDevs%2Fwebargs-sanic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEndurantDevs%2Fwebargs-sanic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEndurantDevs%2Fwebargs-sanic/lists"}