respect-validation/tests/feature/Issues/Issue1334Test.php
Henrique Moody 562d98d805
Refactor the NotEmpty rule
Since we have the ability to use `not` as a prefix, having rules that
validate negative behaviour makes them a bit inflexible, verbose, and
harder to understand.

This commit will refactor the `NotEmpty`, and rename it to `Falsy`. It
will no longer trim strings, because Blank does a much better job at it;
it only simulates the behaviour of PHP’s native `empty()` function.

Because `Falsy`, `Blank`, and `Undef` have similar behaviour, I created
a page to demonstrate the difference and show when the user should use
one or the other.

Assisted-by: Cursor (claude-4.5-opus-high)
2025-12-29 12:48:35 +01:00

50 lines
2.1 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
test('https://github.com/Respect/Validation/issues/1334', catchAll(
function (): void {
v::notBlank()->iterableType()->each(
v::key('street', v::stringType()->notBlank())
->key('region', v::stringType()->notBlank())
->key('country', v::stringType()->notBlank())
->keyOptional('other', v::nullOr(v::notBlank()->stringType())),
)->assert(
[
['region' => 'Oregon', 'country' => 'USA', 'other' => 123],
['street' => '', 'region' => 'Oregon', 'country' => 'USA'],
['street' => 123, 'region' => 'Oregon', 'country' => 'USA'],
],
);
},
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.0.street` must be present')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- Each item in `[["region": "Oregon", "country": "USA", "other": 123], ["street": "", "region": "Oregon", "country": "USA"], ["s ... ]` must be valid
- `.0` must pass the rules
- `.0.street` must be present
- `.0.other` must pass the rules
- `.0.other` must be a string or must be null
- `.1` must pass the rules
- `.1.street` must not be blank
- `.2` must pass the rules
- `.2.street` must be a string
FULL_MESSAGE)
->and($messages)->toBe([
'each' => [
'__root__' => 'Each item in `[["region": "Oregon", "country": "USA", "other": 123], ["street": "", "region": "Oregon", "country": "USA"], ["s ... ]` must be valid',
0 => [
'__root__' => '`.0` must pass the rules',
'street' => '`.0.street` must be present',
'other' => '`.0.other` must be a string or must be null',
],
1 => '`.1.street` must not be blank',
2 => '`.2.street` must be a string',
],
]),
));