ARC-50Recommended
Wrap domain values in their own types
Give each domain value with its own rules a small type, such as EmailAddress, OrderId or Money. Never pass it around as a bare string or number.
How to do it
Validate the value once, in the type's constructor or factory, and trust it everywhere after. Two IDs then cannot be swapped by mistake, because their types differ. Use branded types in TypeScript, NewType or a frozen dataclass in Python, and a readonly class in PHP. Store money as the money rule says, never as a float.
Examples
TypeScript example
type OrderId = string & { readonly brand: 'OrderId' };
type CustomerId = string & { readonly brand: 'CustomerId' };
function refund(order: OrderId, customer: CustomerId) { /* ... */ }
refund(customerId, orderId); // Fails to compile, because the arguments are swapped
Python example
OrderId = NewType("OrderId", str)
@dataclass(frozen=True)
class EmailAddress:
value: str
def __post_init__(self):
if "@" not in self.value:
raise InvalidEmail(self.value)
How to check
A type test passes a swapped ID to a typed function and marks the line as an expected error. Examples are // @ts-expect-error in TypeScript and # type: ignore[arg-type] with mypy --warn-unused-ignores. The checker fails if the error ever disappears. A unit test gives each factory an invalid value, such as "not-an-email", and expects an error.
Related rules
- API-8 Send money as an integer with its currency in API design
- ARC-75 Change an aggregate only through its root in Clean code and architecture
- PAY-10 Hold money as integer minor units everywhere in Payments and billing
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/data-shapes/value-objects">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.