Pydantic Validation Formatter
Installation
Install package using pip -> pip install pydantic-validation-formatter
Usage
Simple Usage
Use @customize_validation_message decorator on pydantic class to apply message templates on specific validation error message.
from pydantic_validation_formatter import customize_validation_message
from pydantic import BaseModel, Field, ValidationError, ConfigDict
@customize_validation_message
class Hero(BaseModel):
id: int = Field(gt=0, lt=5)
name: str
model_config = ConfigDict(
validation_message_template = {
"id": {
"greater_than": "id value should be greater than {gt} but received {input}",
"missing": "id field is required",
},
}
)
try:
Hero(id=10, 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 payloadfield- The last item inlockey value from validation error payloaderror_type- Thetypekey 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 model ConfigDict or Config class (for V1 pydantic).
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.
To know what kind of error type available, follow the official docs -> https://docs.pydantic.dev/latest/errors/validation_errors/
Advanced Usage
For nested pydantic models, you need to define validation_message_template Configuration on root model to trigger the message template customization, also field names should json path like reference to nested path fields.
from pydantic_validation_formatter import customize_validation_message
from pydantic import BaseModel, Field, ValidationError, ConfigDict
from typing import Annotated
@customize_validation_message
class Hero(BaseModel):
id: int = Field(gt=0, lt=5)
@customize_validation_message
class NestedModel(BaseModel):
heros: list[Hero]
leader_hero: Hero
ranks: list[Annotated[int, Field(gt=0)]]
hero_matrix: list[list[Hero]]
model_config = ConfigDict(
validation_message_template = {
"heros.*.id": {
"greater_than": "id value should be greater than {gt} but received {input}",
"missing": "id field is required",
},
"leader_hero.id": {
"greater_than": "id value should be greater than {gt} but received {input}",
"missing": "id field is required",
},
"ranks": {
"greater_than": "id value should be greater than {gt} but received {input}",
"missing": "id field is required",
},
"hero_matrix.*.*.id": {
"greater_than": "id value should be greater than {gt} but received {input}",
"missing": "id field is required",
},
}
)
try:
NestedModel(heros=[{{"id": 1}}, leader_hero={{"id": 1}}, ranks=[-1], hero_matrix=[[{"id": 2}]]])
except ValidationError as exc:
print(exc.errors())
Field References
Here the terminal data type referes to such data type which does not have further nested structure like int, str, list of int etc. Root model field with terminal data type -> just field name Root model field with iterable terminal data type -> just field name Root model field with single nested model ->
Metadata
Release files for pydantic-validation-formatter 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_validation_formatter-0.2.tar.gz | 8.8 kB | Without provenance | Details for pydantic_validation_formatter-0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_validation_formatter-0.2-py3-none-any.whl | Python 3 | none | any | Details for pydantic_validation_formatter-0.2-py3-none-any.whl |
Total release size: 16.5 kB
Release files / pydantic_validation_formatter-0.2.tar.gz
| Download URL | pydantic_validation_formatter-0.2.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
efb616b7a4d161c87dad561f1f09bb723d9e1691048600ea2012b8500fac71f0
|
|
BLAKE2b-256 checksum How to use checksums |
927816a3b7f8b67d4f10f873d05892b1f954f8940a2131b0810c0c7939192a7d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.15
|
Release files / pydantic_validation_formatter-0.2-py3-none-any.whl
| Download URL | pydantic_validation_formatter-0.2-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9150a28394defe67542d645ec47e4bcc7d692630b77b0bafd2155df15e8260ae
|
|
BLAKE2b-256 checksum How to use checksums |
641b63a524f0e2bdbf02a90b1eb64c21c54d5c15e94e557e94301612b36b2ff7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.15
|
Security for pydantic-validation-formatter 0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.