-
Notifications
You must be signed in to change notification settings - Fork 777
Open
Description
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
)
)
)