I have the code
private static Validation<ConstraintViolation, List<Person>> validatePersonDetail(
List<PersonDetailRequest> personRequest) {
for (PersonRequest request:personRequest) {
if (isNull(request.getName())) {
return invalid(new ConstraintViolation("name", "name cannot be empty"));
}
..
// more validations
// build object
Person.builder().name(request.getName()).build();
}
return valid([PERSON_LIST]);
}
I want to return Person List but not sure how to go with Vavr. I cannot use Validation combine as it contains nested validations for nested objects
Suppose you just build Person
when the list pass all validation, then map
is what you need:
private static Validation<ConstraintViolation, List<Person>> validatePersonDetail(
List<PersonRequest> personRequest) {
for (PersonRequest request:personRequest) {
if (isNull(request.getName())) {
return invalid(new ConstraintViolation("name", "name cannot be empty"));
}
// more validations
}
return valid(personRequest.map(request->Person.builder().name(request.getName()).build()));
}
I would recommend something like this. return list of Error from validatePersonDetail and check if it is null or empty, if it is empty then go ahead with your execution ahead.
private void mainExecution() {
Validation<ConstraintViolation> validationErrors = validatePersonDetail(personRequest);
if(validatePersonDetail().isEmpty) {
return valid([PERSON_LIST]);
} else {
throw new ValidationFailedException(validationErrors);
}
}
private static Validation<ConstraintViolation> validatePersonDetail(
List<PersonDetailRequest> personRequest) {
Validation<ConstraintViolation> validations = new ArrayList<>();
for (PersonRequest request:personRequest) {
if (isNull(request.getName())) {
return validations.add(new ConstraintViolation("name", "name cannot be empty"));
}
..
// more validations
}
return validations;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.