{ "info": { "author": "Theron Luhn", "author_email": "theron@luhn.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Framework :: Pyramid", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "# pyramid-marshmallow\n\npyramid-marshmallow is a simple Pyramid plugin that allows you to validate and\nmarshal a JSON HTTP request or response using\n[Marshmallow](http://marshmallow.readthedocs.io/) schemas. You can then\nleverage this to automatically generate an OpenAPI specification for your API.\n\n## Basic usage\n\nInstall the project with `pip install pyramid-marshmallow`.\n\nActivate it by adding `config.include('pyramid_marshmallow')` into your config\nfunction or adding `pyramid.includes = pyramid_marshmallow` into your ini file.\n\nTo validate incoming data, set `validate` to a Marshmallow schema in your\n`view_config`. The request body is parsed as JSON then passed through the\nschema's `load` function. You can access the processed data with\n`request.data`.\n\n```python\nfrom marshmallow import Schema, String\n\n\nclass HelloSchema(Schema):\n name = String()\n\n\n@view_config(\n context=Root,\n name='hello',\n request_method='post',\n validate=HelloSchema(),\n)\ndef hello(context, request):\n return Response(body='Hello, {}'.format(\n request.data['name']\n ))\n```\n\nFor GET requests, the URL parameters are passed into the schema. Value lists\nare not currently supported.\n\nSetting `marshal` in your `view_config` will run the view output through\nmarshmallow (i.e. `Schema.dump`) before going to the renderer. You probably\nwill want to set the renderer to `json`.\n\n```\n@view_config(\n context=Root,\n name='hello',\n request_method='get',\n marshal=HelloSchema(),\n renderer='json',\n)\ndef hello(context, request):\n name = fetch_name()\n return {\n 'name': name,\n }\n```\n\n`validate` and `marshal` operate independently, so can be used separately or\ntogether.\n\nAs a convenience, you can pass in a dictionary to `validate` or `marshal` and\npyramid-marshmallow will turn it into a schema for you.\n\n```python\n@view_config(\n context=Root,\n name='hello',\n request_method='post',\n validate={\n 'name': String(),\n },\n)\n```\n\nYou can also get a schema made from a dictionary by using the\n`pyramid_marshmallow.make_schema` function. This can be useful for `Nested`\nfields.\n\n\n### Error handling\n\nIf the validation fails, a `pyramid_marshmallow.ValidationError` is raised.\nThe `errors` property of the exception contains a dictionary of error messages,\njust like the `Schema.load` method returns.\n\nYou may want to attach a view to this exception to expose the error messages to\nthe user.\n\n```python\n@view_config(\n context=ValidationError,\n renderer='json',\n)\ndef validation_error(context, request):\n request.response.status = 401 # HTTP Bad Request\n return {\n 'errors': context.errors,\n }\n```\n\nA failure during marshalling will result in a\n`pyramid_marshmallow.MarshalError` which behaves in the same manner. It's\nusually less useful to attach a view to that exception, since marshalling\nerrors are usually not encountered during standard operation.\n\n## OpenAPI\n\nBy adding validation and marshalling to your views, we have the opportunity to\nutilize that data to generate documentation. pyramid-marshmallow includes an\nutility that uses [apispec](https://apispec.readthedocs.io/en/stable/) to\ngenerate an [OpenAPI](https://swagger.io/resources/open-api/) specification for\nyour application.\n\nFirst, you'll need to install some extra dependencies.\n\n```bash\npip install pyramid-marshmallow[openapi]\n```\n\nNow you can generate your spec by simply passing in an ini file.\npyramid-marshmallow needs to run your application in order to inspect it, so\nthe ini file should contain all the necessary configuration to do so.\n\n```bash\ngenerate-spec development.ini\n```\n\nThis will output the spec to stdout as JSON. You can set the `--output` flag\nto output the results to a file.\n\nYou can set `--format yaml` to output the spec as YAML instead or\n`--format zip` to output a zip file containing the spec and\n[Swagger UI](https://swagger.io/tools/swagger-ui/), a web interface for viewing\nthe spec.\n\nBy default, your spec will be titled \"Untitled\" and versioned \"0.1.0\". You can\nchange this by setting `openapi.title` and `openapi.version` in your ini file.\n\n### Additional Documentation\n\nTo add additional documentation to schema fields, you can set the `description`\nproperty.\n\n```python\nclass Hello(Schema):\n name = String(required=True, description='Your first and last name.')\n```\n\nDocumentation for the endpoint will be pulled from the view callable's\ndocstring.\n\nYou can also augment the spec by adding a line of three hyphens followed by\nYAML. The YAML will be parsed and merged into to the endpoint's spec. This\ncan be useful for documenting endpoints that cannot be validated or marshalled,\nsuch as endpoints that return an empty body.\n\n```python\n@view_config(\n context=WidgetResource,\n method='post',\n validate=WidgetSchema(),\n)\ndef create_widget(context, request):\n \"\"\"\n Create a new widget.\n ---\n responses:\n 201:\n description: Indicates the widget was successfully created.\n \"\"\"\n create_widget()\n return HTTPCreated()\n```\n\n## URL Traversal\n\nIf you're using Pyramid's URL traversal, the generated spec may be mostly\nempty. This is because pyramid-marshmallow has no way of knowing where in the\nresource tree a resource is. You can denote this by setting the `__path__`\nproperty on each resource.\n\n```python\nclass Widget(Resource):\n __path__ = '/widget'\n```\n\nViews attached to this resource will then be added to the spec.\n\nYou can add parameters to your path via the `__params__` property. You can\nalso tag all attached views via `__tag__`. Once you define a tag in one\nresource, you can use it elsewhere by setting `__tag__` to the tag name.\n\n```python\nclass Widget(Resource):\n __path__ = '/widget/{widgetId}'\n __params__ = [{\n 'name': 'widgetId',\n 'schema': {\n 'type': 'integer',\n },\n }]\n __tag__ = {\n 'name': 'widgets',\n 'description': 'Endpoints for managing a widget.',\n }\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/luhn/pyramid-marshmallow", "keywords": "", "license": "MIT", "maintainer": "Theron Luhn", "maintainer_email": "theron@luhn.com", "name": "pyramid-marshmallow", "package_url": "https://pypi.org/project/pyramid-marshmallow/", "platform": "", "project_url": "https://pypi.org/project/pyramid-marshmallow/", "project_urls": { "Homepage": "https://github.com/luhn/pyramid-marshmallow", "Repository": "https://github.com/luhn/pyramid-marshmallow" }, "release_url": "https://pypi.org/project/pyramid-marshmallow/0.4.1/", "requires_dist": [ "marshmallow (>=2.16,<3.0)", "pyramid (>=1.7,<2.0)", "apispec (>=0.37,<0.38); extra == \"openapi\"", "PyYAML (>=3.10,<4.0); extra == \"openapi\"" ], "requires_python": ">=3.4,<4.0", "summary": "Validate request and response data with Marshmallow and optionally generate an OpenAPI spec.", "version": "0.4.1" }, "last_serial": 5738018, "releases": { "0.4.0": [ { "comment_text": "", "digests": { "md5": "f07d87fb7a8236559fbeac103a3854c5", "sha256": "de6e3575c8dc2fb809d7339de9ebcfa99d732d3c9caeed7e98f08e5bfcd2b9ed" }, "downloads": -1, "filename": "pyramid_marshmallow-0.4.0-py3-none-any.whl", "has_sig": false, "md5_digest": "f07d87fb7a8236559fbeac103a3854c5", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4,<4.0", "size": 2798825, "upload_time": "2018-12-17T22:45:44", "url": "https://files.pythonhosted.org/packages/74/35/54f56ed9da885b40354f250422890f02b5f003b63f8149d5cff9aafd8acb/pyramid_marshmallow-0.4.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "55ff8538b4dc9da37c89390bf771451b", "sha256": "ee686375385fac6baa35c866790205b633cdc8153ae4d6e2fa101fa108a6ee41" }, "downloads": -1, "filename": "pyramid-marshmallow-0.4.0.tar.gz", "has_sig": false, "md5_digest": "55ff8538b4dc9da37c89390bf771451b", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4,<4.0", "size": 2784641, "upload_time": "2018-12-17T22:45:39", "url": "https://files.pythonhosted.org/packages/e3/81/7c88fa7e963474e6060eb211c0a255a168efb27f89df58e0c67f86eba6bb/pyramid-marshmallow-0.4.0.tar.gz" } ], "0.4.1": [ { "comment_text": "", "digests": { "md5": "414e081680adf104205b0c9b8585f685", "sha256": "781e59ba3defdaa69baf521cfe21966aa76f4eb4794cc41c3be183ef63db3c03" }, "downloads": -1, "filename": "pyramid_marshmallow-0.4.1-py3-none-any.whl", "has_sig": false, "md5_digest": "414e081680adf104205b0c9b8585f685", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4,<4.0", "size": 2798826, "upload_time": "2019-01-25T18:19:08", "url": "https://files.pythonhosted.org/packages/ad/0b/6704f7f999bbad9bd855c42c7353d216aeeb7a94449ce32b1ed78fff24b9/pyramid_marshmallow-0.4.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "5d0f7e29e1f8ed9c57da8a74b67d35ad", "sha256": "5c6b13b3f15ed2f737d56da98dbef5dd626019d050a074c97fbd83f04ef8c2ce" }, "downloads": -1, "filename": "pyramid-marshmallow-0.4.1.tar.gz", "has_sig": false, "md5_digest": "5d0f7e29e1f8ed9c57da8a74b67d35ad", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4,<4.0", "size": 2784635, "upload_time": "2019-01-25T18:19:04", "url": "https://files.pythonhosted.org/packages/43/50/837bf16efec170c86c1ddaec3493280a038e1ca9c719d940c530de502d52/pyramid-marshmallow-0.4.1.tar.gz" } ], "0.5.0b1": [ { "comment_text": "", "digests": { "md5": "0025e1fcefd596f8fe2e32fff149e786", "sha256": "7990bc3b932e2621f08423e598cd36c126f42bcbf660b8373015a25e3e82c7f7" }, "downloads": -1, "filename": "pyramid_marshmallow-0.5.0b1-py3-none-any.whl", "has_sig": false, "md5_digest": "0025e1fcefd596f8fe2e32fff149e786", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.5,<4.0", "size": 8146, "upload_time": "2019-08-27T16:44:27", "url": "https://files.pythonhosted.org/packages/2e/ac/afacf9a6a9d66d2c4a957284070bd3e4cf2d2b93419a56a35a197c1b1063/pyramid_marshmallow-0.5.0b1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "45f2c510dc7c667ece660c28dc588f23", "sha256": "58a2f5341d858300d1c06109a1fdee178462ed80e3764bff8a407c2165ee53fd" }, "downloads": -1, "filename": "pyramid-marshmallow-0.5.0b1.tar.gz", "has_sig": false, "md5_digest": "45f2c510dc7c667ece660c28dc588f23", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.5,<4.0", "size": 8416, "upload_time": "2019-08-27T16:44:25", "url": "https://files.pythonhosted.org/packages/1c/98/4a4d4abb0efbfcc56926849eff397226986cf1422ea18a721206b1dae351/pyramid-marshmallow-0.5.0b1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "414e081680adf104205b0c9b8585f685", "sha256": "781e59ba3defdaa69baf521cfe21966aa76f4eb4794cc41c3be183ef63db3c03" }, "downloads": -1, "filename": "pyramid_marshmallow-0.4.1-py3-none-any.whl", "has_sig": false, "md5_digest": "414e081680adf104205b0c9b8585f685", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4,<4.0", "size": 2798826, "upload_time": "2019-01-25T18:19:08", "url": "https://files.pythonhosted.org/packages/ad/0b/6704f7f999bbad9bd855c42c7353d216aeeb7a94449ce32b1ed78fff24b9/pyramid_marshmallow-0.4.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "5d0f7e29e1f8ed9c57da8a74b67d35ad", "sha256": "5c6b13b3f15ed2f737d56da98dbef5dd626019d050a074c97fbd83f04ef8c2ce" }, "downloads": -1, "filename": "pyramid-marshmallow-0.4.1.tar.gz", "has_sig": false, "md5_digest": "5d0f7e29e1f8ed9c57da8a74b67d35ad", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4,<4.0", "size": 2784635, "upload_time": "2019-01-25T18:19:04", "url": "https://files.pythonhosted.org/packages/43/50/837bf16efec170c86c1ddaec3493280a038e1ca9c719d940c530de502d52/pyramid-marshmallow-0.4.1.tar.gz" } ] }