mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 14:50:19 +00:00
27 lines
528 B
Python
27 lines
528 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)
|