{"id":13709402,"url":"https://github.com/daveoncode/pyvaru","last_synced_at":"2025-05-06T16:31:34.500Z","repository":{"id":57458312,"uuid":"80839383","full_name":"daveoncode/pyvaru","owner":"daveoncode","description":"Rule based data validation library for python 3.","archived":false,"fork":false,"pushed_at":"2017-04-04T07:16:21.000Z","size":2023,"stargazers_count":20,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T14:53:42.870Z","etag":null,"topics":["data","data-validation","form-validation","model","validation","validator"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daveoncode.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-03T15:04:28.000Z","updated_at":"2023-06-23T19:49:49.000Z","dependencies_parsed_at":"2022-09-09T23:02:46.999Z","dependency_job_id":null,"html_url":"https://github.com/daveoncode/pyvaru","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daveoncode%2Fpyvaru","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daveoncode%2Fpyvaru/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daveoncode%2Fpyvaru/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daveoncode%2Fpyvaru/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daveoncode","download_url":"https://codeload.github.com/daveoncode/pyvaru/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252721078,"owners_count":21793748,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data","data-validation","form-validation","model","validation","validator"],"created_at":"2024-08-02T23:00:38.872Z","updated_at":"2025-05-06T16:31:33.531Z","avatar_url":"https://github.com/daveoncode.png","language":"Python","readme":".. 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 \u003e= x) *\n- ``MaxValueRule`` (it checks that the target value is \u003c= x) *\n- ``MinLengthRule`` (it checks that the target value length is \u003e= x) *\n- ``MaxLengthRule`` (it checks that the target value length is \u003c= 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) -\u003e 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) -\u003e 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) -\u003e 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) -\u003e 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","funding_links":[],"categories":["Libraries in Python","Data validation"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaveoncode%2Fpyvaru","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaveoncode%2Fpyvaru","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaveoncode%2Fpyvaru/lists"}