{ "info": { "author": "Davide Zanotti", "author_email": "davidezanotti@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Topic :: Software Development :: Libraries" ], "description": ".. image:: https://travis-ci.org/daveoncode/pyvaru.svg?branch=master\n :target: https://travis-ci.org/daveoncode/pyvaru\n\n.. image:: https://codecov.io/gh/daveoncode/pyvaru/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/daveoncode/pyvaru\n\n\nWhat is pyvaru?\n---------------\n\nPyvaru is a simple, flexible and unobtrusive data validation library for Python 3 (3.4+),\nbased on the concept of validation rules.\n\nFrom the software design point of view, a rule is a class implementing the strategy pattern, \nby encapsulating the validation logic in an interface method called ``apply()``.\n\nThe library already offers a series of common validation rules like:\n \n- ``TypeRule`` (it checks that the target value is an instance of the expected type)\n- ``FullStringRule`` (it checks the the target value is a string with content)\n- ``ChoiceRule`` (it checks that the target value is contained in a list of available options)\n- ``MinValueRule`` (it checks that the target value is >= x) *\n- ``MaxValueRule`` (it checks that the target value is <= x) *\n- ``MinLengthRule`` (it checks that the target value length is >= x) *\n- ``MaxLengthRule`` (it checks that the target value length is <= x) *\n- ``RangeRule`` (it checks that the target value is contained in a given ``range``)\n- ``IntervalRule`` (it checks that the target value is contained in a given interval)\n- ``PatternRule`` (it checks that the target value matches a given regular expression)\n- ``PastDateRule`` (it checks that the target value is a date in the past)\n- ``FutureDateRule`` (it checks that the target value is a date in the future)\n- ``UniqueItemsRule`` (it checks that the target iterable does not contain duplicated items)\n \n\n\\* where \"x\" is a provided reference value\n\nThe developer is then free to create his custom rules by extending the abstract ``ValidationRule``\nand implementing the logic in the ``apply()`` method. For example:\n\n.. code-block:: python\n\n class ContainsHelloRule(ValidationRule):\n def apply(self) -> bool:\n return 'hello' in self.apply_to\n\nThese rules are then executed by a ``Validator``, which basically executes them in the provided\norder and eventually returns a ``ValidationResult`` containing the validation response.\n\n\nInstallation\n------------\n\n``pip install pyvaru``\n\n\nUsage\n-----\n \nGiven an existing model to validate, like the one below\n(but it could be a simple dictionary or any data structure since `pyvaru`\ndoes not make any assumption on the data format):\n\n.. code-block:: python\n\n class User:\n def __init__(self, first_name: str, last_name: str, date_of_birth: datetime, sex: str):\n self.first_name = first_name\n self.last_name = last_name\n self.date_of_birth = date_of_birth\n self.sex = sex\n\n \nWe have to define a validator, by implementing the ``get_rules()`` method and for each field we want to\nvalidate we have to provide one or more proper rule(s).\n\n.. code-block:: python\n\n from pyvaru import Validator\n from pyvaru.rules import TypeRule, FullStringRule, ChoiceRule, PastDateRule\n\n class UserValidator(Validator):\n def get_rules(self) -> list:\n user = self.data # type: User\n return [\n TypeRule(apply_to=user,\n label='User',\n valid_type=User,\n error_message='User must be an instance of user model.',\n stop_if_invalid=True),\n FullStringRule(lambda: user.first_name, 'First name'),\n FullStringRule(lambda: user.last_name, 'Last name'),\n ChoiceRule(lambda: user.sex, 'Sex', choices=('M', 'F')),\n PastDateRule(lambda: user.date_of_birth, 'Date of birth')\n ]\n\n\nIt's also possible to create groups of rules by using ``RuleGroup`` and avoid code duplication if multiple rules should\nbe applied to the same field. So this code:\n\n.. code-block:: python\n\n def get_rules(self) -> list:\n return [\n TypeRule(lambda: self.data.countries, 'Countries', valid_type=list),\n MinLengthRule(lambda: self.data.countries, 'Countries', min_length=1),\n UniqueItemsRule(lambda: self.data.countries, 'Countries')\n ]\n\n\ncan be replaced by:\n\n.. code-block:: python\n\n def get_rules(self) -> list:\n return [\n RuleGroup(lambda: self.data.countries,\n 'Countries',\n rules=[(TypeRule, {'valid_type': list}),\n (MinLengthRule, {'min_length': 1}),\n UniqueItemsRule])\n ]\n\n\nFinally we have two choices regarding how to use our custom validator:\n \n1. As a context processor:\n\n.. code-block:: python\n\n with UserValidator(user):\n # do whatever you want with your valid model\n\nIn this case the code inside ``with`` will be executed only if the validation succeed, otherwise a\n``ValidationException`` (containing a ``validation_result`` property with the appropriate report) is raised.\n \n2. By invoking the ``validate()`` method (which returns a ``ValidationResult``)\n\n.. code-block:: python\n\n validation = UserValidator(user).validate()\n if validation.is_successful():\n # do whatever you want with your valid model\n else:\n # you can take a proper action and access validation.errors\n # in order to provide a useful message to the application user,\n # write logs or whatever\n\n\nAssuming we have a instance of an User configured as the one below:\n\n.. code-block:: python\n\n user = User(first_name=' ',\n last_name=None,\n date_of_birth=datetime(2020, 1, 1),\n sex='unknown')\n\n\nBy running a validation with the previous defined rules we will obtain a ``ValidationResult`` with the following errors:\n\n.. code-block:: python\n\n {\n 'First name': ['String is empty.'],\n 'Last name': ['Not a string.'],\n 'Sex': ['Value not found in available choices.'],\n 'Date of birth': ['Not a past date.']\n }\n\n\nFull API Documentation\n----------------------\n\nGo to: http://pyvaru.readthedocs.io/en/latest/contents.html\n\n\nCredits\n-------\n\nPyvaru is developed and maintained by Davide Zanotti.\n\nBlog: http://www.daveoncode.com\n\nTwitter: https://twitter.com/daveoncode\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/daveoncode/pyvaru", "keywords": "validation rule model data", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "pyvaru", "package_url": "https://pypi.org/project/pyvaru/", "platform": "", "project_url": "https://pypi.org/project/pyvaru/", "project_urls": { "Homepage": "https://github.com/daveoncode/pyvaru" }, "release_url": "https://pypi.org/project/pyvaru/0.3.0/", "requires_dist": null, "requires_python": "", "summary": "Rule based data validation library for python.", "version": "0.3.0" }, "last_serial": 2751282, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "a634f2f3795a5498a6d20fd4eeaf9df6", "sha256": "16d7a374dae653a0580f7223ace8fddee12679ec55838c5697ccaa30d831adc6" }, "downloads": -1, "filename": "pyvaru-0.1.0.tar.gz", "has_sig": false, "md5_digest": "a634f2f3795a5498a6d20fd4eeaf9df6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7651, "upload_time": "2017-02-06T20:42:54", "url": "https://files.pythonhosted.org/packages/cb/26/c74cccc798eacbb9a333e5fa7c45f483e50f94e97b269740de72c8668b34/pyvaru-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "302a9c16ec4fcd69c3f0749f19b2644e", "sha256": "16c98c9ab2f96e1033ac7b0b9792dc92df4928e35f00ed7d592c2ffcf87e42eb" }, "downloads": -1, "filename": "pyvaru-0.1.1.tar.gz", "has_sig": false, "md5_digest": "302a9c16ec4fcd69c3f0749f19b2644e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7911, "upload_time": "2017-02-07T13:31:16", "url": "https://files.pythonhosted.org/packages/43/0e/1d001735fac1e9685edb06b607efa9f9c892904f2be2576308975a9ba239/pyvaru-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "b07daa291bdd8c6a1633ba83a828da8c", "sha256": "b1be439f1f6255529e24d4d69df6f70ae8cc2957e76e42df9b8f40e1022b69a9" }, "downloads": -1, "filename": "pyvaru-0.2.0.tar.gz", "has_sig": false, "md5_digest": "b07daa291bdd8c6a1633ba83a828da8c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8242, "upload_time": "2017-02-16T11:17:36", "url": "https://files.pythonhosted.org/packages/ef/10/40e5cbfd56742604dd20d7ebfda2c4289331fefea0f8e80fef0ba471be6e/pyvaru-0.2.0.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "bd2fc28c8701bb014bdcd4e9c47f127f", "sha256": "9c52464aafdb1437bca74134d206b701e8b9152f7b6de53a9d40d898b21a6637" }, "downloads": -1, "filename": "pyvaru-0.3.0.tar.gz", "has_sig": false, "md5_digest": "bd2fc28c8701bb014bdcd4e9c47f127f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8012, "upload_time": "2017-04-04T07:15:11", "url": "https://files.pythonhosted.org/packages/bc/73/08101145042d74f1e441a294345888012ef0ad3198901a192bc4f1418d58/pyvaru-0.3.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "bd2fc28c8701bb014bdcd4e9c47f127f", "sha256": "9c52464aafdb1437bca74134d206b701e8b9152f7b6de53a9d40d898b21a6637" }, "downloads": -1, "filename": "pyvaru-0.3.0.tar.gz", "has_sig": false, "md5_digest": "bd2fc28c8701bb014bdcd4e9c47f127f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8012, "upload_time": "2017-04-04T07:15:11", "url": "https://files.pythonhosted.org/packages/bc/73/08101145042d74f1e441a294345888012ef0ad3198901a192bc4f1418d58/pyvaru-0.3.0.tar.gz" } ] }