From ed789986d2697194d3fe709901d7f80922c5f53e Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Tue, 26 May 2026 18:39:33 +0100 Subject: [PATCH] pydantic_models: Fix broken link, add RootModel equivalent --- synapse/util/pydantic_models.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/synapse/util/pydantic_models.py b/synapse/util/pydantic_models.py index 783d122013..d557694844 100644 --- a/synapse/util/pydantic_models.py +++ b/synapse/util/pydantic_models.py @@ -13,9 +13,16 @@ # # -from typing import Annotated +from typing import Annotated, TypeVar -from pydantic import AfterValidator, BaseModel, ConfigDict, StrictStr, StringConstraints +from pydantic import ( + AfterValidator, + BaseModel, + ConfigDict, + RootModel, + StrictStr, + StringConstraints, +) from synapse.api.errors import SynapseError from synapse.types import EventID @@ -42,12 +49,25 @@ class ParseModel(BaseModel): server operators. Subclassing in this way is recommended by - https://pydantic-docs.helpmanual.io/usage/model_config/#change-behaviour-globally + https://pydantic.dev/docs/validation/latest/concepts/config/#change-behaviour-globally + [accessed at 2026-05-26] """ model_config = ConfigDict(extra="ignore", frozen=True, strict=True) +T = TypeVar("T") + + +class StrictRootModel(RootModel[T]): + """ + A custom version of Pydantic's RootModel, in the same vein as `ParseModel`. + Refer to `ParseModel` above for the configuration details. + """ + + model_config = ConfigDict(frozen=True, strict=True) + + def validate_event_id_v1_and_2(value: str) -> str: try: EventID.from_string(value)