liveBook · Manning

Validation

As explained in Data-Oriented Programming in Java

Validation is the process of checking data and system conditions against the preconditions required for safe continuation.

Validation checks whether data and system conditions satisfy the preconditions for safe continuation. At construction time, a type can reject malformed sample IDs, negative elapsed days, or out-of-range contact angles, preventing incorrect values from entering the program. This turns domain constraints from comments and naming conventions into executable defenses. section 3.2.2 section 3.7

Constructor checks are valuable, but validation must also preserve meaning after construction. A raw double extracted from a measurement no longer carries its unit or allowed range; transformations and method calls can progressively erase the context that made the value safe. Use types to retain important distinctions instead of relying on developers to remember them. section 3.2.2 section 3.7

For feature-level work, validation is more than collecting input errors. Treat it as a preflight stage that gathers the information required by the operation—such as product details, prices, inventory, and cart state—and produces data that demonstrates the operation may proceed. An input with no local errors can still be unsafe when required system knowledge is missing. section 9.3.3 section 9.6

A robust validation boundary should:

  • Check explicit input constraints before processing begins. section 3.2.2
  • Acquire external facts needed to establish feature preconditions, rather than deferring them to later steps. section 9.3.3
  • Return or construct a dedicated valid representation whose fields encode the evidence downstream code needs. section 9.3.3
  • Avoid partially constructed, mutable objects whose missing fields can bypass earlier checks and create invalid states. section 9.2.4

Validating an add-items operation

An add-items request may pass annotations while seller identity, inventory, or current pricing remain unknown. Build a ValidAddItemsRequest only after checking input errors and preloading the needed facts; downstream cart updates then receive items keyed by product and seller, with quantities and prices available. If refreshing a cart creates an intermediate condition, model that condition explicitly rather than pretending it is fully valid. section 9.3.3 section 9.2.4

Listing 9.30 Representing what “valid” means
// -------- Legacy Models ----------
 
class AddItemsRequest {   
  @NotEmpty @Unique List<ItemInfo> items;  #A   
}
class ItemInfo {          
  @NotBlank @NotNull String productId;   #A
  Optional<String> sellerId;  #A
  @Min(1) long quantity;      #A
  BigDecimal displayedPrice;  #A
}
// ----------------------------------
 
 
// ----------- Refactored -----------
record Item(
    ProductSeller productSeller,  #B
    Details details, #B
    long desiredQty,  
    long availableQty, #B
    BigDecimal lastNotifiedPrice, 
    BigDecimal currentPrice #B
){
   // null checks elided
}
 
record ValidAddItemsRequest(           #C  
    Map<ProductSeller, Item> incoming  #C
){
    ValidAddItemsRequest {
        if (incoming.isEmpty()) {
            raise IllegalArgumentExecption("...");
        }
    }
}

// AddItemsRequest -> ValidAddItemsRequest  #D

Compare a loose request model with a validated representation that carries resolved product, inventory, and price evidence. section 9.3.3

How Data-Oriented Programming in Java covers it

How deep it goes
Develops constructor checks, then extends validation into evidence-gathering and typed feature preconditions.
Examples
Uses measurements with constrained fields and cart-item requests missing seller, inventory, pricing, or refreshed-cart evidence.
What it assumes
Assumes Java records, constructors, collections, and application-level input processing.
What you can do after
Apply it via typed boundaries that admit only data backed by the preconditions an operation requires.
How it connects
Connects invariants, data types, constructors, nulls, preconditions, mutation, and shotgun parsing.

Also explained in

Compare how 44 books explain validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.