Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 2fcd327

Browse files
authored
add UUID field (#106)
1 parent b6aec62 commit 2fcd327

4 files changed

Lines changed: 12 additions & 4 deletions

File tree

tests/test_fields.py

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

66
from typesystem.base import Message, ValidationError
77
from typesystem.fields import (
8+
UUID,
89
Array,
910
Boolean,
1011
Choice,
@@ -715,13 +716,13 @@ def test_datetime():
715716

716717

717718
def test_uuid():
718-
validator = String(format="uuid")
719+
validator = UUID()
719720
value, error = validator.validate_or_error("93e19019-c7a6-45fe-8936-f6f4d550f35f")
720721
assert value == uuid.UUID("93e19019-c7a6-45fe-8936-f6f4d550f35f")
721722

722-
validator = String(format="uuid")
723+
validator = UUID()
723724
value, error = validator.validate_or_error("1245a678-1234-1234-1234-123412341234")
724-
assert error == ValidationError(text="Must be valid UUID format.", code="format")
725+
assert error == ValidationError(text="Must be a valid UUID format.", code="format")
725726

726727

727728
def test_union():

typesystem/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typesystem.base import Message, ParseError, Position, ValidationError
22
from typesystem.fields import (
3+
UUID,
34
Any,
45
Array,
56
Boolean,
@@ -44,6 +45,7 @@
4445
"Text",
4546
"Time",
4647
"Union",
48+
"UUID",
4749
# Schemas
4850
"Schema",
4951
"SchemaDefinitions",

typesystem/fields.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,8 @@ def validate(self, value: typing.Any, strict: bool = False) -> typing.Any:
760760
raise self.validation_error("only_null")
761761
raise self.validation_error("const")
762762
return value
763+
764+
765+
class UUID(String):
766+
def __init__(self, **kwargs: typing.Any) -> None:
767+
super().__init__(format="uuid", **kwargs)

typesystem/formats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def serialize(self, obj: typing.Any) -> typing.Union[str, None]:
155155

156156

157157
class UUIDFormat(BaseFormat):
158-
errors = {"format": "Must be valid UUID format."}
158+
errors = {"format": "Must be a valid UUID format."}
159159

160160
def is_native_type(self, value: typing.Any) -> bool:
161161
return isinstance(value, uuid.UUID)

0 commit comments

Comments
 (0)