{ "info": { "author": "ausaki", "author_email": "ljm51689@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Topic :: Internet :: WWW/HTTP :: Dynamic Content", "Topic :: Software Development :: Libraries" ], "description": "# python-validator\n\npython-validator \u662f\u4e00\u4e2a\u7c7b\u4f3c\u4e8e Django ORM \u7684\u6570\u636e\u6821\u9a8c\u5e93\uff0c\u9002\u7528\u4e0e\u4efb\u4f55\u9700\u8981\u8fdb\u884c\u6570\u636e\u6821\u9a8c\u7684\u5e94\u7528\uff0c\u6bd4\u8f83\u5e38\u89c1\u7684\u662f Web \u540e\u7aef\u6821\u9a8c\u524d\u7aef\u7684\u8f93\u5165\u6570\u636e\u3002\n\n[\u6587\u6863](https://ausaki.github.io/python-validator)\n\n---\n\n[![PyPi](https://img.shields.io/pypi/v/python-validator.svg)\n](https://pypi.org/project/python-validator/)\n[![PyPI - Status](https://img.shields.io/pypi/status/python-validator.svg)](https://pypi.org/project/python-validator/)\n[![PyPi - Python Version](https://img.shields.io/pypi/pyversions/python-validator.svg)](https://pypi.org/project/python-validator/)\n[![PyPI - License](https://img.shields.io/pypi/l/python-validator.svg)](https://pypi.org/project/python-validator/)\n\n![GitHub last commit](https://img.shields.io/github/last-commit/ausaki/python-validator.svg)\n[![Build Status - master](https://travis-ci.org/ausaki/python-validator.svg?branch=master)](https://travis-ci.org/ausaki/python-validator)\n\n---\n\n## \u7279\u6027\n\n- \u652f\u6301 python2 \u548c python3\u3002\n\n- \u4f7f\u7528\u7c7b\u63cf\u8ff0\u6570\u636e\u7ed3\u6784\uff0c\u6570\u636e\u5b57\u6bb5\u4e00\u76ee\u4e86\u7136\u3002\u53e6\u5916\u4e5f\u652f\u6301\u4f7f\u7528\u5b57\u5178\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u3002\n\n- \u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u7528\u4e8e\u6d4b\u8bd5\u7684 mocking data\u3002\n\n- \u53ef\u4ee5\u6253\u5370\u51fa\u6e05\u6670\u7684\u6570\u636e\u7ed3\u6784\u3002\n\n- \u6613\u4e8e\u6269\u5c55\u3002\n\n\n## \u4f9d\u8d56\n\n- six\n\n- IPy\n\n- pytz[\u53ef\u9009\uff0c`DatetimeField` \u7684 `tzinfo` \u53c2\u6570\u9700\u8981\u4e00\u4e2a `tzinfo` \u5bf9\u8c61]\n\n\n## \u5b89\u88c5\n\n`pip install python-validator`\n\n\n## \u5feb\u901f\u5165\u95e8\n\n\u5047\u8bbe\u73b0\u5728\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u4e0a\u4f20\u7528\u6237\u4fe1\u606f\u7684\u63a5\u53e3 `POST /api/user/`\uff0c\u7528\u6237\u4fe1\u606f\u5982\u4e0b\uff1a\n\n| \u5b57\u6bb5 | \u7c7b\u578b | \u63cf\u8ff0 |\n|--|--|--|\n|name|String| \u5fc5\u9009 |\n|age|integer| \u53ef\u9009\uff0c\u9ed8\u8ba4 20|\n|sex|String, 'f'\u8868\u793a\u5973, 'm'\u8868\u793a\u7537 | \u53ef\u9009, \u9ed8\u8ba4 None|\n\n\u539f\u59cb\u7684\u3001\u67af\u71e5\u65e0\u5473\u7684\u3001\u91cd\u590d\u6027\u52b3\u52a8\u7684\u6570\u636e\u6821\u9a8c\u4ee3\u7801\u53ef\u80fd\u662f\u4e0b\u9762\u8fd9\u6837\uff1a\n\n```python\ndef user(request):\n # data = json.loads(request.body)\n data = {\n 'age': '24f',\n 'sex': 'f'\n }\n name = data.get('name')\n age = data.get('age', 20)\n sex = dage.get('sex')\n\n if name is None or len(name) == 0:\n return Response('\u5fc5\u987b\u63d0\u4f9b name', status=400)\n\n try:\n age = int(age)\n except ValueError as e:\n return Response('age \u683c\u5f0f\u9519\u8bef', status=400)\n\n if sex is not None and sex not in ('f', 'm'):\n return Response('sex \u683c\u5f0f\u9519\u8bef', status=400)\n\n user_info = {\n 'name': name,\n 'age': age,\n 'sex': sex,\n }\n ...\n```\n\n\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u603b\u7684\u6765\u8bf4\u6709\u51e0\u4e2a\u95ee\u9898\uff1a\n\n- \u67af\u71e5\u65e0\u5473\u548c\u91cd\u590d\u6027\u4ee3\u7801\uff0c\u4e0d\u65ad\u7684\u53d6\u51fa\u6570\u636e\uff0c\u68c0\u67e5\u5b57\u6bb5\u662f\u5426\u7f3a\u5931\uff0c\u7c7b\u578b\u662f\u5426\u5408\u6cd5\u7b49\u7b49\u3002\n\n- \u4ece\u6570\u636e\u6821\u9a8c\u7684\u4ee3\u7801\u65e0\u6cd5\u8f7b\u6613\u770b\u51fa\u7528\u6237\u4fe1\u606f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5373\u5b57\u6bb5\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\uff0c\u662f\u5426\u53ef\u9009\uff0c\u9ed8\u8ba4\u503c\u662f\u4ec0\u4e48\u3002\n\n**\u4f7f\u7528 python-validator \u6821\u9a8c\u6570\u636e**\n\n\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a UserInfoValidator \u7c7b\n\n```python\n# validators.py\nfrom validator import Validator, StringField, IntegerField, EnumField\n\nclass UserInfoValidator(Validator):\n name = StringField(max_length=50, required=True)\n age = IntegerField(min_value=1, max_value=120, default=20)\n sex = EnumField(choices=['f', 'm'])\n```\n\n\u63a5\u4e0b\u6765\u4f7f\u7528 `UserInfoValidator` \u8fdb\u884c\u6570\u636e\u6821\u9a8c\uff0c\n\n```python\nfrom .validators import UserInfoValidator\n\ndef user(request):\n # data = json.loads(request.body)\n data = {\n 'age': '24',\n 'sex': 'f'\n }\n v = UserInfoValidator(data)\n if not v.is_valid():\n return Response({'msg': v.str_errors, 'code': 400}, status=400)\n\n user_info = v.validated_data\n ...\n```\n\n`v.str_errors` \u662f\u4e00\u4e2a\u5b57\u6bb5\u540d - \u9519\u8bef\u4fe1\u606f\u7684 dict\uff0c\u4f8b\u5982\uff1a\n\n```python\n{'age': 'got a wrong type: str, expect integer', 'name': 'Field is required'}\n```\n\n\u9519\u8bef\u4fe1\u606f\u89e3\u91ca\uff1a\n\n- `age` \u7b49\u4e8e \"24\"\uff0c\u4e0d\u662f\u5408\u6cd5\u7684 `int` \u7c7b\u578b\u3002\n\n- `name` \u662f\u5fc5\u987b\u63d0\u4f9b\u7684\uff0c\u4e14\u6ca1\u6709\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002\n\n\nv.validated_data \u662f\u6821\u9a8c\u540e\u5408\u6cd5\u7684\u6570\u636e\uff0c\u4f8b\u5982\uff1a\n\n```json\n{'age': 24, 'name': u'Michael', 'sex': 'f'}\n```\n\n\u4e0b\u9762\u662f\u4e00\u4e9b\u9519\u8bef\u6570\u636e\u7684\u4f8b\u5b50\uff1a\n\n```python\ndata: {'age': 24, 'name': 'abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc', 'sex': 'f'}\nis_valid: False\nerrors: {'name': 'string is too long, max-lenght is 50'}\nvalidated_data: None\n```\n\n```python\ndata: {'age': 24, 'name': 'Michael', 'sex': 'c'}\nis_valid: False\nerrors: {'sex': \"'c' not in the choices\"}\nvalidated_data: None\n```\n\n\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u53d1\u73b0\u4e86 `IntegerField` \u4e0d\u63a5\u53d7 \u201c\u6570\u5b57\u5b57\u7b26\u4e32\u201d\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d `age` \u662f\u4e00\u4e2a `IntegerField`\uff0c\u5f62\u5982 `'24'` \u8fd9\u6837\u7684\u503c\u662f\u975e\u6cd5\u7684\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b `IntegerField` \u4e0d\u8981\u8fd9\u4e48\u4e25\u683c\uff0c`'24'` \u8fd9\u6837\u7684\u503c\u4e5f\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a `strict` \u9009\u9879\u8bbe\u4e3a `False`\uff0c\u5982\uff1a`age = IntegerField(min_value=1, max_value=120, default=20, strict=False)`\u3002\u5f53 `strict` \u9009\u9879\u4e3a `False` \u65f6\uff0cpython-validator \u4f1a\u5c1d\u8bd5\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u5047\u5982\u8f6c\u6362\u5931\u8d25\u5219\u4f1a\u62a5\u9519\u3002\n\n\u63a5\u4e0b\u6765\u4f60\u53ef\u4ee5 [\u67e5\u770b\u8fdb\u9636](https://ausaki.github.io/python-validator/advanced/) \u4e86\u89e3 python-validator \u66f4\u591a\u7684\u7528\u6cd5\uff0c[\u67e5\u770b\u5b57\u6bb5 API](https://ausaki.github.io/python-validator/fields/) \u4e86\u89e3\u5b57\u6bb5\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\n\n\n## \u6d4b\u8bd5\n\n\u4f7f\u7528 tox \u548c pytest \u8fdb\u884c\u4ee3\u7801\u6d4b\u8bd5\u3002\n\n\u63a8\u8350\u4f7f\u7528 [pipenv](https://github.com/pypa/pipenv) \u6765\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u3002\n\n**\u5047\u5982\u4f7f\u7528 pipenv\uff1a**\n\n- `pipenv install`(\u5b89\u88c5\u4f9d\u8d56\u5e93)\n\n- `pipenv run test`\n\n**\u5047\u5982\u4f7f\u7528 pip\uff1a**\n\n- `pip install -r requirements.txt`(\u5b89\u88c5\u4f9d\u8d56\u5e93)\n\n- `tox`\n\n\n## \u5176\u5b83\n\n\u5982\u679c\u9047\u5230 BUG \u6216\u8005\u6709\u4efb\u4f55\u5efa\u8bae\uff0c\u6b22\u8fce\u63d0\u4ea4 issue \u6216\u8005 PR\u3002\n\n\u5982\u679c\u5e0c\u671b\u8d21\u732e\u4ee3\u7801\uff0c\u8bf7\u5c3d\u91cf\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u5e76\u6d4b\u8bd5\u4f60\u7684\u4ee3\u7801\uff0c\u7136\u540e\u518d\u63d0\u4ea4 PR\u3002\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/ausaki/python-validator", "keywords": "validator,like Django-ORM,data validator,validation,python", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "python-validator", "package_url": "https://pypi.org/project/python-validator/", "platform": "", "project_url": "https://pypi.org/project/python-validator/", "project_urls": { "Homepage": "http://github.com/ausaki/python-validator" }, "release_url": "https://pypi.org/project/python-validator/0.0.7/", "requires_dist": [ "six", "IPy", "pytz; extra == 'tzinfo'" ], "requires_python": ">=2.7", "summary": "a data validator like Django ORM", "version": "0.0.7" }, "last_serial": 4923317, "releases": { "0.0.2": [ { "comment_text": "", "digests": { "md5": "6335718b8929a8236b5c9ea7d9d89ed8", "sha256": "87fa3379284842b47c8b71fbfe41261f9232b86938d6aea6461de7e6c893cfe9" }, "downloads": -1, "filename": "python_validator-0.0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "6335718b8929a8236b5c9ea7d9d89ed8", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.6", "size": 10822, "upload_time": "2018-07-27T03:30:48", "url": "https://files.pythonhosted.org/packages/9d/c3/d41f128d7ac38514f2dd2979050f4356554d04631319405f70a6b4e2909a/python_validator-0.0.2-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "3c726fc620a468bc21c54218c8bad78d", "sha256": "9b5453fea2a4d4e818eb0c29c10d4a530b5e7e61a11c6299e96465bdb11f9e98" }, "downloads": -1, "filename": "python-validator-0.0.2.tar.gz", "has_sig": false, "md5_digest": "3c726fc620a468bc21c54218c8bad78d", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.6", "size": 11661, "upload_time": "2018-07-27T03:30:49", "url": "https://files.pythonhosted.org/packages/cb/75/3c0f555b871d13dd319a79af765cca24a43ac6ca23031134e66e142332c7/python-validator-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "7ba2cf0615146b7c28ca15e36ad4edc9", "sha256": "46800e6df349b936f8638ec7cda9ddbe87a2b5dafeda258a109e44cc49c40cd7" }, "downloads": -1, "filename": "python_validator-0.0.3-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "7ba2cf0615146b7c28ca15e36ad4edc9", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.6", "size": 13118, "upload_time": "2018-07-28T12:22:36", "url": "https://files.pythonhosted.org/packages/72/e2/346637fefe610c99ba3189805e2fa81d30392b5c860a3abe7d0772626074/python_validator-0.0.3-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "d25d71fe4ab8face65549df560885190", "sha256": "9d70149212181b1c3222d48373397b2c9c213351e49c1e980929192f3224d9c9" }, "downloads": -1, "filename": "python-validator-0.0.3.tar.gz", "has_sig": false, "md5_digest": "d25d71fe4ab8face65549df560885190", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.6", "size": 14025, "upload_time": "2018-07-28T12:22:39", "url": "https://files.pythonhosted.org/packages/7a/4e/d5997dccf003369bec8eff75a47198f2f83a02aa3930f41d1f218cd1fb92/python-validator-0.0.3.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "c3b154e6eb53fe5864940b3e2d270f6f", "sha256": "d2041c98cdcfefc390e36072989d6831da5247af094dd46843ce821cd2331efc" }, "downloads": -1, "filename": "python_validator-0.0.4-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "c3b154e6eb53fe5864940b3e2d270f6f", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 13120, "upload_time": "2018-07-30T05:54:22", "url": "https://files.pythonhosted.org/packages/82/9c/dc6b51a6b1a104cdf784b0bce37b9596d4699579228815c9bcd5f1a76f67/python_validator-0.0.4-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "089647d1d10cfeb99a306db63ab60f96", "sha256": "854db63c03a88438d9ba2d93b0dc164cb5e2b2aaed62db77e6f33304cb8f4316" }, "downloads": -1, "filename": "python-validator-0.0.4.tar.gz", "has_sig": false, "md5_digest": "089647d1d10cfeb99a306db63ab60f96", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 14038, "upload_time": "2018-07-30T05:54:23", "url": "https://files.pythonhosted.org/packages/e3/43/18c2030a09a146d88324e6970725f1e41f412d18ed2e01645a5ccda967d9/python-validator-0.0.4.tar.gz" } ], "0.0.5.post0": [ { "comment_text": "", "digests": { "md5": "5e6a9fe601590fe3136c929c850760f9", "sha256": "38080f152b2ed4378aefefe3c157d1e5d0ff9a2a57c9cd60b7262dc81a457cf9" }, "downloads": -1, "filename": "python_validator-0.0.5.post0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "5e6a9fe601590fe3136c929c850760f9", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 18747, "upload_time": "2018-08-01T08:34:09", "url": "https://files.pythonhosted.org/packages/6c/77/29ac150c81d7a5b46ce8b83bf6292ff1a7e4a9b51f03869f2814713cebd2/python_validator-0.0.5.post0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b68b717b2d64ca721f42eb13af191d93", "sha256": "41d1f97385a5a47868ac11d9ff46e98e13f5773fc558e510494c02aa13a6f242" }, "downloads": -1, "filename": "python-validator-0.0.5.post0.tar.gz", "has_sig": false, "md5_digest": "b68b717b2d64ca721f42eb13af191d93", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 14730, "upload_time": "2018-08-01T08:34:11", "url": "https://files.pythonhosted.org/packages/2e/9c/ce1385735c079aafba440d363777b9f5fd3cbb0ff0ba5b8785b312bf89bb/python-validator-0.0.5.post0.tar.gz" } ], "0.0.6": [ { "comment_text": "", "digests": { "md5": "a3c9a35803cc0ee2e945aa3964fea7d1", "sha256": "40e85d8fbf27f4fe63719bce015b1d9d8ffb126a9ae11d4e817dced9d0510627" }, "downloads": -1, "filename": "python_validator-0.0.6-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "a3c9a35803cc0ee2e945aa3964fea7d1", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 18734, "upload_time": "2018-08-01T08:30:04", "url": "https://files.pythonhosted.org/packages/e0/b5/b6a80deb0dfbbed1049d1d515589b9ff83725b35143319e7c728e4aa789d/python_validator-0.0.6-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "5bfea242064211a9a0ad5f1f26e6301a", "sha256": "8ffcd3565be4cbc036588f71207db8abf6cd2524ec15254285e635d20d930cc9" }, "downloads": -1, "filename": "python-validator-0.0.6.tar.gz", "has_sig": false, "md5_digest": "5bfea242064211a9a0ad5f1f26e6301a", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 17518, "upload_time": "2018-08-01T08:30:06", "url": "https://files.pythonhosted.org/packages/71/b5/ec4e6586afcd48c5a9570b22414f19bb1e44b4dd3b5c5de3f94296be6296/python-validator-0.0.6.tar.gz" } ], "0.0.7": [ { "comment_text": "", "digests": { "md5": "da0b240b48659f9b6efc7e7d016cbfb6", "sha256": "1be9b4f6a8a30b43d4a731feaa06a9ebba0633b55c9f162264a2a9e3046bcf28" }, "downloads": -1, "filename": "python_validator-0.0.7-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "da0b240b48659f9b6efc7e7d016cbfb6", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 18747, "upload_time": "2019-03-11T02:17:57", "url": "https://files.pythonhosted.org/packages/5e/2e/622dee65fd2190f480717926dfa4bfbf7cff2737226fbc07ee05504a31a2/python_validator-0.0.7-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6f2ad9d8dcd1194192d4fd552cb3a390", "sha256": "9a2a6b4c182da8b14737d1dbb794eac74b1509f47aece81a649e6298a0dad3e4" }, "downloads": -1, "filename": "python-validator-0.0.7.tar.gz", "has_sig": false, "md5_digest": "6f2ad9d8dcd1194192d4fd552cb3a390", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 17553, "upload_time": "2019-03-11T02:17:59", "url": "https://files.pythonhosted.org/packages/bc/85/60bc0ad5d3bf494b51496a5e879c54037e0bad86fdb9b70b2714c21b8a47/python-validator-0.0.7.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "da0b240b48659f9b6efc7e7d016cbfb6", "sha256": "1be9b4f6a8a30b43d4a731feaa06a9ebba0633b55c9f162264a2a9e3046bcf28" }, "downloads": -1, "filename": "python_validator-0.0.7-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "da0b240b48659f9b6efc7e7d016cbfb6", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7", "size": 18747, "upload_time": "2019-03-11T02:17:57", "url": "https://files.pythonhosted.org/packages/5e/2e/622dee65fd2190f480717926dfa4bfbf7cff2737226fbc07ee05504a31a2/python_validator-0.0.7-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6f2ad9d8dcd1194192d4fd552cb3a390", "sha256": "9a2a6b4c182da8b14737d1dbb794eac74b1509f47aece81a649e6298a0dad3e4" }, "downloads": -1, "filename": "python-validator-0.0.7.tar.gz", "has_sig": false, "md5_digest": "6f2ad9d8dcd1194192d4fd552cb3a390", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7", "size": 17553, "upload_time": "2019-03-11T02:17:59", "url": "https://files.pythonhosted.org/packages/bc/85/60bc0ad5d3bf494b51496a5e879c54037e0bad86fdb9b70b2714c21b8a47/python-validator-0.0.7.tar.gz" } ] }