liveBook · Manning

Domain validation

As explained in Clean Applications with Hexagonal Architecture

Business invariant validation checks whether domain data and operations satisfy the rules that must remain true for valid business state.

When each adapter validates article titles independently, their rules can drift. Domain validation keeps business invariants in the domain so every entry point uses the same rules. It guards valid business state, not just the shape of incoming data. section 2.4.7 section 3.8

An always-valid domain model rejects invalid state before an object exists. A private constructor and a validating factory method give callers one domain creation path: if an invariant fails, the factory returns an error instead of an object. Invariants also apply during state transitions. section 3.3 section 3.8

Place each check at the boundary it protects:

  • The adapter checks transport structure, such as whether an HTTP body is well-formed JSON. section 3.8
  • Commands and queries check input syntax, such as blank fields or UUID format. section 3.8
  • Entity factories, value object constructors, and domain services enforce business invariants. The handler orchestrates rather than duplicating those rules. section 3.8

Creating and publishing an article

A create command can reject a blank title as malformed input. The domain enforces semantic rules, such as title uniqueness within an author's portfolio and the prohibition on publishing an article without content. A handler can look up the author, call the factory, and save the result without owning the title rule. section 3.8

Constructing an object and calling isValid afterward leaves an invalid object available in the meantime. Moving the check into a handler avoids that window but makes enforcement depend on every handler remembering the rule. A domain factory separates valid creation from framework-shaped persistence or serialization construction. section 3.3.1

Table 3.2 Input validation vs business invariants
Aspect
Input validation
Business invariant
Where
Commands, queries, adapters
Domain entities, value objects, domain services
When
At the boundary, before entering the core
At construction time or during state transitions
Response to failure
Return all errors at once (user-friendly)
Reject invalid state; either fail fast or return accumulated domain errors, depending on the operation
Error type
Validation errors—expected, recoverable
Domain errors— business rule or state violations

Compare validation locations, timing, and failure responses across the boundary and domain. section 3.8

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Explains creation-time and transition checks, contrasting them with boundary validation and handler-owned rules; defers command validation implementation.
Examples
Uses article title validation, author-portfolio uniqueness, and the prohibition on publishing without content.
What you can do after
Build an always-valid domain creation path using a private constructor and a factory that rejects invariant violations.
How it connects
Connects business invariants to the always-valid domain model, hexagonal architecture, value objects, and domain services.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clean Applications with Hexagonal Architecture. The section links point to the parts of the book it draws on.