respect-validation/tests/integration/get_messages_should_include_all_validation_messages_in_a_chain.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

35 lines
914 B
PHP

--FILE--
<?php
declare(strict_types=1);
date_default_timezone_set('UTC');
require 'vendor/autoload.php';
use Respect\Validation\Validator;
exceptionMessages(static function (): void {
$input = [
'username' => 'u',
'birthdate' => 'Not a date',
'password' => '',
];
Validator::create()
->key('username', Validator::lengthBetween(2, 32))
->key('birthdate', Validator::dateTime())
->key('password', Validator::notEmpty())
->key('email', Validator::email())
->assert($input);
});
?>
--EXPECT--
[
'__root__' => 'All of the required rules must pass for `["username": "u", "birthdate": "Not a date", "password": ""]`',
'username' => 'The length of username must be between 2 and 32',
'birthdate' => 'birthdate must be a valid date/time',
'password' => 'password must not be empty',
'email' => 'email must be present',
]