RoDev
RoDev

Reputation: 61

Bean Validation and altering the collection

We have a list of objects that need to be validated and processed before sending downstream. Can I use the bean validators to check the validity and remove the items if they are invalid from the list. Is it a correct approach to alter the state of input using a validator ?

Should the List be filtered/altered inside the business logic or Can i have a custom validator for the same?

Upvotes: 0

Views: 42

Answers (1)

fdreger
fdreger

Reputation: 12495

The assumption is that validation reads but never modifies objects.

This is the only way to ensure that validators are independent and thus - composable. Otherwise a change introduced by one validator could make an object invalid from the point of view of another validator, and vice-versa. This would also mean that running validation multiple times on the same object could give different results (which doesn't usually make sense, outside of specific cases where it's the outside world that changed).

So, while nothing will stop you from modifying an object in a constraint validator - it seems that the "correct" (whatever it means) approach would be to leave the filtering on the business logic side while keeping the the specific validation rules in the validation layer.

Upvotes: 1

Related Questions