I am writing a code to validate category using vavr
private static Validation<ConstraintViolation, List<Category>> isCategoryValid(
List<Category> categories) {
java.util.List<Category> categoryList = new ArrayList<>();
for (Category category : categories) {
List<Product> detailsRequest = validateList(category.getDetails());
if (detailsRequest .isEmpty()) {
return invalid(new ConstraintViolation("Details", "Details cannot be empty"));
}
...more validations
categoryList.add(WCategory.builder().details(List.ofAll(detailsList))
.type(category.getType()).build());
}
return valid(List.ofAll(categoryList));
}
I have to use java.util.List as I cannot achieve it using vavr itself. If I use
categories.map(x -> x..);
I cannot return from the loop if validation fails and will get the output as List<Validations<ConstraintViolation, List<Category>>> which is not what I wanted.
EDIT:
private static Validation<RuntimeException, List<String>> isCategoryValid(
List<String> categories) {
java.util.List<String> categoryList = new ArrayList<>();
for (String category : categories) {
String detailsRequest = validateList(category);
if (detailsRequest != "") {
return invalid(new RuntimeException("Details cannot be empty"));
}
...more validations
categoryList.add(detailsRequest);
}
return valid(List.ofAll(categoryList));
}
It depends a bit on the behaviour you'd like to achieve. If you only want to get the failed validation for the first category in the list, which seems to be the case, you could use
Either.traverseRightand then convert that to a validation. ThetraverseRightwill only keep the first failed entry or the list of succesful things.So the code could look like this:
It might also be a good idea, depending on the usecase, to keep more validation errors. If you opt for this approach, you might look into the
Validation.traverseto keep as much validation errors as possible. That's where theValidationreally shines in my opinion.So then the code would look like this: