From 9aa60c4c9889a4638f140f0b11e4dcc871da1cbf Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Wed, 22 May 2024 08:56:10 -0700 Subject: [PATCH] Add the model to parse_timedelta documentation Rather than only showing the bare field_validator function, put it in a sample model for a better example. --- docs/user-guide/datetime.rst | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/docs/user-guide/datetime.rst b/docs/user-guide/datetime.rst index c0f49e1..a7728ee 100644 --- a/docs/user-guide/datetime.rst +++ b/docs/user-guide/datetime.rst @@ -99,17 +99,22 @@ To accept this syntax as input for a Pydantic model, use a field validator such .. code-block:: python - from pydantic import field_validator + from pydantic import BaseModel, field_validator from safir.datetime import parse_timedelta - @field_validator("lifetime", mode="before") - @classmethod - def _validate_lifetime( - cls, v: str | float | timedelta - ) -> float | timedelta: - if not isinstance(v, str): - return v - return parse_timedelta(v) + class Someething(BaseModel): + lifetime: timedelta = Field(..., title="Lifetime") + + # ... other fields + + @field_validator("lifetime", mode="before") + @classmethod + def _validate_lifetime( + cls, v: str | float | timedelta + ) -> float | timedelta: + if not isinstance(v, str): + return v + return parse_timedelta(v) This disables the built-in Pydantic support for ISO 8601 durations in favor of the syntax shown above.