{"id":23554167,"url":"https://github.com/silverbackhq/pyvalitron","last_synced_at":"2025-11-01T14:30:30.422Z","repository":{"id":57458282,"uuid":"52166533","full_name":"silverbackhq/pyvalitron","owner":"silverbackhq","description":"🔰 Python Inputs Validation Library.","archived":false,"fork":false,"pushed_at":"2019-07-18T22:48:12.000Z","size":71,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T11:01:52.190Z","etag":null,"topics":["escape","form","pyhton","sanitize-values","sanitizer","security","silverbackhq","statuspage","validation","xss"],"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/silverbackhq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-20T17:45:35.000Z","updated_at":"2021-03-19T15:24:45.000Z","dependencies_parsed_at":"2022-09-09T22:50:17.131Z","dependency_job_id":null,"html_url":"https://github.com/silverbackhq/pyvalitron","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/silverbackhq%2Fpyvalitron","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silverbackhq%2Fpyvalitron/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silverbackhq%2Fpyvalitron/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silverbackhq%2Fpyvalitron/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/silverbackhq","download_url":"https://codeload.github.com/silverbackhq/pyvalitron/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239294149,"owners_count":19615081,"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":["escape","form","pyhton","sanitize-values","sanitizer","security","silverbackhq","statuspage","validation","xss"],"created_at":"2024-12-26T12:12:22.912Z","updated_at":"2025-11-01T14:30:30.353Z","avatar_url":"https://github.com/silverbackhq.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"PyValitron\n==========\n\nPyValitron is a light-weight python inputs validation library.\n\n[![Build Status](https://travis-ci.org/silverbackhq/pyvalitron.svg?branch=master)](https://travis-ci.org/silverbackhq/pyvalitron)\n[![PyPI version](https://badge.fury.io/py/pyvalitron.svg)](https://badge.fury.io/py/pyvalitron)\n[![Coverage Status](https://coveralls.io/repos/github/silverbackhq/pyvalitron/badge.svg)](https://coveralls.io/github/silverbackhq/pyvalitron)\n\nInstallation\n------------\nTo install PyValitron run this command:\n```\npip3 install pyvalitron\n```\n\nUsage\n-----\nAfter installing the library, Read the following usage criteria:\n\n#### Validate Values:\n\nTo validate a list of values:\n```python\nfrom pyvalitron.form import Form\n\nform = Form({\n    'test_field1': {\n        'value': 'Hello World',\n        'validate': {\n            'length_between': {\n                'param': [1, 12],\n                'error': 'Input lenght must be between 1 and 12 characters'\n            }\n        }\n    },\n    'test_field2': {\n        'value': 'Hello World',\n        'validate': {\n            'length_between': {\n                'param': [1, 9],\n                'error': 'Input lenght must be between 1 and 9 characters'\n            }\n        }\n    }\n})\nform.process()\nerrors = form.get_errors()\nprint(errors['test_field2'])   # Input lenght must be between 1 and 9 characters\n```\n\n#### Sanitize Values:\n\nTo sanitize a list of values:\n```python\nfrom __future__ import print_function\nfrom pyvalitron.form import Form\n\n\nform = Form({\n    'test_field': {\n        'value': 'Hello\u0026 W\"or\"ld\u003cbr\u003e.',\n        'sanitize': {\n            'escape': {}\n        }\n    }\n})\nform.process()\ninputs = form.get_inputs()\nprint(inputs['test_field']) # {'is_exact': False, 'svalue': 'Hello\u0026amp; W\u0026quot;or\u0026quot;ld\u0026lt;br\u0026gt;.', 'sanitize': {'escape': {}}, 'value': 'Hello\u0026 W\"or\"ld\u003cbr\u003e.'}\nprint(inputs['test_field']['is_exact']) # False\nprint(inputs['test_field']['svalue']) # Hello\u0026amp; W\u0026quot;or\u0026quot;ld\u0026lt;br\u0026gt;.\nprint(inputs['test_field']['value']) # Hello\u0026 W\"or\"ld\u003cbr\u003e.\n```\n```python\nfrom __future__ import print_function\nfrom pyvalitron.form import Form\n\n\nform = Form({\n    'test_field': {\n        'value': 'Hello World.',\n        'sanitize': {\n            'escape': {}\n        }\n    }\n})\nform.process()\ninputs = form.get_inputs()\nprint(inputs['test_field']) # {'is_exact': True, 'svalue': 'Hello World.', 'sanitize': {'escape': {}}, 'value': 'Hello World.'}\nprint(inputs['test_field']['is_exact']) # True\nprint(inputs['test_field']['svalue']) # Hello World.\nprint(inputs['test_field']['value']) # Hello World.\n```\n\n#### Validate \u0026 Sanitize Values:\n\nTo validate and sanitize a list of values:\n```python\nfrom __future__ import print_function\nfrom pyvalitron.form import Form\n\n\nform = Form({\n    'test_field': {\n        'value': 'hello@clivern.com',\n        'sanitize': {\n            'escape': {}\n        },\n        'validate': {\n            'email': {\n                'error': 'Please provide a valid email.'\n            }\n        }\n    }\n})\nform.process()\ninputs = form.get_inputs()\nerrors = form.get_errors()\nprint(errors) # {'test_field': []}\nprint(errors['test_field']) # []\nprint(inputs['test_field']) # {'status': True, 'is_exact': True, 'value': 'hello@clivern.com', 'sanitize': {'escape': {}}, 'svalue': 'hello@clivern.com', 'validate': {'email': {'error': 'Please provide a valid email.'}}}\nprint(inputs['test_field']['status']) # True\nprint(inputs['test_field']['is_exact']) # True\nprint(inputs['test_field']['value']) # hello@clivern.com\nprint(inputs['test_field']['svalue']) # hello@clivern.com\n```\n```python\nfrom __future__ import print_function\nfrom pyvalitron.form import Form\n\n\nform = Form({\n    'test_field': {\n        'value': 'hello@cliv@ern.com',\n        'sanitize': {\n            'escape': {}\n        },\n        'validate': {\n            'email': {\n                'error': 'Please provide a valid email.'\n            }\n        }\n    }\n})\nform.process()\ninputs = form.get_inputs()\nerrors = form.get_errors()\nprint(errors) # {'test_field': ['Please provide a valid email.']}\nprint(errors['test_field']) # ['Please provide a valid email.']\nprint(inputs['test_field']) # {'status': False, 'is_exact': True, 'value': 'hello@cliv@ern.com', 'sanitize': {'escape': {}}, 'svalue': 'hello@cliv@ern.com', 'validate': {'email': {'error': 'Please provide a valid email.'}}}\nprint(inputs['test_field']['status']) # False\nprint(inputs['test_field']['is_exact']) # True\nprint(inputs['test_field']['value']) # hello@cliv@ern.com\nprint(inputs['test_field']['svalue']) # hello@cliv@ern.com\n```\n\n#### Using With Frameworks:\n\nFlask Framework\n```python\nfrom flask import Flask\nfrom flask import request\nfrom pyvalitron.form import Form\n\n\napp = Flask(__name__)\n\n@app.route(\"/\")\ndef hello():\n    form = Form({\n        'test_field1': {\n            'value': request.args.get('test_field1'),\n            'validate': {\n                'length_between': {\n                    'param': [1, 12],\n                    'error': 'Input lenght must be between 1 and 12 characters'\n                }\n            }\n        },\n        'test_field2': {\n            'value': request.args.get('test_field2'),\n            'validate': {\n                'length_between': {\n                    'param': [1, 9],\n                    'error': 'Input lenght must be between 1 and 9 characters'\n                }\n            }\n        }\n    }, 'values')\n    form.process()\n    errors = form.get_errors()\n    if 'Input lenght must be between 1 and 9 characters' in errors['test_field2']:\n        return 'error'\n    else:\n        return 'success'\n\nif __name__ == \"__main__\":\n    app.run(debug=True)\n```\n\n#### Validators List:\n\nHere is a list of all available validators:\n\n* `empty`: Validate if input or a value is empty.\n* `not_empty`: Validate if input or a value is not empty.\n* `length_between`: Validate if input or a value length is between provided lengths. It requires two parameters `[from_length, to_length]` like `[1, 13]`.\n* `min_length`: Validate if input or a value min lenght is like provided one. It requires one parameter `[min_length]` like `[1]`.\n* `max_length`: Validate if input or a value max lenght is like provided one. It requires one parameter `[max_length]` like `[12]`.\n* `exact_length`: Validate if input or a value lenght is equal to provided one. It requires one parameter `[exact_length]` like `[9]`.\n* `greater_than`: Validate if input or a value is greater than provided one. It requires one parameter `[number]` like `[5]`.\n* `greater_than_equal`: Validate if input or a value is greater than or equal provided one. It requires one parameter `[number]` like `[4]`.\n* `less_than`: Validate if input or a value is less than provided one. It requires one parameter `[number]` like `[5]`.\n* `less_than_equal`: Validate if input or a value is less than or equal provided one. It requires one parameter `[number]` like `[5]`.\n* `equal`: Validate if input or a value is equal to provided one. It requires one parameter `[number]` like `[5]`.\n* `same_as`: Validate if input or a value is same as provided one. It requires one parameter `[text]` like `['Hello World']`\n* `any_of`: Validate if input or a value is any of the provided list. It requires one parameter `[[options]]` like `[1,5,'text']`.\n* `all_of`: Validate if input or a value is all of the provided list. It requires one parameter `[[options]]` like `[1,5,'text']`.\n* `none_of`: Validate if input or a value is none of the provided list. It requires one parameter `[[options]]` like `[1,5,'text']`.\n* `alpha`: Validate if input or a value is alphabetical.\n* `alpha_numeric`: Validate if input or a value is alphanumeric.\n* `digit`: Validate if input or a value is digits.\n* `email`: Validate if input or a value is email.\n* `emails`: Validate if input or a value is a list of emails. It requires one parameter `[separator]` like `[',']`.\n* `url`: Validate if input or a value is a URL. It requires one parameter (a list of protocols) `[[protocols]]` like `[['http', 'https']]`.\n* `ip`: Validate if input or a value is IP. It requires one parameter (a list of formats) `[[formats]]` like `['ipv4']`\n* `ipv4`: Validate if input or a value is IPv4.\n* `uuid`: Validate if input or a value is universally unique identifier (UUID)\n* `matches`: Validate if input or a value matches provided regex. It requires one parameter `[regex]` like `[r'^[_a-z0-9-]+$']`.\n\n\n#### Sanitizers List\n\nHere is a list of all available sanitizers:\n\n* `strip`: Strip the input value. It accepts one parameter `[[chars]]` like `[[',', '.', '\\s']]`.\n* `lstrip`: Left strip the input value. It accepts one parameter `[[chars]]` like `[[',', '.', '\\s']]`.\n* `rstrip`: Right strip the input value. It accepts one parameter `[[chars]]` like `[[',', '.', '\\s']]`.\n* `escape`: Escape the input value to prevent evil scripts. It accepts one parameter (a list of chars to escape). currently it support these characters `['\u0026', '\"', '\\'', '\u003e', '\u003c']`.\n\n\n#### Custom Validators\n\nTo define a new validator:\n```python\nfrom pyvalitron.validator import Validator\nfrom pyvalitron.form import Form\n\n\nclass MyValidator(Validator):\n\n    def username(self):\n        if not isinstance(self._input, (str)):\n            return False\n        current_input = self._input.strip()\n        if len(current_input) \u003e 5 and current_input.isalpha():\n            return True\n        return False\n\n    def otherrule(self):\n        return True\n\n    #...and so one\n\n\nform = Form()\nform.add_validator(MyValidator())\nform.add_inputs({\n    'user_name': {\n        'value': '',\n        'validate': {\n            'username': {\n                'error': 'Invalid Username'\n            }\n        }\n    }\n})\nform.process()\nerrors = form.get_errors() #{'user_name': ['Invalid Username']}\n```\n\n#### Custom Sanitizers\n\nTo define a new sanitizer:\n```python\nfrom __future__ import print_function\nfrom pyvalitron.sanitizer import Sanitizer\nfrom pyvalitron.form import Form\n\nclass MySanitizer(Sanitizer):\n\n    def clear_spaces(self):\n        if not isinstance(self._input, (str)):\n            self._sinput = str(self._input)\n        else:\n            self._sinput = self._input\n\n        self._sinput = self._sinput.replace(\" \", \"\")\n        return self._sinput\n\n    def lower_case(self):\n        if not isinstance(self._input, (str)):\n            self._sinput = str(self._input)\n        else:\n            self._sinput = self._input\n        self._sinput = self._sinput.lower()\n        return self._sinput\n\nform = Form({\n    'test_field': {\n        'value': 'Hello World',\n        'sanitize': {\n            'clear_spaces':{},\n            'lower_case': {}\n        }\n    }\n}, 'values')\nform.add_sanitizer(MySanitizer())\nform.process()\ninputs = form.get_inputs()\nprint(inputs['test_field']['svalue']) #helloworld\n```\n\nMisc\n====\n\nChangelog\n----------\nVersion 1.1.3:\n```\nFix issue with validation extensions.\n```\n\nVersion 1.1.2:\n```\nFix import issues.\n```\n\nVersion 1.1.1:\n```\nNew Validation Rules.\nAdd python lint check.\n```\n\nVersion 1.0.0:\n```\nInitial Release.\n```\n\nAcknowledgements\n----------------\n\n© 2019, Silverback. Released under [MIT License](https://opensource.org/licenses/mit-license.php).\n\n**PyValitron** is authored and maintained by [@silverbackhq](http://github.com/silverbackhq).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilverbackhq%2Fpyvalitron","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsilverbackhq%2Fpyvalitron","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilverbackhq%2Fpyvalitron/lists"}