Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more accurate JSON schema for Pydantic #25

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ Changelog

Versions follow `Semantic Versioning <http://www.semver.org>`_

`2.4.0`_ - 2024-04-02
`2.5.0`_ - tbd
---------------------
Changed
~~~~~~~
* Generate a more accurate JSON schema with Pydantic's ``BaseModel.model_json_schema()``. This
includes a specification for string and byte representations.

`2.4.0`_ - 2024-04-02
---------------------
Added
~~~~~
* :class:`.ULID` objects are now properly serialized when used as Pydantic types `@Avihais12344 <https://github.com/Avihais12344>`_.
Expand Down Expand Up @@ -151,6 +157,7 @@ Changed
* The package now has no external dependencies.
* The test-coverage has been raised to 100%.

.. _2.5.0: https://github.com/mdomke/python-ulid/compare/2.4.0...2.5.0
.. _2.4.0: https://github.com/mdomke/python-ulid/compare/2.3.0...2.4.0
.. _2.3.0: https://github.com/mdomke/python-ulid/compare/2.2.0...2.3.0
.. _2.2.0: https://github.com/mdomke/python-ulid/compare/2.1.0...2.2.0
Expand Down
23 changes: 22 additions & 1 deletion tests/test_ulid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from typing import Optional
from typing import Union

import pytest
Expand Down Expand Up @@ -168,7 +169,7 @@ def test_pydantic_protocol() -> None:
ulid = ULID()

class Model(BaseModel):
ulid: ULID
ulid: Optional[ULID] = None

for value in [ulid, str(ulid), int(ulid), bytes(ulid)]:
model = Model(ulid=value)
Expand All @@ -188,3 +189,23 @@ class Model(BaseModel):
model_json = model.model_dump_json()
assert isinstance(json.loads(model_json)["ulid"], str)
assert Model.model_validate_json(model_json) == model

model_json_schema = model.model_json_schema()
assert "properties" in model_json_schema
assert "ulid" in model_json_schema["properties"]
assert "anyOf" in model_json_schema["properties"]["ulid"]
assert {
"maxLength": 26,
"minLength": 26,
"pattern": "[A-Z0-9]{26}",
"type": "string",
} in model_json_schema["properties"]["ulid"]["anyOf"]
assert {
"maxLength": 16,
"minLength": 16,
"type": "string",
"format": "binary",
} in model_json_schema["properties"]["ulid"]["anyOf"]
assert {
"type": "null",
} in model_json_schema["properties"]["ulid"]["anyOf"]
2 changes: 2 additions & 0 deletions ulid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler
[
core_schema.is_instance_schema(ULID),
core_schema.no_info_plain_validator_function(ULID),
core_schema.str_schema(pattern=r"[A-Z0-9]{26}", min_length=26, max_length=26),
core_schema.bytes_schema(min_length=16, max_length=16),
]
),
serialization=core_schema.to_string_ser_schema(
Expand Down