Files
pydantic/docs/examples/ex_dataclasses_config.py
T
jarekkar 72791787ea Add support for passing Configs to Dataclasses (#285)
* Add support for passing Configs to Dataclasses

* formatting and fixes

* move tests into dataclasses_config.py

* use inherit_config in create_model

fix #276
2018-12-27 17:54:17 +00:00

32 lines
630 B
Python

from datetime import datetime
from pydantic import ValidationError
from pydantic.dataclasses import dataclass
class MyConfig:
max_anystr_length = 10
validate_assignment = True
error_msg_templates = {
'value_error.any_str.max_length': 'max_length:{limit_value}',
}
@dataclass(config=MyConfig)
class User:
id: int
name: str = 'John Doe'
signup_ts: datetime = None
user = User(id='42', signup_ts='2032-06-21T12:00')
try:
user.name = 'x' * 20
except ValidationError as e:
print(e)
"""
1 validation error
name
max_length:10 (type=value_error.any_str.max_length; limit_value=10)
"""