Respect / Validation

The most awesome validation engine ever created for PHP
https://respect-validation.readthedocs.io
MIT License
5.76k stars 774 forks source link

Exceptions don't have reference to the failing rule #1353

Closed dakujem closed 1 year ago

dakujem commented 3 years ago

The exceptions don't have any reference to the failing rule. This makes working with the exceptions less usable.

The $params array could be used for this, easily implementable in Factory::exception method.

alganet commented 1 year ago

Can you provide an example that showcases the problem you're having?

dakujem commented 1 year ago

Sorry, I'm not working on a code base using this anymore. I used symfony in the end.

What I needed at the time was a reference to the rule that triggers the exception.

In an API endpoint, you want to tell the user that a specific rule is failing, example fragment of a response to such a failing request:

{
  message: "E-mail not valid",
  rule: "user.email"
}

In the case above the rule is user.email, in other words, email rule nested inside a user group validator.

alganet commented 1 year ago

You can definitely perform ->getId() on any instance of ValidationException and get its reference. Also, ->getChildren() is available on NestedValidationException instances so you can walk the tree structure.

The dot notation reference, however, is something that we can't expose automatically, as it might cause ambiguity. A rule might as well be a user.allOf.email or user.oneOf.keySet.email and not what the user expected it to be. Validating plain structures is only one of the many use cases of this library.

Your last example is not enough for me to provide solution on how to do it. I would need a snippet of the declaration rules and some inputs to test it with.

I'm sorry to hear that you chosen another library instead, but I totally get it. During the time this issue was opened, we were lacking maintenance personnel and couldn't provide the support you needed.

If you ever decide to come back and try Respect\Validation again, reopen this or another issue and we'll be here to support your needs 🐼