Skip to content
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
4 changes: 4 additions & 0 deletions pydantic_extra_types/ulid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import uuid
from dataclasses import dataclass
from typing import Any, Union

Expand Down Expand Up @@ -40,6 +41,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
core_schema.int_schema(),
core_schema.bytes_schema(),
core_schema.str_schema(),
core_schema.uuid_schema(),
]
),
)
Expand All @@ -54,6 +56,8 @@ def _validate_ulid(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHa
ulid = _ULID.from_int(value)
elif isinstance(value, str):
ulid = _ULID.from_str(value)
elif isinstance(value, uuid.UUID):
ulid = _ULID.from_uuid(value)
elif isinstance(value, _ULID):
ulid = value
else:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@
{
'properties': {
'x': {
'anyOf': [{'type': 'integer'}, {'format': 'binary', 'type': 'string'}, {'type': 'string'}],
'anyOf': [
{'type': 'integer'},
{'format': 'binary', 'type': 'string'},
{'type': 'string'},
{'format': 'uuid', 'type': 'string'},
],
'title': 'X',
}
},
Expand Down
18 changes: 16 additions & 2 deletions tests/test_ulid.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from datetime import datetime, timezone
from typing import Any

Expand Down Expand Up @@ -27,6 +28,9 @@ class Something(BaseModel):
# Invalid ULID for str format
('01BTGNYV6HRNK8K8VKZASZCFP', None, False), # Invalid ULID (short length)
('01BTGNYV6HRNK8K8VKZASZCFPEA', None, False), # Invalid ULID (long length)
# Valid ULID for UUID format
(uuid.UUID('0196FEB3-9C99-8D8C-B3F3-4301C5E9DCE1'), '01JVZB774SHP6B7WT3072YKQ71', True),
(uuid.UUID('0196FEB3-CD14-4B50-0015-C1E09BF7B221'), '01JVZB7K8M9D8005E1W2DZFCH1', True),
# Valid ULID for _ULID format
(_ULID.from_str('01BTGNYV6HRNK8K8VKZASZCFPE'), '01BTGNYV6HRNK8K8VKZASZCFPE', True),
(_ULID.from_str('01BTGNYV6HRNK8K8VKZASZCFPF'), '01BTGNYV6HRNK8K8VKZASZCFPF', True),
Expand Down Expand Up @@ -62,7 +66,12 @@ def test_json_schema():
assert Something.model_json_schema(mode='validation') == {
'properties': {
'ulid': {
'anyOf': [{'type': 'integer'}, {'format': 'binary', 'type': 'string'}, {'type': 'string'}],
'anyOf': [
{'type': 'integer'},
{'format': 'binary', 'type': 'string'},
{'type': 'string'},
{'format': 'uuid', 'type': 'string'},
],
'title': 'Ulid',
}
},
Expand All @@ -73,7 +82,12 @@ def test_json_schema():
assert Something.model_json_schema(mode='serialization') == {
'properties': {
'ulid': {
'anyOf': [{'type': 'integer'}, {'format': 'binary', 'type': 'string'}, {'type': 'string'}],
'anyOf': [
{'type': 'integer'},
{'format': 'binary', 'type': 'string'},
{'type': 'string'},
{'format': 'uuid', 'type': 'string'},
],
'title': 'Ulid',
}
},
Expand Down