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));
}