{"id":19588387,"url":"https://github.com/insistence/pydantic-validation-decorator","last_synced_at":"2026-03-14T02:05:29.604Z","repository":{"id":247044365,"uuid":"824485239","full_name":"insistence/pydantic-validation-decorator","owner":"insistence","description":"Practical pydantic validation decorators that support manual invocation. 支持手动调用的实用的pydantic验证装饰器。","archived":false,"fork":false,"pushed_at":"2024-09-03T15:45:09.000Z","size":30,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-03T16:23:06.209Z","etag":null,"topics":["decorators","pydantic","pydantic-models","pydantic-v2","pydantic-validation","validation-decorator","validations"],"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/insistence.png","metadata":{"files":{"readme":"README-en_US.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-07-05T08:30:27.000Z","updated_at":"2024-09-03T15:46:50.000Z","dependencies_parsed_at":"2024-07-06T09:17:12.272Z","dependency_job_id":"cdd023a8-dc0d-4411-bcd5-56157a3ef453","html_url":"https://github.com/insistence/pydantic-validation-decorator","commit_stats":null,"previous_names":["insistence/pydantic-validation-decorator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/insistence%2Fpydantic-validation-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/insistence%2Fpydantic-validation-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/insistence%2Fpydantic-validation-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/insistence%2Fpydantic-validation-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/insistence","download_url":"https://codeload.github.com/insistence/pydantic-validation-decorator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224069407,"owners_count":17250456,"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":["decorators","pydantic","pydantic-models","pydantic-v2","pydantic-validation","validation-decorator","validations"],"created_at":"2024-11-11T08:13:38.007Z","updated_at":"2026-03-14T02:05:29.598Z","avatar_url":"https://github.com/insistence.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003epydantic-validation-decorator\u003c/h1\u003e\n\u003ch3 align=\"center\"\u003ePractical pydantic validation decorators that support manual invocation\u003c/h3\u003e\n\u003cdiv align=\"center\"\u003e\n\n[![GitHub](https://shields.io/badge/license-MIT-informational)](https://github.com/insistence/pydantic-validation-decorator/blob/master/LICENSE)\n[![PyPI](https://img.shields.io/pypi/v/pydantic-validation-decorator.svg?color=dark-green)](https://pypi.org/project/pydantic-validation-decorator/)\n\n\u003c/div\u003e\n\nEnglish | [简体中文](./README.md)\n\n## Directory\n[Install](#install)\u003cbr\u003e\n[Get Started](#get-started)\u003cbr\u003e\n[List of Existing Decorators](#decorators-list)\u003cbr\u003e\n[Contribute](#contribute)\n\n\u003ca name=\"install\" \u003e\u003c/a\u003e\n\n## Install\n```bash\npip install pydantic-validation-decorator -U\n```\n\n\u003ca name=\"get-started\" \u003e\u003c/a\u003e\n\n## Get Started\n1.Create a `Pydantic` Model.\n```python\nfrom pydantic import BaseModel\nfrom typing import Optional\n\n\nclass NotBlankTestModel(BaseModel):\n    user_name: Optional[str] = None\n```\n2.Introducing a validation decorator into the `Pydantic` model, using the `@NotBlank` decorator as an example.\n```python\nfrom pydantic import BaseModel\nfrom typing import Optional\nfrom pydantic_validation_decorator import NotBlank\n\n\nclass NotBlankTestModel(BaseModel):\n    user_name: Optional[str] = None\n\n    @NotBlank(\n        field_name='user_name',\n        message='user_name cannot be blank',\n    )\n    def get_user_name(self):\n        return self.user_name\n\n    def validate_fields(self):\n        self.get_user_name()\n```\n3.Use the `@ValidateFields` validation decorator in functions that require manual triggering of validation.\n```python\nfrom pydantic_validation_decorator import ValidateFields\n\n\n@ValidateFields(validate_model='not_blank_test', validate_function='validate_fields')\ndef test_not_blank_decorator(not_blank_test: NotBlankTestModel):\n    return not_blank_test.model_dump()\n```\n4.Calling this function triggers validation. When the validation fails, a  `FieldValidationError` exception will be thrown. The exception object contains a `message` attribute with a value of the `message` attribute set in the `@NotBlank` decorator.\n```python\nfrom pydantic_validation_decorator import FieldValidationError\n\n\nif __name__ == '__main__':\n    not_blank_test = NotBlankTestModel()\n    try:\n        print(test_not_blank_decorator(not_blank_test=not_blank_test))\n    except FieldValidationError as e:\n        print(e.__dict__)\n```\nThe final output result of calling this function is：\n```python\n{'model_name': 'NotBlankTestModel', 'field_name': 'user_name', 'field_value': None, 'validator': 'NotBlank', 'message': 'user_name cannot be blank'}\n```\nThe complete code example is：\n```python\nfrom pydantic_validation_decorator import (\n    ValidateFields,\n    NotBlank,\n    FieldValidationError,\n)\nfrom pydantic import BaseModel\nfrom typing import Optional\n\n\nclass NotBlankTestModel(BaseModel):\n    user_name: Optional[str] = None\n\n    @NotBlank(\n        field_name='user_name',\n        message='user_name cannot be blank',\n    )\n    def get_user_name(self):\n        return self.user_name\n\n    def validate_fields(self):\n        self.get_user_name()\n\n\n@ValidateFields(validate_model='not_blank_test', validate_function='validate_fields')\ndef test_not_blank_decorator(not_blank_test: NotBlankTestModel):\n    return not_blank_test.model_dump()\n\n\nif __name__ == '__main__':\n    not_blank_test = NotBlankTestModel()\n    try:\n        print(test_not_blank_decorator(not_blank_test=not_blank_test))\n    except FieldValidationError as e:\n        print(e.__dict__)\n```\n\n\u003ca name=\"decorators-list\" \u003e\u003c/a\u003e\n\n## List of Existing Decorators\n\n### `@ValidateFields` Field Validation Decorator\n| Parameter | Type | Parameter Description | Default Value |\n| - | - | - | - |\n| `mode` | str, optional | How to obtain the model that needs to be validate. Optional options include 'args' (obtained from positional parameters) and' kwargs' (obtained from keyword parameters) | 'kwargs' |\n| `validate_model` | str, optional | The name of the `Pydantic` model that needs to be validated in the function.(obtained from keyword parameters) | - |\n| `validate_model_index` | int, optional | The index of the `Pydantic` model that needs to be validated in the function.(obtained from positional parameters) | - |\n| `validate_function` | str, optional | The name of the validation function defined in the `Pydantic` model. | 'validate_fields' |\n\n### `@Alpha`    Field Alpha Validation Decorator\n| Parameter | Type | Parameter Description | Default Value |\n| - | - | - | - |\n| `field_name` | str | Field name that need to be validate. | - |\n| `mode` | Literal['upper', 'lower', 'mixed'], optional | Validation mode. Options: 'upper' (only uppercase), 'lower' (only lowercase), 'mixed' (both upper and lower). | 'mixed' |\n| `message` | str, optional | Prompt message for validation failure. Defaults to None. | `'{field_name} must contain only letters.'` OR `'{field_name} must contain only uppercase letters.'` OR `'{field_name} must contain only lowercase letters.'` |\n\n### `@Network`    Field Network Type Validation Decorator \n| Parameter | Type | Parameter Description | Default Value |\n| - | - | - | - |\n| `field_name` | str | Field name that need to be validate. | - |\n| `field_type` | str | Field type that need to be validate. Optional options include 'AnyUrl', 'AnyHttpUrl', 'HttpUrl', 'AnyWebsocketUrl', 'WebsocketUrl', 'FileUrl', 'FtpUrl', 'PostgresDsn', 'CockroachDsn', 'AmqpDsn', 'RedisDsn', 'MongoDsn', 'KafkaDsn', 'NatsDsn', 'MySQLDsn', 'MariaDBDsn', 'ClickHouseDsn', 'EmailStr', 'NameEmail', 'IPvAnyAddress', | - |\n| `message` | str, optional | Prompt message for validation failure. Defaults to None. | `'{field_name} is not the correct {field_type} type.'` |\n\n### `@NotBlank`   Field NotBlank Validation Decorator\n| Parameter | Type | Parameter Description | Default Value |\n| - | - | - | - |\n| `field_name` | str | Field name that need to be validate. | - |\n| `allow_unset` | bool, optional | If True, validation only runs when the optional field is explicitly provided. | False |\n| `message` | str, optional | Prompt message for validation failure. Defaults to None. | `'{field_name} cannot be empty.'` |\n\n### `@Pattern`    Field Pattern Validation Decorator\n| Parameter | Type | Parameter Description | Default Value |\n| - | - | - | - |\n| `field_name` | str | Field name that need to be validate. | - |\n| `regexp` | str | Regular expression. | - |\n| `message` | str, optional | Prompt message for validation failure. Defaults to None. | `'The format of {field_name} is incorrect.'` |\n\n### `@Size`   Field Size Validation Decorator\n| Parameter | Type | Parameter Description | Default Value |\n| - | - | - | - |\n| `field_name` | str | Field name that need to be validate. | - |\n| `gt` | float or int, optional | The numerical field value must be greater than gt. | - |\n| `ge` | float or int, optional | The numerical field value must be greater than or equal to ge. | - |\n| `lt` | float or int, optional | The numerical field value must be less than lt. | - |\n| `le` | float or int, optional | The numerical field value must be less than or equal to le. | - |\n| `min_length` | int, optional | The length of a string field cannot be less than min_length. | 0 |\n| `max_length` | int, optional | The length of a string field cannot be greater than max_length. | - |\n| `message` | str, optional | Prompt message for validation failure. Defaults to None. | `'{field_name} must be greater than {gt}.'` OR `'{field_name} must be greater than or equal to {ge}.'` OR `'{field_name} must be less than {lt}.'` OR `'{field_name} must be less than or equal to {le}.'` OR `'The length of {field_name} cannot be less than {min_length}.'` OR `'The length of {field_name} cannot be greater than {max_length}.'` |\n\n### `@Xss`    Field Xss Validation Decorator\n| Parameter | Type | Parameter Description | Default Value |\n| - | - | - | - |\n| `field_name` | str | Field name that need to be validate. | - |\n| `message` | str, optional | Prompt message for validation failure. Defaults to None. | `'{field_name} cannot contain script characters.'` |\n\n\u003ca name=\"contribute\" \u003e\u003c/a\u003e\n\n## Contribute\n```bash\ngit clone https://github.com/insistence/pydantic-validation-decorator.git\ncd pydantic-validation-decorator\n# Install dependencies required for development environment\npip install -r requirements-dev.txt\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finsistence%2Fpydantic-validation-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finsistence%2Fpydantic-validation-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finsistence%2Fpydantic-validation-decorator/lists"}