respect-validation/tests/integration/issue-1348.phpt
Henrique Moody fefe905e0b
Include "__root__" when getting message as an array
When converting an object into an array, we exclude the message root
message from it. Since we're using a convention to template those
messages as an array, we could also use the same convention to return
those messages.

While working on it, I noticed that the name "__self__" wasn't
reflecting what that really meant, so I renamed it "__root__" because it
better reflects the meaning of those messages/templates.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-25 22:38:19 +01:00

64 lines
2.7 KiB
PHP

--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Validator;
$cars = [
['manufacturer' => 'Honda', 'model' => 'Accord'],
['manufacturer' => 'Toyota', 'model' => 'Rav4'],
['manufacturer' => 'Ford', 'model' => 'not real'],
['manufacturer' => 'Honda', 'model' => 'not valid'],
];
exceptionMessages(static function () use ($cars): void {
Validator::arrayType()->each(
Validator::oneOf(
Validator::key('manufacturer', Validator::equals('Honda'))
->key('model', Validator::in(['Accord', 'Fit'])),
Validator::key('manufacturer', Validator::equals('Toyota'))
->key('model', Validator::in(['Rav4', 'Camry'])),
Validator::key('manufacturer', Validator::equals('Ford'))
->key('model', Validator::in(['F150', 'Bronco']))
)
)->assert($cars);
});
?>
--EXPECT--
[
'each' => [
'__root__' => 'Each item in `[["manufacturer": "Honda", "model": "Accord"], ["manufacturer": "Toyota", "model": "Rav4"], ["manufacturer": "Fo ... ]` must be valid',
'oneOf.3' => [
'__root__' => 'Only one of these rules must pass for `["manufacturer": "Ford", "model": "not real"]`',
'allOf.1' => [
'__root__' => 'All of the required rules must pass for `["manufacturer": "Ford", "model": "not real"]`',
'manufacturer' => 'manufacturer must equal "Honda"',
'model' => 'model must be in `["Accord", "Fit"]`',
],
'allOf.2' => [
'__root__' => 'All of the required rules must pass for `["manufacturer": "Ford", "model": "not real"]`',
'manufacturer' => 'manufacturer must equal "Toyota"',
'model' => 'model must be in `["Rav4", "Camry"]`',
],
'allOf.3' => 'model must be in `["F150", "Bronco"]`',
],
'oneOf.4' => [
'__root__' => 'Only one of these rules must pass for `["manufacturer": "Honda", "model": "not valid"]`',
'allOf.1' => 'model must be in `["Accord", "Fit"]`',
'allOf.2' => [
'__root__' => 'All of the required rules must pass for `["manufacturer": "Honda", "model": "not valid"]`',
'manufacturer' => 'manufacturer must equal "Toyota"',
'model' => 'model must be in `["Rav4", "Camry"]`',
],
'allOf.3' => [
'__root__' => 'All of the required rules must pass for `["manufacturer": "Honda", "model": "not valid"]`',
'manufacturer' => 'manufacturer must equal "Ford"',
'model' => 'model must be in `["F150", "Bronco"]`',
],
],
],
]