Do not create results with siblings in the When rule

When creating a result with a sibling in the When rule, the result
generates an unhelpful message. In most of the use cases of the When
rule, the initial rule ("when") is only helpful in determining which
will be the "real" rule to use.

Those changes will change the When rule not to generate results with
siblings.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-03-25 23:39:12 +01:00
parent fefe905e0b
commit 9322cd6375
No known key found for this signature in database
GPG key ID: 221E9281655813A6
4 changed files with 10 additions and 23 deletions

View file

@ -10,16 +10,10 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Helpers\CanBindEvaluateRule;
use Respect\Validation\Message\Template;
use Respect\Validation\Mode;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use Respect\Validation\Validatable;
#[Template(
'after asserting that',
'after failing to assert that',
)]
final class When extends Standard
{
use CanBindEvaluateRule;
@ -43,15 +37,9 @@ final class When extends Standard
{
$whenResult = $this->bindEvaluate($this->when, $this, $input);
if ($whenResult->isValid) {
$thenResult = $this->bindEvaluate($this->then, $this, $input);
$thisResult = new Result($thenResult->isValid, $input, $this);
return $thenResult->withNextSibling($thisResult->withNextSibling($whenResult));
return $this->bindEvaluate($this->then, $this, $input);
}
$elseResult = $this->bindEvaluate($this->else, $this, $input);
$thisResult = (new Result($elseResult->isValid, $input, $this))->withMode(Mode::NEGATIVE);
return $elseResult->withNextSibling($thisResult->withNextSibling($whenResult));
return $this->bindEvaluate($this->else, $this, $input);
}
}

View file

@ -10,4 +10,4 @@ use Respect\Validation\Validator as v;
exceptionMessage(static fn() => v::when(v::alwaysInvalid(), v::alwaysValid())->check('foo'));
?>
--EXPECT--
"foo" is not valid after failing to assert that "foo" is always invalid
"foo" is not valid

View file

@ -41,18 +41,18 @@ run([
--EXPECT--
When valid use "then"
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
-1 must be positive after asserting that -1 must be an integer number
- -1 must be positive after asserting that -1 must be an integer number
-1 must be positive
- -1 must be positive
[
'positive' => '-1 must be positive after asserting that -1 must be an integer number',
'positive' => '-1 must be positive',
]
When invalid use "else"
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
The value must not be empty after failing to assert that "" must be an integer number
- The value must not be empty after failing to assert that "" must be an integer number
The value must not be empty
- The value must not be empty
[
'notEmpty' => 'The value must not be empty after failing to assert that "" must be an integer number',
'notEmpty' => 'The value must not be empty',
]
When valid use "then" using single template

View file

@ -21,12 +21,11 @@ exceptionMessage(static fn() => v::keyNested('foo.bar')->assert($input));
exceptionMessage(static fn() => v::keyNested('foo.bar', v::stringType())->assert($input));
exceptionMessage(static fn() => v::keyNested('foo.bar.baz', v::notEmpty(), false)->assert($input));
exceptionMessage(static fn() => v::keyNested('foo.bar', v::floatType(), false)->assert($input));
// phpcs:disable Generic.Files.LineLength.TooLong
?>
--EXPECTF--
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
foo must be present after asserting that `["foo.bar.baz": false]` must be an array value
foo must be present
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
No exception was thrown