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

Getting validation error messages without truncating the message #1356

Closed ThisuraThejith closed 1 year ago

ThisuraThejith commented 3 years ago

Hi,

I'm trying to improve the validation error messages where the messages are coming truncated when there are more than five filter parameters are defined in an API

Sample error message:

{
    "title": "Invalid data",
    "details": "- All of the required rules must pass for `{ \"sa\": \"kjkhkjhk\" }`\n  - Must have keys `{ \"emp_number\", \"include\", \"includeSelf\", \"fields\", \"filter\", ... }`"
}

In the above example you can see the error message is truncated by '...' at the end without showing the rest of the parameters.

This happens because of a code in the library.

There is a final class called 'ClusterStringifier'. In the ClusterStringifier.php file there is a method like following.

public static function createDefault(): self
    {
        $quoter = new CodeQuoter();

        $stringifier = new self();
        $stringifier->setStringifiers([
            new TraversableStringifier($stringifier, $quoter),
            new DateTimeStringifier($stringifier, $quoter, 'c'),
            new ThrowableStringifier($stringifier, $quoter),
            new StringableObjectStringifier($stringifier),
            new JsonSerializableStringifier($stringifier, $quoter),
            new ObjectStringifier($stringifier, $quoter),
            new ArrayStringifier($stringifier, $quoter, 3, 5),
            new InfiniteStringifier($quoter),
            new NanStringifier($quoter),
            new ResourceStringifier($quoter),
            new BoolStringifier($quoter),
            new NullStringifier($quoter),
            new JsonParsableStringifier(),
        ]);

        return $stringifier;
    }

In here there is an instance of 'ArrayStringifier' initiated by passing the fourth parameter (which is the displaying items count) as 5. Because of that this is happening.

Is there anyway to customize this and show the entire set of parameter names in the error message?

Any help would be highly appreciated.

alganet commented 1 year ago

You can override the whole stringifyer using:

Factory::setDefaultInstance(
    (new Factory())->withParameterStringifier(new MyCustomStringifier())
);

You can also inspect each message individually, see this comment.

If that doesn't help you, feel free to reopen this!