Skip to content

Validation error output #610

@bouledepate

Description

@bouledepate

Hi everyone again :)

My input data array contains dot-separated keys. According to the documentation, it is recommended to escape such keys so as not to break them into subcollections. This is good, but when I get the list of errors via getErrorMessagesIndexedByPath I don't want to see the escaping. I looked at the code and saw what can be done:

The 1st way is to move the parameter to a separate flag.

    public function getErrorMessagesIndexedByPath(string $separator = '.', bool $escape = true): array
    {
        $errors = [];
        foreach ($this->errors as $error) {
            $stringValuePath = implode($separator, $error->getValuePath($escape));
            $errors[$stringValuePath][] = $error->getMessage();
        }
        return $errors;
    }

2nd way - just remove it.

    public function getErrorMessagesIndexedByPath(string $separator = '.'): array
    {
        $errors = [];
        foreach ($this->errors as $error) {
            $stringValuePath = implode($separator, $error->getValuePath());
            $errors[$stringValuePath][] = $error->getMessage();
        }
        return $errors;
    }

Example. If the parameter is set to "true", then I get this output.

    "error": {
        "code": 790,
        "title": "Validation error!",
        "detail": {
            "filters.amount\\.value": [
                "Value must be a number."
            ]
        }
    }

Otherwise like this.

"error": {
        "code": 790,
        "title": "Validation error!",
        "detail": {
            "filters.amount.value": [
                "Value must be a number."
            ]
        }
    }

Do you agree that the second conclusion is much better and more convenient? Thank you!

What steps will reproduce the problem?

  1. Make validation of data with keys 'amount.value' for example.
  2. Request the error messages by 'getErrorMessagesIndexedByPath';

What is the expected result?

Paths without escaping.

What do you get instead?

Paths with escaping.

Additional info

Q A
Version 1.0.1
PHP version 8.2.6
Operating system Linux Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions