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

Unclear error messages for each() when using getMessages() #1469

Open
MichelJonkman opened this issue Oct 28, 2024 · 1 comment
Open

Unclear error messages for each() when using getMessages() #1469

MichelJonkman opened this issue Oct 28, 2024 · 1 comment

Comments

@MichelJonkman
Copy link

MichelJonkman commented Oct 28, 2024

Maybe I'm doing something wrong but the error messages for the each rule are very unclear when using getMessages(). getFullMessage() has a much better result but seeing as I need to return any errors in JSON format it doesn't fit my needs.

My code:

$data = [
    'order_items' => [
        [
            'product_title' => 'test',
            'quantity' => 'test',
        ],
        [
            'product_title2' => 'test'
        ]
    ]
];

$validator = V::arrayVal()->keySet(
    V::key('order_items', V::arrayVal()->each(V::keySet(
        V::key('product_title', V::stringVal()->notEmpty()),
        V::key('quantity', V::intVal()->notEmpty()),
    ))->notEmpty()),
);

try {
    $validator->assert($data);
} catch (NestedValidationException $exception) {
    echo '<pre>';
    print_r($exception->getMessages());
    echo '</pre>';
    exit;
}

Resulting error messages:

Array
(
    [allOf] => Array
        (
            [validator.0] => All of the required rules must pass for `{ "product_title": "test", "quantity": "test" }`
            [validator.1] => All of the required rules must pass for `{ "product_title2": "test" }`
        )

)

The desired result would look something like this

Array
(
    [order_items] => Array
        (
            [item_1] => Array
                (
                    [product_title] => product_title must be present
                )

        )

)
@MichelJonkman
Copy link
Author

This seems to be true for when() as well, I'm guessing nested rules just don't work very well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant