Skip to content

Commit

Permalink
Add the model to parse_timedelta documentation
Browse files Browse the repository at this point in the history
Rather than only showing the bare field_validator function, put it
in a sample model for a better example.
  • Loading branch information
rra committed May 22, 2024
1 parent 93e7f0e commit 9aa60c4
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions docs/user-guide/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 9aa60c4

Please sign in to comment.