Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method in KiwiConstraintViolations to get the property path of a violation #1170

Closed
sleberknight opened this issue Jul 26, 2024 · 0 comments · Fixed by #1171
Closed

Add method in KiwiConstraintViolations to get the property path of a violation #1170

sleberknight opened this issue Jul 26, 2024 · 0 comments · Fixed by #1171
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

sleberknight commented Jul 26, 2024

This is purely a convenience method that does:

var path = violation.getPropertyPath().toString();

No, it's not much but it saves boilerplate. In addition, if the actual implementation ever does change, this method's implementation could also be changed to work the way it has for literally all the years I've been using Jakarta Bean Validation (and Java Bean Validation before it moved to Jakarta). In other words, code that uses this new method instead of Path#toString directly would not need to change in the (unlikely) event that the implementation changes, because we can simply update the implementation in KiwiConstraintViolations, and all code using our implementation keeps working once the kiwi dependency version is updated.

We will add all the potential problems with using this to the javadoc as an implementation note, specifically this:

Clients should not rely on any specific structure of the returned value.

While that's nice in theory, Hibernate Validator (the Jakarta Beans Validation reference implementation) has pretty much used the same implementation for as long as I've been using it probably going back to 2007-ish. The javadocs for jakarta.validation.Path#toString then say:

Instead they should iterate through the path nodes and obtain any required information by calling the methods on Path. Node and its sub-types.

Again, while nice in theory this is tedious at best, and quite annoying at worst. It seems like the spec should define a standardized way to represent a property path, but it doesn't. And I am tired of always writing violation.getPropertyPath().toString() so I'm going to add this method, with the possible issues in the implementation note. I know for a fact that if Hibernate Validator ever changes the Path#toString method in any significant way, a lot of code will probably break or start working in unexpected ways. I have seen many projects calling Path#toString to get a decent representation, usually for reporting errors. YOLO.

@sleberknight sleberknight added this to the 4.2.0 milestone Jul 26, 2024
@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Jul 26, 2024
@sleberknight sleberknight self-assigned this Jul 27, 2024
sleberknight added a commit that referenced this issue Jul 27, 2024
* Add asMap, asSingleValuedMap, asMultiValuedMap, and asMultimap
  methods to convert a Set of ConstraintViolation into a JDK or
  Guava Multimap. Overloads provide the ability to customize the
  translation from a property Path to the map key.
* Add pathStringOf, a pure convenience method to eliminate boilerplate.
* Clean up a few minor grammatical errors in javadoc.

Closes #1169
Closes #1170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant