Custom index for ConstraintViolation propertyPath


#1

Hello,
the result of ConstraintViolation.getPropertyPath() method is quite useful to identify where the violation occurs. It could be improved in case of collections in a hierarchy. In this case the path contains the numeric index of the element of the collection.
Is there a way to obtain instead the value of a field (that has a unique value for the bean) or method? How can achieve this?

For example:

bean1[field1Value].bean2[field2Value].property

instead of

bean1[2].bean2[3].property


#2

Hi @abiuan,

You can use the getValue() method of a org.hibernate.validator.path.PropertyNode or of a org.hibernate.validator.path.ContainerElementNode by casting the node to it (you have a as() method on the node to do the cast).

This way, you have access to the value.

It’s an extra feature of Hibernate Validator, it’s not in Bean Validation, that’s why it’s a bit hidden.