liveBook · Manning

Validation result

As explained in Clean Applications with Hexagonal Architecture

Validation is a Vavr value type for applicatively accumulating validation errors or producing a valid value.

Vavr’s `Validation<E, T>` represents either a valid value or one or more validation errors. Unlike `Either`, it can accumulate failures from multiple checks, so callers can see all problems at once. section 3.3.2

Use Validation where several checks should contribute to one result:

  • In a domain factory, combine field and invariant checks; apply the constructor only when all checks pass. section 3.3.2
  • At the command boundary, validate input syntax; leave business-rule checks to the domain. section 3.8.1

Collecting article errors

If an article has a null title and blank content, `Validation.combine(...).ap(Article::new)` returns both errors together. With valid fields, it produces a valid Article. section 3.3.2

Listing 3.3 Implementation of a factory method to enforce validation in construction
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Article {
  private final ArticleId id;
  private final Title title;
  private final Content content;
  private final AuthorId authorId;
 
  public static Validation<Seq<DomainError>, Article>
    validateThenCreate( #A
      final ArticleId id,
      final Title title,
      final Content content,
      final AuthorId authorId) {
    return Validation.combine( #B
      validateNotNull(id, "Article ID"),
      validateTitle(title),
      validateContent(content),
      validateNotNull(authorId, "Author ID")
    ).ap(Article::new);
  } #C
 
  private static Validation<DomainError, Title>
    validateTitle(final Title title) {
    return title == null || title.value().isBlank()
      ? Validation.invalid(
          new DomainError("Title must not be blank"))
      : Validation.valid(title);
  }
 
  private static Validation<DomainError, Content>
    validateContent(final Content content) {
    return content == null || content.value().isBlank()
      ? Validation.invalid(
          new DomainError("Content must not be blank"))
      : Validation.valid(content);
  }
 
  private static <T> Validation<DomainError, T>
    validateNotNull(final T value, final String name) {
    return value == null
      ? Validation.invalid(
          new DomainError(name + " must not be null"))
      : Validation.valid(value);
  }
}

See the factory method combine field validations and apply the private Article constructor, keeping direct construction unavailable. section 3.3.2

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Explains valid and invalid results, combining checks, and applying a constructor to validated values.
Examples
Uses article creation with invalid title and content, plus command checks for required fields and UUID formats.
What it assumes
Uses the pattern without requiring functional-programming terminology and distinguishes input syntax checks from domain rules.
What you can do after
Apply it via Vavr to collect multiple errors or produce a validated value from combined checks.
How it connects
Contrasts with Either and connects to factory methods, domain invariants, and command validation.

Also explained in

Compare how 3 books explain validation result →

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.