respect-validation/tests/feature/Issues/Issue1289Test.php
Henrique Moody 5b00d69766
Update how we handle templates
Currently, the templates that a user provides when running `assert()`
can significantly impact how the message is displayed. Because of this,
the formatters become complex as they all need to handle similar
conditions to format results.

This commit changes this behaviour, letting only the
`InterpolationRenderer` handle the templates. This makes the code
simpler and allows people to use the `InterpolationRenderer` directly,
without needing to figure out how to handle templates. Thinking about it
further, I believe handling templates is a concern for the `Renderer`
anyway, and this will open the way to other improvements using the
renderer.

I also removed the exception that is thrown when the template is not a
string, because I think that after validation has failed, we should not
throw any other exceptions, as that could cause unexpected errors for
users.
2025-12-22 14:05:55 +01:00

67 lines
2.2 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
use Respect\Validation\Rules\ArrayType;
use Respect\Validation\Rules\BoolType;
use Respect\Validation\Rules\Each;
use Respect\Validation\Rules\KeyOptional;
use Respect\Validation\Rules\OneOf;
use Respect\Validation\Rules\StringType;
use Respect\Validation\Rules\StringVal;
use Respect\Validation\Validator;
test('https://github.com/Respect/Validation/issues/1289', catchAll(
fn() => Validator::create(
new Each(
Validator::create(
new KeyOptional(
'default',
new OneOf(
new StringType(),
new BoolType(),
),
),
new KeyOptional(
'description',
new StringVal(),
),
new KeyOptional(
'children',
new ArrayType(),
),
),
),
)
->assert([
[
'default' => 2,
'description' => [],
'children' => ['nope'],
],
]),
fn(string $message, string $fullMessage, array $messages) => expect()
// Currently we're getting `.0.default.default` instead of `.0.default`
// that is happening the result has multiple children, and they're not inheriting the path from their
// parent, but instead, the parent is duplicating the children's path.
->and($message)->toBe('`.0.default` must be a string')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- `.0` must pass the rules
- `.0.default` must pass one of the rules
- `.0.default` must be a string
- `.0.default` must be a boolean
- `.0.description` must be a string value
FULL_MESSAGE)
->and($messages)->toBe([
0 => [
'__root__' => '`.0` must pass the rules',
'default' => '`.0.default` must be a boolean',
'description' => '`.0.description` must be a string value',
],
]),
));