pydantic-validation-formatter


Namepydantic-validation-formatter JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/soamicharan/pydantic-validation-formatter
SummaryThis is a package that allows to customize pydantic built-in validation error messages
upload_time2024-03-05 08:56:20
maintainerSoami Charan
docs_urlNone
authorSoami Charan
requires_python>=3.8,<4.0
licenseApache-2.0
keywords python pydantic sqlmodel
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pydantic Validation Formatter

[![GitHub license badge](https://raw.githubusercontent.com/soamicharan/sqlmodel-plus/main/badges/badge-license.svg)](http://www.apache.org/licenses/LICENSE-2.0)
[![pypi](https://img.shields.io/pypi/v/pydantic-validation-formatter.svg)](https://pypi.org/project/pydantic-validation-formatter/)

## Installation

Install package using pip -> `pip install pydantic-validation-formatter`

## Usage

Use `@customize_validation_message` decorator on pydantic class to apply message templates on specific validation error message.

```python
from pydantic_validation_formatter import customize_validation_message
from pydantic import BaseModel, Field, ValidationError

@customize_validation_message
class Hero(BaseModel):
    id: int = Field(gt=0)
    name: str
    class Config:
        validation_message_template = {
            "id": {
                "greater_than": "id value should be greater than {gt} but received {input}",
                "missing": "id field is required",
            },
        }

try:
    Hero(id=-1, name="hero")
except ValidationError as exc:
    print(exc.errors())
```

This customize the `msg` field of validation error as follows - 
```
[
    {
        'type': 'greater_than',
        'loc': ('id',),
        'msg': 'id value should be greater than 0 but received -1',     # The default generated message will be 'Input should be greater than 0' but it customize the message.
        'input': -1,
        'ctx': {'gt': 0},
        'url': 'https://errors.pydantic.dev/2.6/v/greater_than'
    }
]
```

The validation error message can be templated with following variables
- `input` - The input value in validation error payload
- `field` - The last item in `loc` key value from validation error payload
- `error_type` - The `type` key value from validation error payload

If any other keys found in `ctx` dict, then you can use those values in templated validation error message.

To provide custom validation templated message, you need to define `validation_message_template` attribute in `Config` class. <br>
This should be a dict value which contains field name as keys (same as attribute name defined in pydantic class)
and values should be dict of validation error type and customize templated error message mapping. <br>
To know what kind of error type available, follow the official docs -> https://docs.pydantic.dev/latest/errors/validation_errors/


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/soamicharan/pydantic-validation-formatter",
    "name": "pydantic-validation-formatter",
    "maintainer": "Soami Charan",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "charansoami@gmail.com",
    "keywords": "python,pydantic,sqlmodel",
    "author": "Soami Charan",
    "author_email": "charansoami@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/f1/d5/f3d1a438ee9037dfea51fb54f0079427e5f549e56880b41ce861e348e06a/pydantic_validation_formatter-0.1.2.tar.gz",
    "platform": null,
    "description": "# Pydantic Validation Formatter\n\n[![GitHub license badge](https://raw.githubusercontent.com/soamicharan/sqlmodel-plus/main/badges/badge-license.svg)](http://www.apache.org/licenses/LICENSE-2.0)\n[![pypi](https://img.shields.io/pypi/v/pydantic-validation-formatter.svg)](https://pypi.org/project/pydantic-validation-formatter/)\n\n## Installation\n\nInstall package using pip -> `pip install pydantic-validation-formatter`\n\n## Usage\n\nUse `@customize_validation_message` decorator on pydantic class to apply message templates on specific validation error message.\n\n```python\nfrom pydantic_validation_formatter import customize_validation_message\nfrom pydantic import BaseModel, Field, ValidationError\n\n@customize_validation_message\nclass Hero(BaseModel):\n    id: int = Field(gt=0)\n    name: str\n    class Config:\n        validation_message_template = {\n            \"id\": {\n                \"greater_than\": \"id value should be greater than {gt} but received {input}\",\n                \"missing\": \"id field is required\",\n            },\n        }\n\ntry:\n    Hero(id=-1, name=\"hero\")\nexcept ValidationError as exc:\n    print(exc.errors())\n```\n\nThis customize the `msg` field of validation error as follows - \n```\n[\n    {\n        'type': 'greater_than',\n        'loc': ('id',),\n        'msg': 'id value should be greater than 0 but received -1',     # The default generated message will be 'Input should be greater than 0' but it customize the message.\n        'input': -1,\n        'ctx': {'gt': 0},\n        'url': 'https://errors.pydantic.dev/2.6/v/greater_than'\n    }\n]\n```\n\nThe validation error message can be templated with following variables\n- `input` - The input value in validation error payload\n- `field` - The last item in `loc` key value from validation error payload\n- `error_type` - The `type` key value from validation error payload\n\nIf any other keys found in `ctx` dict, then you can use those values in templated validation error message.\n\nTo provide custom validation templated message, you need to define `validation_message_template` attribute in `Config` class. <br>\nThis should be a dict value which contains field name as keys (same as attribute name defined in pydantic class)\nand values should be dict of validation error type and customize templated error message mapping. <br>\nTo know what kind of error type available, follow the official docs -> https://docs.pydantic.dev/latest/errors/validation_errors/\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "This is a package that allows to customize pydantic built-in validation error messages",
    "version": "0.1.2",
    "project_urls": {
        "Homepage": "https://github.com/soamicharan/pydantic-validation-formatter",
        "Repository": "https://github.com/soamicharan/pydantic-validation-formatter"
    },
    "split_keywords": [
        "python",
        "pydantic",
        "sqlmodel"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f37918b6abd201b360317e03f09e110882973bc69e2fba2af4cfe2ed8c5fd657",
                "md5": "0c2ce497492dfb391d0e4865f88a9a81",
                "sha256": "ec48d589845fa225fa97e5a84d3082b1f207e67ad2db44fc82a2daee2744f880"
            },
            "downloads": -1,
            "filename": "pydantic_validation_formatter-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0c2ce497492dfb391d0e4865f88a9a81",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 6903,
            "upload_time": "2024-03-05T08:56:18",
            "upload_time_iso_8601": "2024-03-05T08:56:18.525912Z",
            "url": "https://files.pythonhosted.org/packages/f3/79/18b6abd201b360317e03f09e110882973bc69e2fba2af4cfe2ed8c5fd657/pydantic_validation_formatter-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f1d5f3d1a438ee9037dfea51fb54f0079427e5f549e56880b41ce861e348e06a",
                "md5": "19928b58506911be1e2bc33ed8a6dfe2",
                "sha256": "eb458aeb05f328feab7f2bda733db5f9ad165b6285bccf53c806d2d5dc4fc828"
            },
            "downloads": -1,
            "filename": "pydantic_validation_formatter-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "19928b58506911be1e2bc33ed8a6dfe2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 6175,
            "upload_time": "2024-03-05T08:56:20",
            "upload_time_iso_8601": "2024-03-05T08:56:20.498156Z",
            "url": "https://files.pythonhosted.org/packages/f1/d5/f3d1a438ee9037dfea51fb54f0079427e5f549e56880b41ce861e348e06a/pydantic_validation_formatter-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-05 08:56:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "soamicharan",
    "github_project": "pydantic-validation-formatter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pydantic-validation-formatter"
}
        
Elapsed time: 3.44561s