Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Dec 26, 2024
1 parent cdc8b7e commit 4b79cec
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
6 changes: 1 addition & 5 deletions library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,8 @@ public function withChildren(Result ...$children): self

public function withName(string $name): self
{
if ($this->unchangeableName) {
return $this;
}

return $this->clone(
name: $name,
name: $this->name ?? $name,
adjacent: $this->adjacent?->withName($name),
unchangeableName: true,
);
Expand Down
9 changes: 7 additions & 2 deletions library/Rules/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ public function getKey(): int|string

public function evaluate(mixed $input): Result
{
$name = (string) $this->key;
if ($this->rule instanceof Named) {
$name = $this->rule->getName();
}

$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
if (!$keyExistsResult->isValid) {
return $keyExistsResult;
return $keyExistsResult->withName($name);
}

return $this->rule
->evaluate($input[$this->key])
->withSoftName($keyExistsResult->name)
->withName($name)
->withUnchangeableId((string) $this->key);
}
}
2 changes: 1 addition & 1 deletion library/Rules/KeyExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function evaluate(mixed $input): Result
{
$name = (string) $this->key;

return (new Result($this->hasKey($input), $input, $this, name: $name, id: $name));
return (new Result($this->hasKey($input), $input, $this, name: $name, id: $name, unchangeableName: true));
}

private function hasKey(mixed $input): bool
Expand Down
25 changes: 8 additions & 17 deletions library/Rules/Named.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Respect\Validation\Result;
use Respect\Validation\Rule;
use Respect\Validation\Rules\Core\Binder;
use Respect\Validation\Rules\Core\Composite;
use Respect\Validation\Rules\Core\Wrapper;

use function array_map;
Expand All @@ -34,28 +35,18 @@ public function getName(): string

public function evaluate(mixed $input): Result
{
$result = (new Binder($this, $this->rule))->evaluate($input);
$name = $result->name ?? $this->name;
if ($result->unchangeableName === false) {
$name = $this->name;
}

$children = array_map(
fn (Result $child) => $this->enrichResult($child),
array_filter(
$result->children,
fn (Result $child) => !$child->rule instanceof Key
)
);

return $result->withName($name)->withChildren(...$children);
return $this->enrichResult((new Binder($this, $this->rule))->evaluate($input));
}

private function enrichResult(Result $result): Result
{
$name = $result->name ?? $this->name;
if ($result->unchangeableName === false) {
$name = $this->name;
// if ($result->unchangeableName === false) {
// $name = $this->name;
// }

if ($result->rule instanceof Composite) {
return $result->withName($name);
}

return $result
Expand Down
7 changes: 7 additions & 0 deletions tests/feature/Rules/NamedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
['vegetable' => 'Broccoli must be present'],
));

test('With property that does not exist #2', expectAll(
fn() => v::key('vegetable', v::named(v::stringType(), 'Broccoli'))->assert((object) []),
'Broccoli must be present',
'- Broccoli must be present',
['vegetable' => 'Broccoli must be present'],
));

test('With key that fails validation', expectAll(
fn() => v::named(v::key('vegetable', v::stringType()), 'Artichoke')->assert(['vegetable' => 12]),
'Artichoke must be a string',
Expand Down

0 comments on commit 4b79cec

Please sign in to comment.