-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
31 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,46 @@ | ||
from bson import ObjectId as _ObjectId | ||
from bson.errors import InvalidId | ||
from pydantic import BaseModel, Field, ConfigDict, AfterValidator | ||
from pydantic import BaseModel, Field, ConfigDict, AfterValidator, BeforeValidator | ||
from typing_extensions import Annotated | ||
|
||
|
||
def check_object_id(value: str) -> str: | ||
# def check_object_id(value: str) -> str: | ||
# if not _ObjectId.is_valid(value): | ||
# raise ValueError('Invalid ObjectId') | ||
# return value | ||
|
||
|
||
def check_object_id(value: _ObjectId) -> str: | ||
""" | ||
Checks if the given _ObjectId is valid and returns it as a string. | ||
Args: | ||
value: The _ObjectId to be checked. | ||
Returns: | ||
str: The _ObjectId as a string. | ||
Raises: | ||
ValueError: If the _ObjectId is invalid. | ||
""" | ||
|
||
if not _ObjectId.is_valid(value): | ||
raise ValueError('Invalid ObjectId') | ||
return value | ||
raise ValueError("Invalid ObjectId") | ||
return str(value) | ||
|
||
|
||
ObjectIdField = Annotated[str, AfterValidator(check_object_id)] | ||
ObjectIdField = Annotated[str, BeforeValidator(check_object_id)] | ||
|
||
|
||
config = ConfigDict(arbitrary_types_allowed=True, populate_by_name=True) | ||
|
||
|
||
class Document(BaseModel): | ||
# model_config = config | ||
model_config = config | ||
|
||
name: str = Field(...) | ||
desc: str = Field(...) | ||
name: str | ||
desc: str | ||
|
||
|
||
class DocumentResponse(BaseModel): | ||
id: ObjectIdField = Field(...) | ||
id: ObjectIdField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters