{ "info": { "author": "Sean Parsons", "author_email": "seanpatrick2013@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.6" ], "description": "Validator\n=========\n|PyPI| |PyPI version|\n\nA simple way to validate dictionary values by using functions.\n\n\nInstallation\n------------\n- ``pip3 install simple-validator``\n\n\nUsage\n-----\nThere are 2 main classes to create custom validation classes(``Field``, ``Validator``).\n\n.. code:: python\n\n from validator import Field, Validator\n\n\n def is_valid_email(val: str):\n \"\"\" A horrible way to check if a string is a valid email. \"\"\"\n passed = False\n err_msg = \"{} isn't a valid email.\".format(val)\n\n if '@' in val:\n passed = True\n return passed, err_msg\n\n\n class UserValidator(Validator):\n \"\"\" Validates a user dictionary. \"\"\"\n email = Field(data_type=str, validators=[is_valid_email])\n\n\nValidation\n----------\nThe Validator provides the same api as Django forms for checking if all the fields\nare valid.\n\n.. code:: python\n\n data = {\n 'email': 'Sean@parsons.com'\n }\n\n user = UserValidator(data)\n\n if user.is_valid():\n # Do things in here...\n email = user.email # Optionally you can do user.data['email']\n else:\n print(user.errors)\n\nRequired Fields\n---------------\nTo make a ``Field`` required, all that needs to be done is add ``required=True`` as a kwarg\nto the definition\n\nIf the field isn't present it will be added to ```Validator.errors`` under the key for the declared Field\nthat is set to required.\n\n.. code:: python\n\n class UserValidator(Validator):\n \"\"\" Validates a user dictionary. \"\"\"\n\n email = Field(\n data_type=str,\n validators=[is_valid_email],\n required=True\n )\n\nErrors\n------\nThe ``Validator.errors`` attribute is a ``defaultdict(list)``.\n\nWhen validators don't pass, the declared field(Ex: 'email', 'password' etc..) errors\ngets populated with the return error string from the validator or required errors if the data is missing.\n\n.. code:: python\n\n data = {\n 'email': 'sean'\n }\n\n user = UserValidator(data)\n\n if user.is_valid():\n # Do things in here...\n else:\n print(user.errors['email'])\n\n # \"sean isn't a valid email.\"\n\n\nValidating Field Types\n----------------------\nThe ``Field`` class has a ``data_type`` parameter which should be used to validate a field value\nbefore passing it into validators.\n\nThis prevents from having ``try, except, else`` blocks inside of validator functions because your guaranteed\nit won't be passed into validators until it's the correct type.\n\n**If the field value is the wrong type, it will ony return an error like the one below**\n\n\n.. code:: python\n\n data = {\n 'email': 1\n }\n\n user = UserValidator(data)\n\n if user.is_valid():\n # Do things in here...\n else:\n print(user.errors['email'])\n\n # \"'1' is expected to be a 'String'\"\n\n\n.. |PyPI| image:: https://img.shields.io/pypi/v/simple-validator.svg\n :target: https://pypi.python.org/pypi/simple-validator/\n\n.. |PyPI version| image:: https://img.shields.io/pypi/pyversions/simple-validator.svg\n :target: https://pypi.python.org/pypi/simple-validator/\n", "description_content_type": null, "docs_url": null, "download_url": "https://github.com/seanpar203/validator/archive/1.0.3.tar.gz", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/seanpar203/validator", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "simple-validator", "package_url": "https://pypi.org/project/simple-validator/", "platform": "any", "project_url": "https://pypi.org/project/simple-validator/", "project_urls": { "Download": "https://github.com/seanpar203/validator/archive/1.0.3.tar.gz", "Homepage": "https://github.com/seanpar203/validator" }, "release_url": "https://pypi.org/project/simple-validator/1.0.3/", "requires_dist": null, "requires_python": "", "summary": "A simple way to validate dictionary values by using functions.", "version": "1.0.3" }, "last_serial": 3037209, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "e16407cd7ce016ff1693643dd23ed250", "sha256": "276237e4fb691b97da6dba522e8796c0cf52ce8007cc0a55fc8c3916a119c064" }, "downloads": -1, "filename": "simple-validator-1.0.0.tar.gz", "has_sig": false, "md5_digest": "e16407cd7ce016ff1693643dd23ed250", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 1890, "upload_time": "2017-07-19T18:25:46", "url": "https://files.pythonhosted.org/packages/72/82/f3566a3f5e6086e826658b42c6f9650a2e06a95e1b46a764e05616ee73d6/simple-validator-1.0.0.tar.gz" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "33145fa2150c956f304281bd503136f2", "sha256": "9d562024e63e451e9422e66b2c1d885a5f9d1cca0694395e15cfca43720ea2d9" }, "downloads": -1, "filename": "simple-validator-1.0.2.tar.gz", "has_sig": false, "md5_digest": "33145fa2150c956f304281bd503136f2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3156, "upload_time": "2017-07-20T13:59:52", "url": "https://files.pythonhosted.org/packages/b1/49/97639675b764f6208d04cf1fa46d644780ed2ed1b8220caf29dcd42eed4a/simple-validator-1.0.2.tar.gz" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "e9736109714d094789f1a54e3d5856b9", "sha256": "d51a4595bc9ca46b497f6149c1223e7613d5400275d6df68d65ff5d3bfbe9482" }, "downloads": -1, "filename": "simple-validator-1.0.3.tar.gz", "has_sig": false, "md5_digest": "e9736109714d094789f1a54e3d5856b9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3693, "upload_time": "2017-07-20T15:39:06", "url": "https://files.pythonhosted.org/packages/ea/50/d074164786c16428058e9fac08742920cca9059d9c80b304f3779a73ad08/simple-validator-1.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e9736109714d094789f1a54e3d5856b9", "sha256": "d51a4595bc9ca46b497f6149c1223e7613d5400275d6df68d65ff5d3bfbe9482" }, "downloads": -1, "filename": "simple-validator-1.0.3.tar.gz", "has_sig": false, "md5_digest": "e9736109714d094789f1a54e3d5856b9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3693, "upload_time": "2017-07-20T15:39:06", "url": "https://files.pythonhosted.org/packages/ea/50/d074164786c16428058e9fac08742920cca9059d9c80b304f3779a73ad08/simple-validator-1.0.3.tar.gz" } ] }