{ "info": { "author": "Tomas Sandven", "author_email": "tomas191191@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "\n# bottle-jsonschema\n\nBottle plugin for automatically validating JSON schemas for all relevant\nrequests.\n\n## Installation\n\n pip install bottle_jsonschema\n\n## Usage example\n\n```python\nimport bottle\nfrom bottle.ext.jsonschema import JSONSchemaPlugin, SchemaValidationError\n\nbottle.install(JSONSchemaPlugin())\n\n@bottle.error(400)\ndef handle_error_400(error):\n # This forwards the error directly to the user, which will display a nicely\n # formatted JSON object containing the validation errors.\n if isinstance(error, SchemaValidationError):\n return response\n\n # Handle other error situations...\n\n return json.dumps({\"error\": \"invalid request\"})\n```\n\nThe error object contains a list of strings describing the validation errors.\nYou can easily customize how the errors are displayed, here's how you can\ndisplay them as a HTML list:\n\n```python\n@bottle.error(400)\ndef handle_error_400(error):\n if isinstance(error, SchemaValidationError):\n response.content_type = \"text/html\"\n\n error_list = \"\\n\".join(\n \"
  • {}
  • \".format(x) for x in error.validation_errors\n )\n\n return \"\"\"\n

    JSON schema validation failed

    \n

    Errors:

    \n \n \"\"\".format(error_list)\n\n return response\n```\n\n## Output samples\n\nHere are some samples of the output of the plugin, given the following error\nhandler:\n\n```python\n@bottle_app.error(400)\ndef handle_error_400(error):\n if isinstance(error, SchemaValidationError):\n return response\n\n response.content_type = \"application/json\"\n return json.dumps({\"error\": error.body})\n```\n\n```\n$ curl -s -X PUT \"http://127.0.0.1:1300/login\" | jq .\n{\n \"error\": \"content type application/json required\"\n}\n```\n\n```\n$ curl -s -X PUT -H \"Content-Type: application/json\" \"http://127.0.0.1:1300/login\" | jq .\n{\n \"error\": \"json payload required\"\n}\n```\n\n```\n$ curl -s -X PUT -H \"Content-Type: application/json\" -d '{}' \"http://127.0.0.1:8080/login\" | jq .\n{\n \"error\": \"payload failed json schema validation\",\n \"validation_errors\": [\n \"failed constraint: required: ['email', 'password']\"\n ]\n}\n```\n\n```\n$ curl -s -X PUT -H \"Content-Type: application/json\" -d '{\"email\": \"x\", \"password\": \"123\"}' \"http://127.0.0.1:1300/login\" | jq .\n{\n \"error\": \"payload failed json schema validation\",\n \"validation_errors\": [\n \"failed constraint: properties.password.minLength: 8\",\n \"failed constraint: properties.email.minLength: 6\"\n ]\n}\n```\n\n```\n$ curl -s -X PUT -H \"Content-Type: application/json\" -d '{\"email\": \"hubro@example.net\", \"password\": \"12345678\"}' \"http://127.0.0.1:1300/login\" | jq .\n{\n \"code\": \"success\"\n}\n```\n\n## More information\n\nHere's basically everything the plugin does for every request:\n\n1. Check the HTTP method of the request. If it's one of POST, PATCH or PUT, then\n continue. Otherwise stop.\n1. Try to find a schema for the request. If a schema was found, continue,\n otherwise stop. The default logic for finding a schema is explained below,\n and can be overridden.\n1. Check that the request content type is \"application/json\". If it's not, raise\n a 400 Bad Request error.\n1. Check that the request contains a JSON parseable payload. If not, raise a 400\n Bad Request error.\n1. Validate the payload using\n [jsonschema](https://github.com/Julian/jsonschema). If there were no errors,\n stop. Otherwise raise a 400 Bad Request error.\n\n### How do I override which HTTP methods trigger schema validation?\n\n```python\nbottle.install(JSONSchemaPlugin(methods=(\"GET\", \"POST\")))\n```\n\n### How does the plugin find schemas?\n\nBy default, the plugin finds schemas by checking your Bottle application's\nresource manager for \"schemas/<name>.json\". The default strategy for converting\na route to a schema name is:\n\n```\nPUT /login -> schemas/login[.PUT].json\nPOST /admin/users -> schemas/admin.users[.POST].json\nPUT /admin/users/ -> schemas/admin.users.id[.POST].json\nPATCH /users/ -> schemas/users.name[.PATCH].json\n```\n\nBasically, it replaces slashes with dots, replaces wildcards with the wildcard\nname and strips leading and trailing slashes. The HTTP method is optional, as\nsignified by the square brackets.\n\nYou can override the naming strategy like this:\n\n```python\ndef get_schema_name():\n # Generate one or more schema names for the current request.\n\n return [\"list\", \"of\", \"names\", \"that\", \"will\", \"be\", \"tried\", \"in\", \"order\"]\n\nbottle.install(JSONSchemaPlugin(schema_name=get_schema_name))\n```\n\nOr you can override the whole schema lookup altogether:\n\n```python\ndef find_schema():\n # Find the correct schema for the current request and return it as a dict.\n\n # Returning None will skip schema validation.\n return None\n\nbottle.install(JSONSchemaPlugin(schema_lookup=find_schema))\n```\n\n### How do I set up the resource manager to work with this plugin?\n\nAssuming you have a project folder layout like this:\n\n```\n.\n\u251c\u2500\u2500 assets\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 schemas\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 login.json\n\u2514\u2500\u2500 myapp.py\n```\n\nThen you just need this line:\n\n```python\napp.resources.add_path(\"assets/\")\n```\n\nOr, if you're using the default app:\n\n```python\nbottle.default_app().resources.add_path(\"assets/\")\n```\n\nNow JSONSchemaPlugin will validate requests against your schemas without any\nfurther configuration. You can make it work with any project layout by\noverriding the schema naming strategy as explained above, or you can skip the\nresource manager altogether by overriding the schema lookup function.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Hubro/bottle-jsonschema", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "bottle-jsonschema", "package_url": "https://pypi.org/project/bottle-jsonschema/", "platform": "", "project_url": "https://pypi.org/project/bottle-jsonschema/", "project_urls": { "Homepage": "https://github.com/Hubro/bottle-jsonschema" }, "release_url": "https://pypi.org/project/bottle-jsonschema/0.0.1/", "requires_dist": null, "requires_python": "", "summary": "Automatic JSON schema validation for Bottle", "version": "0.0.1" }, "last_serial": 4088688, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "a4fb2cd71d1418d9430cb141ddc07aa5", "sha256": "721ef9029501f9e24c1f02630bc8c77e7a023867d9ef5fee8ac0287a008b37b3" }, "downloads": -1, "filename": "bottle_jsonschema-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "a4fb2cd71d1418d9430cb141ddc07aa5", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4516, "upload_time": "2018-07-21T14:24:50", "url": "https://files.pythonhosted.org/packages/2d/b1/d53527c06708c0a82c60f72d3c0a6d15f5943b24b174074418b8b7d45b0e/bottle_jsonschema-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "be823f667424fbeef6f938c8898680c2", "sha256": "0452abd1ceefb8b2327eee297163a9b8b3f0faa5547f2991e7acc079a421c9cb" }, "downloads": -1, "filename": "bottle-jsonschema-0.0.1.tar.gz", "has_sig": false, "md5_digest": "be823f667424fbeef6f938c8898680c2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4470, "upload_time": "2018-07-21T14:24:52", "url": "https://files.pythonhosted.org/packages/60/ce/d07239a9b8cfa761de09672efda3edf6bd85086ec970d112e5a5d0d25ded/bottle-jsonschema-0.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "a4fb2cd71d1418d9430cb141ddc07aa5", "sha256": "721ef9029501f9e24c1f02630bc8c77e7a023867d9ef5fee8ac0287a008b37b3" }, "downloads": -1, "filename": "bottle_jsonschema-0.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "a4fb2cd71d1418d9430cb141ddc07aa5", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4516, "upload_time": "2018-07-21T14:24:50", "url": "https://files.pythonhosted.org/packages/2d/b1/d53527c06708c0a82c60f72d3c0a6d15f5943b24b174074418b8b7d45b0e/bottle_jsonschema-0.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "be823f667424fbeef6f938c8898680c2", "sha256": "0452abd1ceefb8b2327eee297163a9b8b3f0faa5547f2991e7acc079a421c9cb" }, "downloads": -1, "filename": "bottle-jsonschema-0.0.1.tar.gz", "has_sig": false, "md5_digest": "be823f667424fbeef6f938c8898680c2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4470, "upload_time": "2018-07-21T14:24:52", "url": "https://files.pythonhosted.org/packages/60/ce/d07239a9b8cfa761de09672efda3edf6bd85086ec970d112e5a5d0d25ded/bottle-jsonschema-0.0.1.tar.gz" } ] }