mirror of
https://github.com/element-hq/synapse.git
synced 2026-08-28 09:24:41 +00:00
pydantic_models: Fix broken link, add RootModel equivalent
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user