-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Moving to Pydantic V2 Co-authored-by: Zeke Zumbro <[email protected]> Co-authored-by: Jeferson Daniel <[email protected]>
- Loading branch information
1 parent
cda9b63
commit 561c122
Showing
10 changed files
with
72 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ build/ | |
pydantic_mongo.egg-info/ | ||
var/ | ||
.pytest_cache/ | ||
.idea/ |
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,18 +1,33 @@ | ||
from typing import Any | ||
|
||
from bson import ObjectId | ||
from pydantic_core import core_schema | ||
|
||
|
||
class ObjectIdField: | ||
class ObjectIdField(str): | ||
@classmethod | ||
def __get_validators__(cls): | ||
yield cls.validate | ||
def __get_pydantic_core_schema__( | ||
cls, _source_type: Any, _handler: Any | ||
) -> core_schema.CoreSchema: | ||
object_id_schema = core_schema.chain_schema( | ||
[ | ||
core_schema.str_schema(), | ||
core_schema.no_info_plain_validator_function(cls.validate), | ||
] | ||
) | ||
return core_schema.json_or_python_schema( | ||
json_schema=object_id_schema, | ||
python_schema=core_schema.union_schema( | ||
[core_schema.is_instance_schema(ObjectId), object_id_schema] | ||
), | ||
serialization=core_schema.plain_serializer_function_ser_schema( | ||
lambda x: str(x) | ||
), | ||
) | ||
|
||
@classmethod | ||
def validate(cls, value): | ||
if not ObjectId.is_valid(value): | ||
raise ValueError("Invalid id") | ||
|
||
return ObjectId(value) | ||
|
||
@classmethod | ||
def __modify_schema__(cls, field_schema): | ||
field_schema.update(type="string") |
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
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
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,33 +1,30 @@ | ||
import pytest | ||
from bson import ObjectId | ||
from pydantic import BaseModel | ||
from pydantic.error_wrappers import ValidationError | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from pydantic_mongo import ObjectIdField | ||
|
||
|
||
class User(BaseModel): | ||
id: ObjectIdField = None | ||
|
||
class Config: | ||
json_encoders = {ObjectId: str} | ||
|
||
|
||
class TestFields: | ||
def test_object_id_validation(self): | ||
with pytest.raises(ValidationError): | ||
User.parse_obj({"id": "lala"}) | ||
User.parse_obj({"id": "611827f2878b88b49ebb69fc"}) | ||
User.model_validate({"id": "lala"}) | ||
User.model_validate({"id": "611827f2878b88b49ebb69fc"}) | ||
|
||
def test_object_id_serialize(self): | ||
lala = User(id=ObjectId("611827f2878b88b49ebb69fc")) | ||
json_result = lala.json() | ||
assert '{"id": "611827f2878b88b49ebb69fc"}' == json_result | ||
json_result = lala.model_dump_json() | ||
assert '{"id":"611827f2878b88b49ebb69fc"}' == json_result | ||
|
||
def test_modify_schema(self): | ||
user = User(id=ObjectId("611827f2878b88b49ebb69fc")) | ||
schema = user.schema() | ||
schema = user.model_json_schema() | ||
assert { | ||
"title": "User", | ||
"type": "object", | ||
"properties": {"id": {"title": "Id", "type": "string"}}, | ||
"properties": {"id": {"default": None, "title": "Id", "type": "string"}}, | ||
} == schema |
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