Context: Microservice that has exposed REST API and handles input requests in JSON format sequentially. Handled request is deserialized, validated and put into Kafka topic for futher processing.
Given example of class to be validated:
// autogenerated from openapi schema using custom templates
public class DataPayload {
@NotNull
@Size(min=1, max=100)
private String description;
@Valid
@Size(max=1024)
private List<DataLine> dataLines;
// getters, setters, etc.
public static class DataLine {
// lots of fields to be validated..
}
}
We run validation using jsr303/jsr380 Bean Validation:
public static void main(String[] args) {
var validator = Validation.buildDefaultValidatorFactory().getValidator();
var violations = validator.validate(getDataPayload());
}
Does anybody have an idea how validation of List<DataLine> dataLines
could be parallelized with minimal efforts?
Several (obvious) options I have so far:
- Manually run in parallel
validator.validate(dataLine)
from the list along with validation DataPayload without dataLinesvalidator.validate(withoutDataLines(dataPayload))
. - Similar to 1st option but with some tricks around Validation Groups.
- (Not sure if it is possible). Custom
ConstraintValidator
that runs validation for container objects in parallel. Open question - how to delegate nested/cascaded validation to default mechanism?
Despite options are viable I am wondering is there more smart and elegant way to solve this problem..