{"id":23718541,"url":"https://github.com/hvu-dev/pyvalidator","last_synced_at":"2025-07-12T02:04:13.778Z","repository":{"id":245069563,"uuid":"766532600","full_name":"hvu-dev/pyvalidator","owner":"hvu-dev","description":"Simple type validator with Python","archived":false,"fork":false,"pushed_at":"2024-06-19T05:44:34.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2024-12-30T21:23:18.173Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hvu-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-03T14:29:31.000Z","updated_at":"2024-06-19T05:56:09.000Z","dependencies_parsed_at":"2024-06-19T14:40:24.171Z","dependency_job_id":"26f97696-2fbb-469a-ade7-cb48b0584cfa","html_url":"https://github.com/hvu-dev/pyvalidator","commit_stats":null,"previous_names":["huyvq18411c/pyvalidator","hvu-dev/pyvalidator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hvu-dev%2Fpyvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hvu-dev%2Fpyvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hvu-dev%2Fpyvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hvu-dev%2Fpyvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hvu-dev","download_url":"https://codeload.github.com/hvu-dev/pyvalidator/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239808462,"owners_count":19700442,"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":[],"created_at":"2024-12-30T21:23:21.276Z","updated_at":"2025-02-20T08:44:58.984Z","avatar_url":"https://github.com/hvu-dev.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyValidator - Simple Field Validator Using Python\nThis is an unfinished package, further updates may change its functionalities.\n## The idea\nUsing [Descriptor](https://docs.python.org/3/howto/descriptor.html) we can understand not only one of the most essential concept of Python but also many validation libraries.\nWithin this repo, I try to avoid external libraries as much as I can. Currently, the only library I'm using is [dateutil](https://github.com/dateutil/dateutil), for datetime conversion. \n\nUsing 3 methods (`__set_name__`, `__get__`, `__set__`) of a `Descriptor` we can perform many actions on value of a field/object including validation, please read the article I mentioned above for more details.\n## Installation\n```\npip install git+ssh://git@github.com:HuyVQ18411c/pyvalidator.git\n```\n\n## Usage\nThe targets of this package are pure Python model and with `Form`, which is also provided within this package. \n### Field\nExample with `Field` class:\n```\nfrom pyvalidator.core.validators import IntField\n\nclass Person:\n    age = IntField(min_value=18)\n    \n    def __init__(self, age):\n        self.age = age\n\np = Person(2) # raise ValueError since min_value was set to 18 for `age` field\np = Person(19) # No complaint!\n```\n#### Type conversion\nSince we are using descriptor, hence an exception is raised on assignment. As soon as you assign a value to the field, it will raise error any.\n\nIf you are uncertain about the data type you can either set `force_conversion=True` or set your own conversion function using `custom_conversion` on initialization or through `set_custom_conversion`.\n\nAs the result, the value will be converted to target type. For example, if you pass a numeric `str` to `IntField` it will be transformed to an `int` the next time you retrieve it.  \n#### Custom field\nUser defined Field is possible, simply by inheriting `Field`, `NumericField`, `StringField` class from `pyvalidator.core.validators`.\n\nIf you decided to use `Field` class, you must implement the following methods in order to make a field work properly:\n\n- `_built_in_validation`: any validations for that field such as: min value, max value, min_length, etc. \n\n### Form\nExample with `Form` class:\n```\nfrom pyvalidator.core.forms import Form\nfrom pyvalidator.core.exceptions import ValidationError\n\nclass AudienceForm(Form):\n    name = StringField(max_length=20)\n    age = IntField(min_value=18)\n    last_login = DateTimeField(nullable=True, force_conversion=True)\n\n    def clean_name(self, value: str):\n        if not value.startswith('A'):\n            raise ValidationError('Invalid name for audience')\n        return value\n\n    def clean(self):\n        print(self.last_login, self.age)\n        if (datetime.now().year - self.last_login.year) \u003e self.age:\n            raise ValidationError('Invalid last login year')\n\n```\nIf you have used Django or Django REST Framework before, this piece of code will be fairly familiar.\n#### Form clean cycle:\nForm is designed to work in the following order, when `is_valid` is called:\n- Data is set to matched field\n- Trigger field validation\n- Run user-defined clean_\u003cfield\u003e\n- Set value (again) to field\n- Trigger field validation\n- Run user-defined clean for form\n\n#### User-defined clean\n- Field level: if you want to have an additional clean for specific field, you can define a method called `clean_\u003cfield_name\u003e`. For example if you have a field name `age` then the custom clean function for it should be named `clean_age`.\n- Form level: if you want to have an additional clean for the whole form, using multiple fields by retrieving from `form.cleaned_data`, you can declare a method call `clean`. \n\nSee more in the example above.\n## Author \u0026 contributor:\nVũ Quang Huy (Huy Vũ)\n- **Email**: vuquanghuy2k@gmail.com\n- [Github](https://github.com/HuyVQ18411c)\n- [LinkedIn](https://www.linkedin.com/in/huy-vu-dev/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhvu-dev%2Fpyvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhvu-dev%2Fpyvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhvu-dev%2Fpyvalidator/lists"}