Skip to content

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

@MichelJonkman

Description

@MichelJonkman

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
                )

        )

)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions