Skip to content

Commit

Permalink
Add more accurate JSON schema for Pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
mdomke committed Apr 25, 2024
1 parent f6a761b commit 1b26676
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
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
22 changes: 21 additions & 1 deletion tests/test_ulid.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_pydantic_protocol() -> None:
ulid = ULID()

class Model(BaseModel):
ulid: ULID
ulid: ULID | None = None

for value in [ulid, str(ulid), int(ulid), bytes(ulid)]:
model = Model(ulid=value)
Expand All @@ -188,3 +188,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

0 comments on commit 1b26676

Please sign in to comment.