From f73e8c4001e09c3b9d03655155b073c591447592 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 30 Nov 2020 19:12:15 +0200 Subject: Move ObjectId to special types directory --- backend/models/_object_id.py | 23 ----------------------- backend/models/types/__init__.py | 3 +++ backend/models/types/object_id.py | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 23 deletions(-) delete mode 100644 backend/models/_object_id.py create mode 100644 backend/models/types/__init__.py create mode 100644 backend/models/types/object_id.py (limited to 'backend') diff --git a/backend/models/_object_id.py b/backend/models/_object_id.py deleted file mode 100644 index f0e47cf..0000000 --- a/backend/models/_object_id.py +++ /dev/null @@ -1,23 +0,0 @@ -import typing as t -from bson import ObjectId as OriginalObjectId - - -class ObjectId(OriginalObjectId): - """ObjectId implementation for Pydantic.""" - - @classmethod - def __get_validators__(cls) -> t.Generator[t.Callable, None, None]: - """Get validators for Pydantic.""" - yield cls.validate - - @classmethod - def validate(cls, value: t.Any) -> t.Optional["ObjectId"]: - """Checks value validity to become ObjectId and if valid, return ObjectId.""" - if OriginalObjectId.is_valid(value): - raise ValueError(f"Invalid value '{value}' for ObjectId.") - return ObjectId(value) - - @classmethod - def __modify_schema__(cls, field_schema: t.Dict[str, t.Any]) -> None: - """Update data type to string.""" - field_schema.update(type="string") diff --git a/backend/models/types/__init__.py b/backend/models/types/__init__.py new file mode 100644 index 0000000..ae408d7 --- /dev/null +++ b/backend/models/types/__init__.py @@ -0,0 +1,3 @@ +from .object_id import ObjectId + +__all__ = ["ObjectId"] diff --git a/backend/models/types/object_id.py b/backend/models/types/object_id.py new file mode 100644 index 0000000..f0e47cf --- /dev/null +++ b/backend/models/types/object_id.py @@ -0,0 +1,23 @@ +import typing as t +from bson import ObjectId as OriginalObjectId + + +class ObjectId(OriginalObjectId): + """ObjectId implementation for Pydantic.""" + + @classmethod + def __get_validators__(cls) -> t.Generator[t.Callable, None, None]: + """Get validators for Pydantic.""" + yield cls.validate + + @classmethod + def validate(cls, value: t.Any) -> t.Optional["ObjectId"]: + """Checks value validity to become ObjectId and if valid, return ObjectId.""" + if OriginalObjectId.is_valid(value): + raise ValueError(f"Invalid value '{value}' for ObjectId.") + return ObjectId(value) + + @classmethod + def __modify_schema__(cls, field_schema: t.Dict[str, t.Any]) -> None: + """Update data type to string.""" + field_schema.update(type="string") -- cgit v1.2.3