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


