Contextual validation
Checking whether an object is valid for a specific action, such as “ready to publish”, instead of one isValid() that answers for every situation.
Martin Fowler points out that “valid” depends on what you are about to do. A draft doctor profile without a photo is fine to save and wrong to publish; an order without a shipping address is fine in the cart and wrong at checkout. A single isValid() forces either blocking drafts or letting bad data through.
Naming the check after the action (canPublish, readyForCheckout) keeps the rule in the ubiquitous language and lets the user save work in progress. Rules that hold always are invariants and belong in the model; input format checks at the edge are runtime validation.
Sources
- Martin Fowler: Contextual Validation martinfowler.com
Related terms
- Business invariant: A business rule that must be true at all times, such as “a slot cannot be booked twice”, which the model enforces on every change.
- Runtime validation (Zod): Checking at run time that data from outside (requests, APIs, files, models) matches the type your code assumes.
- Value object: A small immutable object defined only by its values, like money or a date range; two with the same values are the same thing.
