mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
f55515820a
* renaming .json -> .model_dump_json * renaming .dict -> .model_dump * renaming .__fields__ -> .model_fields * renaming .schema -> .model_json_schema * renaming .construct -> .model_construct * renaming .parse_obj -> .model_validate * make linters happy * add changes md-file Co-authored-by: Samuel Colvin <s@muelcolvin.com>
28 lines
604 B
Python
28 lines
604 B
Python
from datetime import date, timedelta
|
|
from pydantic import BaseModel
|
|
from pydantic.validators import int_validator
|
|
|
|
|
|
class DayThisYear(date):
|
|
"""
|
|
Contrived example of a special type of date that
|
|
takes an int and interprets it as a day in the current year
|
|
"""
|
|
|
|
@classmethod
|
|
def __get_validators__(cls):
|
|
yield int_validator
|
|
yield cls.validate
|
|
|
|
@classmethod
|
|
def validate(cls, v: int):
|
|
return date.today().replace(month=1, day=1) + timedelta(days=v)
|
|
|
|
|
|
class FooModel(BaseModel):
|
|
date: DayThisYear
|
|
|
|
|
|
m = FooModel(date=300)
|
|
print(m.model_dump_json())
|