Skip to content

Commit 9915011

Browse files
authored
Allow constructing an ULID from an uuid.UUID (#322)
* Allow constructing ULID from UUID * Add UUID=>ULID test
1 parent cdd564f commit 9915011

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

pydantic_extra_types/ulid.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import annotations
77

8+
import uuid
89
from dataclasses import dataclass
910
from typing import Any, Union
1011

@@ -40,6 +41,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
4041
core_schema.int_schema(),
4142
core_schema.bytes_schema(),
4243
core_schema.str_schema(),
44+
core_schema.uuid_schema(),
4345
]
4446
),
4547
)
@@ -54,6 +56,8 @@ def _validate_ulid(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHa
5456
ulid = _ULID.from_int(value)
5557
elif isinstance(value, str):
5658
ulid = _ULID.from_str(value)
59+
elif isinstance(value, uuid.UUID):
60+
ulid = _ULID.from_uuid(value)
5761
elif isinstance(value, _ULID):
5862
ulid = value
5963
else:

tests/test_json_schema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,12 @@
222222
{
223223
'properties': {
224224
'x': {
225-
'anyOf': [{'type': 'integer'}, {'format': 'binary', 'type': 'string'}, {'type': 'string'}],
225+
'anyOf': [
226+
{'type': 'integer'},
227+
{'format': 'binary', 'type': 'string'},
228+
{'type': 'string'},
229+
{'format': 'uuid', 'type': 'string'},
230+
],
226231
'title': 'X',
227232
}
228233
},

tests/test_ulid.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import uuid
12
from datetime import datetime, timezone
23
from typing import Any
34

@@ -27,6 +28,9 @@ class Something(BaseModel):
2728
# Invalid ULID for str format
2829
('01BTGNYV6HRNK8K8VKZASZCFP', None, False), # Invalid ULID (short length)
2930
('01BTGNYV6HRNK8K8VKZASZCFPEA', None, False), # Invalid ULID (long length)
31+
# Valid ULID for UUID format
32+
(uuid.UUID('0196FEB3-9C99-8D8C-B3F3-4301C5E9DCE1'), '01JVZB774SHP6B7WT3072YKQ71', True),
33+
(uuid.UUID('0196FEB3-CD14-4B50-0015-C1E09BF7B221'), '01JVZB7K8M9D8005E1W2DZFCH1', True),
3034
# Valid ULID for _ULID format
3135
(_ULID.from_str('01BTGNYV6HRNK8K8VKZASZCFPE'), '01BTGNYV6HRNK8K8VKZASZCFPE', True),
3236
(_ULID.from_str('01BTGNYV6HRNK8K8VKZASZCFPF'), '01BTGNYV6HRNK8K8VKZASZCFPF', True),
@@ -62,7 +66,12 @@ def test_json_schema():
6266
assert Something.model_json_schema(mode='validation') == {
6367
'properties': {
6468
'ulid': {
65-
'anyOf': [{'type': 'integer'}, {'format': 'binary', 'type': 'string'}, {'type': 'string'}],
69+
'anyOf': [
70+
{'type': 'integer'},
71+
{'format': 'binary', 'type': 'string'},
72+
{'type': 'string'},
73+
{'format': 'uuid', 'type': 'string'},
74+
],
6675
'title': 'Ulid',
6776
}
6877
},
@@ -73,7 +82,12 @@ def test_json_schema():
7382
assert Something.model_json_schema(mode='serialization') == {
7483
'properties': {
7584
'ulid': {
76-
'anyOf': [{'type': 'integer'}, {'format': 'binary', 'type': 'string'}, {'type': 'string'}],
85+
'anyOf': [
86+
{'type': 'integer'},
87+
{'format': 'binary', 'type': 'string'},
88+
{'type': 'string'},
89+
{'format': 'uuid', 'type': 'string'},
90+
],
7791
'title': 'Ulid',
7892
}
7993
},

0 commit comments

Comments
 (0)