respect-validation/tests/integration/get_messages_with_replacements.phpt
Henrique Moody 2ae1df177a
Allow to customise messages while asserting
Because we now have a single "assert()" method, we have more freedom to
add more customizations to it. This specific one is handy if someone
wants to use the library to validate but wants to use their own
exceptions.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-26 15:04:04 +01:00

71 lines
2.2 KiB
PHP

--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
exceptionMessages(
static function (): void {
v::create()
->key(
'mysql',
v::create()
->key('host', v::stringType())
->key('user', v::stringType())
->key('password', v::stringType())
->key('schema', v::stringType())
)
->key(
'postgresql',
v::create()
->key('host', v::stringType())
->key('user', v::stringType())
->key('password', v::stringType())
->key('schema', v::stringType())
)
->assert(
[
'mysql' => [
'host' => 42,
'schema' => 42,
],
'postgresql' => [
'user' => 42,
'password' => 42,
],
],
[
'mysql' => [
'user' => 'Value should be a MySQL username',
'host' => '`{{name}}` should be a MySQL host',
],
'postgresql' => [
'schema' => 'You must provide a valid PostgreSQL schema',
],
]
);
}
);
?>
--EXPECT--
[
'__root__' => 'All of the required rules must pass for `["mysql": ["host": 42, "schema": 42], "postgresql": ["user": 42, "password": 42]]`',
'mysql' => [
'__root__' => 'All of the required rules must pass for mysql',
'host' => '`host` should be a MySQL host',
'user' => 'Value should be a MySQL username',
'password' => 'password must be present',
'schema' => 'schema must be of type string',
],
'postgresql' => [
'__root__' => 'All of the required rules must pass for postgresql',
'host' => 'host must be present',
'user' => 'user must be of type string',
'password' => 'password must be of type string',
'schema' => 'You must provide a valid PostgreSQL schema',
],
]