From 3cf97556d347cb52b08cb2ba990d1ef4f53b024b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20G=C5=82owski?= Date: Wed, 2 Apr 2025 17:27:48 +0200 Subject: [PATCH] fix: disallow bool as input value for ULID --- pydantic_extra_types/ulid.py | 2 ++ tests/test_ulid.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/pydantic_extra_types/ulid.py b/pydantic_extra_types/ulid.py index 814e0508..ae02f5a6 100644 --- a/pydantic_extra_types/ulid.py +++ b/pydantic_extra_types/ulid.py @@ -47,6 +47,8 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH @classmethod def _validate_ulid(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> Any: ulid: _ULID + if isinstance(value, bool): + raise PydanticCustomError('ulid_format', 'Unrecognized format') try: if isinstance(value, int): ulid = _ULID.from_int(value) diff --git a/tests/test_ulid.py b/tests/test_ulid.py index 94cec2d8..7b8b1296 100644 --- a/tests/test_ulid.py +++ b/tests/test_ulid.py @@ -37,6 +37,9 @@ class Something(BaseModel): (109667145845879622871206540411193812282, '2JG4FVY7N8XS4GFVHPXGJZ8S9T', True), (109667145845879622871206540411193812283, '2JG4FVY7N8XS4GFVHPXGJZ8S9V', True), (109667145845879622871206540411193812284, '2JG4FVY7N8XS4GFVHPXGJZ8S9W', True), + # Invalid ULID for bool format + (True, None, False), + (False, None, False), ], ) def test_format_for_ulid(ulid: Any, result: Any, valid: bool):