mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
The `findByPath()` method was failing to return results when using nested dot-notation paths such as `user.email` or `items.1`. However, it’s returning `null` instead of the expected result in some cases. The root cause was a mismatch between how paths are stored vs searched: - Storage: Validators like Key and Each create results where the path is stored as a linked list. For `user.email`, the "email" result has `path="email"` with `parent="user"`. - Search (old): The method expected a tree structure where it would find a child with `path="user"`, then search that child for `path="email"`. But no child had `path="user"` - only "email" (with "user" as its parent). The fix computes each result's full path by walking up the parent chain and compares it against the search path. Also converts numeric strings to integers when parsing paths (e.g., `items.1` → `['items', 1]`) since array indices are stored as integers. While working on this fix, I also realised that to expose the result's status, it’s best to use `hasFailed()` instead of `isValid()` in `ResultQuery`, since users will mostly use results when validation failed, not when it passed. Assisted-by: Claude Code (Opus 4.5)
77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
test('findByPath with nested keys', function (): void {
|
|
$validator = v::key('user', v::key('email', v::email()))
|
|
->key('items', v::each(v::positive()));
|
|
|
|
$result = $validator->validate([
|
|
'user' => ['email' => 'invalid'],
|
|
'items' => [10, -5, 20],
|
|
]);
|
|
|
|
$emailResult = $result->findByPath('user.email');
|
|
expect()
|
|
->and($emailResult)->not->toBeNull()
|
|
->and($emailResult?->hasFailed())->toBeTrue()
|
|
->and($emailResult?->getMessage())->toBe('`.user.email` must be a valid email address');
|
|
});
|
|
|
|
test('findByPath with array index', function (): void {
|
|
$validator = v::key('items', v::each(v::positive()));
|
|
|
|
$result = $validator->validate([
|
|
'items' => [10, -5, 20],
|
|
]);
|
|
|
|
$itemResult = $result->findByPath('items.1');
|
|
expect()
|
|
->and($itemResult)->not->toBeNull()
|
|
->and($itemResult?->hasFailed())->toBeTrue()
|
|
->and($itemResult?->getMessage())->toBe('`.items.1` must be a positive number');
|
|
});
|
|
|
|
test('findByName with named validator', function (): void {
|
|
$result = v::named('User Email', v::email())->validate('bad');
|
|
|
|
$namedResult = $result->findByName('User Email');
|
|
expect()
|
|
->and($namedResult)->not->toBeNull()
|
|
->and($namedResult?->hasFailed())->toBeTrue()
|
|
->and($namedResult?->getMessage())->toBe('User Email must be a valid email address');
|
|
});
|
|
|
|
test('findById with validator id', function (): void {
|
|
$result = v::stringType()->email()->validate(123);
|
|
|
|
$stringResult = $result->findById('stringType');
|
|
expect()
|
|
->and($stringResult)->not->toBeNull()
|
|
->and($stringResult?->hasFailed())->toBeTrue()
|
|
->and($stringResult?->getMessage())->toBe('123 must be a string');
|
|
});
|
|
|
|
test('findByPath returns null when path not found', function (): void {
|
|
$result = v::key('user', v::email())->validate(['user' => 'bad']);
|
|
|
|
expect($result->findByPath('nonexistent'))->toBeNull();
|
|
});
|
|
|
|
test('findByName returns null when name not found', function (): void {
|
|
$result = v::email()->validate('bad');
|
|
|
|
expect($result->findByName('Nonexistent'))->toBeNull();
|
|
});
|
|
|
|
test('findById returns null when id not found', function (): void {
|
|
$result = v::email()->validate('bad');
|
|
|
|
expect($result->findById('nonexistent'))->toBeNull();
|
|
});
|