{ "info": { "author": "WiFast", "author_email": "rgb@wifast.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7" ], "description": "BearForm\n========\nEasy data conversion and validation for frontends. BearForm is expected to be\nuseful primarily as part of an API endpoint. Processing deserialized JSON,\nYAML, or whatever flavor you prefer is what it's good at.\n\n[![Build Status](https://travis-ci.org/WiFast/bearform.svg)](https://travis-ci.org/WiFast/bearform)\n\nExample\n-------\nBearForm works like a lot of form validators in that you declare Form classes to define a data schema. For example:\n\n from bearform import Form, Field\n\n class BearForm(Form):\n name = Field(str)\n type = Field(str)\n height = Field(float)\n\nLet's say someone's POSTing bears to a Flask view. You might use it like this:\n\n from bearform import FormError\n\n @app.route(\"/bear\")\n def bear():\n if request.is_json:\n try:\n data = request.get_json()\n bear = BearForm.decode(data)\n bear.validate()\n msg = \"this is a {} bear\".format(bear.type)\n return jsonify({'status': 'success', 'msg': 'msg'})\n except FormError as e:\n return jsonify({'status': 'fail', 'error': str(e)}), 400\n else:\n return jsonify({'status': 'fail', 'error': 'invalid request'}), 400\n\nThe other direction works, too. Lets modify our view to return a small grizzly\nbear named Fluffy when no JSON is posted:\n\n @app.route(\"/bear\")\n def bear():\n if request.is_json:\n try:\n data = request.get_json()\n bear = BearForm.decode(data)\n bear.validate()\n msg = \"this is a {} bear\".format(bear.type)\n return jsonify({'status': 'success', 'msg': 'msg'})\n except FormError as e:\n return jsonify({'status': 'fail', 'error': str(e)}), 400\n else:\n bear = BearForm(name='Fluffy', type='grizzly', height=6.3)\n return jsonify(bear.encode())\n\nBearForm gives you three ways to get the data out of a populated form object:\nattribute access, the to_dict method, and the to_object method. You've seen\nattribute access used in the above examples. The to_dict method does the\nobvious thing: it returns the form as a dictionary. The to_obj method is more\ninteresting as it can populate an existing object with decoded data stored in a\nform object. Let's say we have a Bear document (built with BearField, appropriately):\n\n from bearfield import Document, Field\n\n class Bear(Document):\n class Meta:\n connection = 'bears'\n name = Field(str)\n type = Field(str)\n height = Field(float)\n\nThat looks oddly familiar... Lets decode some data and save it to the database:\n\n data = {'name': 'Fluffy', 'type': 'grizzly', height: '6.3'}\n bear_form = BearForm.decode(data)\n bear = Bear()\n bear_form.to_obj(bear)\n bear.save()\n\nEasy, yes? Since decode returns a form object and to_obj returns the object we\npassed to it we can shorten the example to this:\n \n data = {'name': 'Fluffy', 'type': 'grizzly', height: '6.3'}\n BearForm.decode(data).to_obj(Bear()).save()\n\nExpect even better BearField integration in the future.\n\nBut what about validation? Including validators on fields is straightforward:\n\n from bearform import ValidationError\n\n def is_not_empty(cls, name, value):\n if not value:\n raise ValidationError(\"{} cannot be empty\".format(name))\n\n def is_positive(cls, name, value):\n if value <= 0:\n raise ValidationError(\"{} must be a positive value\".format(name))\n\n class BearForm(form):\n name = Field(str, validators=[is_not_empty])\n type = Field(str)\n height = Field(float, validators=[is_positive])\n\nNow during decoding a ValidationError will be raised of the name is empty or\nthe bear does not have a positive height.\n\nLicense\n-------\nCopyright (c) 2014 WiFast, Inc. This project and all of its contents is licensed under the\nBSD-derived license as found in the included [LICENSE][1] file.\n\n[1]: https://github.com/WiFast/bearform/blob/master/LICENSE \"LICENSE\"", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/WiFast/bearform", "keywords": "python json frontend validation", "license": "BSD-derived", "maintainer": null, "maintainer_email": null, "name": "bearform", "package_url": "https://pypi.org/project/bearform/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/bearform/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/WiFast/bearform" }, "release_url": "https://pypi.org/project/bearform/1.2/", "requires_dist": null, "requires_python": null, "summary": "Easy data conversion and validation for frontends.", "version": "1.2" }, "last_serial": 1153814, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "05b36444adf9294c79620e9a92f5a5d6", "sha256": "f6f94b9b5f77110f1fe5449d181860d9a6c40dfb62d780bcc512be0f3294c1eb" }, "downloads": -1, "filename": "bearform-1.0.tar.gz", "has_sig": false, "md5_digest": "05b36444adf9294c79620e9a92f5a5d6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11771, "upload_time": "2014-06-19T17:32:10", "url": "https://files.pythonhosted.org/packages/d7/7c/c37dd13459adb19008e747a0868579939a3b25b6081eb3865616626b9eb3/bearform-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "aa183ab679d866f38febe1ef2623f9bf", "sha256": "ec12476145f455b2ffc88d9e389264dab1e7f04c281153cbccb39fcdad70e859" }, "downloads": -1, "filename": "bearform-1.1.tar.gz", "has_sig": false, "md5_digest": "aa183ab679d866f38febe1ef2623f9bf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9031, "upload_time": "2014-07-08T23:03:58", "url": "https://files.pythonhosted.org/packages/96/8a/3447aae0481eb2fdb4df00cae835d9e61c2e921efa6789b1c3b41369afe0/bearform-1.1.tar.gz" } ], "1.2": [ { "comment_text": "", "digests": { "md5": "6c6817580dac58c8d22921d6eeac1626", "sha256": "920333fe15fe2f917aa6011b0dafb56abeb47252788f86fa766e829b40061a27" }, "downloads": -1, "filename": "bearform-1.2.tar.gz", "has_sig": false, "md5_digest": "6c6817580dac58c8d22921d6eeac1626", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9039, "upload_time": "2014-07-10T18:31:59", "url": "https://files.pythonhosted.org/packages/69/bb/3f6b362257919c4e0b564190136aa69e0c31ccb51561e12c6fd4dfa4635f/bearform-1.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6c6817580dac58c8d22921d6eeac1626", "sha256": "920333fe15fe2f917aa6011b0dafb56abeb47252788f86fa766e829b40061a27" }, "downloads": -1, "filename": "bearform-1.2.tar.gz", "has_sig": false, "md5_digest": "6c6817580dac58c8d22921d6eeac1626", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9039, "upload_time": "2014-07-10T18:31:59", "url": "https://files.pythonhosted.org/packages/69/bb/3f6b362257919c4e0b564190136aa69e0c31ccb51561e12c6fd4dfa4635f/bearform-1.2.tar.gz" } ] }