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

Explore the idea of attaching custom details to ConstraintViolation #34

Open
imulab opened this issue Jul 11, 2024 · 2 comments
Open

Explore the idea of attaching custom details to ConstraintViolation #34

imulab opened this issue Jul 11, 2024 · 2 comments
Labels
core The core of the library is involved DSL API design and its DSL enhancement New feature or request

Comments

@imulab
Copy link

imulab commented Jul 11, 2024

The ConstraintViolation has path and message, which is sufficient for most cases.

But it is not the most straightforward design if I need to pinpoint on the exact constraint being violated and act on it. For instance, to pick out a particular constraint violation and throw an alternate Exception instead of the generic Exception.

Currently, to workaround this limitation, there are possibly two ways:

  • Override the path using the withPath { absolute("some_identifier") } DSL
  • Override the message using the otherwise { "some_identifier" } DSL

The first workaround loses the path in a meaningful way. And the second workaround loses human readable messages.

Is it possible to add a third field of metadata: Map<String, Any> to ConstraintViolation, so that user can freely attach whatever metadata they need to the violation instance.

Possible DSL design:

val validate = Validator<Data> {
   fieldOne.apply {
      shouldNotBeEmpty()
      constraint { it.meetsCustomCondition() } withMetadata { mapOf("key" to "customErrorKey1") }
   }
}

Then, when analyzing the ConstraintViolation:

fun handleViolation(cv: ConstraintViolation) {
   if (cv.metadata["key"] == "customErrorKey1")
      throw CustomError(cv.message)
  throw FallbackError(cv.message)
}
@nesk
Copy link
Owner

nesk commented Jul 24, 2024

This is clearly an issue I want to address!

I think your idea of metadata is good. We could even add a dedicated code property to ConstraintViolation, because this is definitely something a lot of people could use.

Let me think about it, but I love the idea :)

@nesk nesk added enhancement New feature or request core The core of the library is involved constraints Related to the built-in constraints DSL API design and its DSL and removed constraints Related to the built-in constraints labels Jul 24, 2024
@ageorgousakisn
Copy link

I agree that we need more information in ConstraintViolation.

In valiktor the ConstraintViolation had all the information of the violation, such as the constraint with a message key and message parameters and the value that was not validated. This helps a lot when you want to sent a localised message or you want to send in JSON all the information to the UI and construct the error message there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core The core of the library is involved DSL API design and its DSL enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

3 participants