Built-in Value Objects
The library provides several built-in value object types.
All primitive value objects inherit from the generic ValueObject[T] base class and
implement a consistent validation pattern using the @validate decorator.
Each primitive type wraps a specific Python built-in type and enforces two core
validations:
- Ensuring the value is not
None, otherwise raising aRequiredValueError. - Ensuring the value matches the expected annotated type, otherwise raising an
InvalidTypeError.
Each value object type can be extended and customized as needed.
Boolean
The Boolean class wraps Python bool values and provides the foundation for creating domain-specific boolean value objects.
Integer
The Integer class wraps Python int values and provides the foundation for creating domain-specific integer value objects.
Float
The Float class wraps Python float values and provides the foundation for creating domain-specific float value objects.
String
The String class wraps Python str values and provides the foundation for creating domain-specific string value objects.
List
The List class wraps Python list values and provides the foundation for creating domain-specific list value objects.
List[T] wraps a Python list and must be subclassed with a type parameter. It
enforces list-level and element-level constraints at runtime.
Following the same pattern as other value objects, it ensures the value is not None and is of type list.
Additionally, it validates that the class is typed with a specific type parameter and
enforces at runtime that all elements in the list match the specified type.
You can use this value object to wrap lists of primitive types or other value objects.
from sindripy.value_objects import List, String
class Strings(List[str]): ...
class Email(String): ...
class Emails(List[Email]): ...
When creating a new instance you can pass a list of values matching the specified type
or delegate the instantiation of the elements to the value object itself through the from_primitives
named constructor:
emails = Emails([Email("johndoe@gmail.com"), Email("dimanupy@hotmail.com")])
emails = Emails.from_primitives(["johndoe@gmail.com", "dimanupy@hotmail.com"])
String UUID
Identifier value objects provide type-safe, validated wrappers around entity identifiers.
Unlike primitive String value objects, identifiers enforce specific format constraints
(such as UUID format) and communicate intent in the domain model. They prevent mixing different types
of identifiers (e.g., using a UserId where an OrderId is expected) through the type system.
The library currently provides StringUuid as the base identifier type, which validates UUID format strings.