liveBook · Manning

Validation

As explained in Simple Object-Oriented Design: Create clean, maintainable applications

A validation mechanism checks requested values or state changes against validity constraints.

Validation checks requested values or state changes against validity constraints, preventing unexpected errors and helping systems handle invalid input gracefully. Define the consequences of invalid data instead of leaving clients to discover them through failures. section 3.2

Keep validation close to the behavior that protects consistency, while moving genuinely complex checks outward:

  • Let the class containing the data enforce its own consistency whenever the required information is available locally. section 3.1.1
  • Encapsulate operations requiring database information or complex business rules in a service or dedicated validation class. section 3.1.2 section 3.7
  • Avoid setters for attributes whose changes require consistency checks; expose meaningful operations that perform the checks safely. section 3.4.2
  • Choose between explicitly defining method preconditions and postconditions and validating input from a business perspective. section 3.2

Protecting an offering

An Offering should reject enrollment when its participant limit is reached and reduce its available seats when enrollment succeeds. Clients should call an enrollment operation rather than modify a participant collection or seat count through unrestricted access. If deciding whether enrollment is valid requires database information, an accompanying service should encapsulate the complete operation and its checks. section 3.1.1 section 3.1.2

Figure 3.1 An entity ensuring consistency

Use the entity-consistency diagram to see how validation remains with the data-owning class when local rules are sufficient. section 3.1.1

How Simple Object-Oriented Design: Create clean, maintainable applications covers it

How deep it goes
Explains ownership, service extraction, setter design, and two validation approaches, but does not detail their implementations.
Examples
Uses training-course offerings, participant limits, enrollment, database-dependent checks, and protected versus unprotected baskets.
What it assumes
Assumes object-oriented entities and clients, while treating validation design as part of managing object consistency.
What you can do after
Apply validation by placing local checks in entities and complex business checks in services or dedicated validation classes.
How it connects
Connects validation with consistency, entities, services, setters, aggregate roots, preconditions, and postconditions.

Also explained in

Compare how 30 books explain validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Simple Object-Oriented Design: Create clean, maintainable applications. The section links point to the parts of the book it draws on.