Skip to content

Commit

Permalink
Fix property name usage in error messages of rules in Nested rule (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Nov 7, 2023
1 parent f938670 commit 010ac44
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ name: rector
jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
secrets:
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
['8.2']
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Chg #624: Fix meaning of error message in `OneOf` rule (@arogachev)
- Chg #625: Improve meaning and use pluralization in error message for `OneOf` and `AtLeast` rules (@arogachev)
- Chg #626: Disallow `$min` greater than amount of `$attributes` in `AtLeast` configuration (@arogachev)
- Bug #632: Fix property name usage in error messages of rules in `Nested` rule (@vjik)

## 1.1.0 April 06, 2023

Expand Down
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/Rule/EmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function validate(mixed $value, object $rule, ValidationContext $context)
if ($rule->isIdnEnabled()) {
$matches['local'] = idn_to_ascii($matches['local']);
$matches['domain'] = idn_to_ascii($matches['domain']);
$value = implode([
$value = implode('', [
$matches['name'],
$matches['open'],
$matches['local'],
Expand Down
32 changes: 14 additions & 18 deletions src/Rule/NestedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ public function validate(mixed $value, object $rule, ValidationContext $context)

foreach ($rule->getRules() as $valuePath => $rules) {
if ($rule->isPropertyPathRequired() && !ArrayHelper::pathExists($data, $valuePath)) {
if (is_int($valuePath)) {
$valuePathList = [$valuePath];
} else {
/** @var list<string> $valuePathList */
$valuePathList = StringHelper::parsePath($valuePath);
}
$valuePathList = is_int($valuePath)
? [$valuePath]
: StringHelper::parsePath($valuePath);

$compoundResult->addError(
$rule->getNoPropertyPathMessage(),
Expand All @@ -82,25 +79,24 @@ public function validate(mixed $value, object $rule, ValidationContext $context)
continue;
}

/** @var mixed $validatedValue */
$validatedValue = ArrayHelper::getValueByPath($data, $valuePath);

$itemResult = $context->validate($validatedValue, $rules);
if (is_int($valuePath)) {
$itemResult = $context->validate($validatedValue, $rules);
} else {
$valuePathList = StringHelper::parsePath($valuePath);
$attribute = (string) end($valuePathList);
$itemResult = $context->validate([$attribute => $validatedValue], [$attribute => $rules]);
}

if ($itemResult->isValid()) {
continue;
}

foreach ($itemResult->getErrors() as $error) {
if (is_int($valuePath)) {
$valuePathList = [$valuePath];
} else {
/** @var list<string> $valuePathList */
$valuePathList = StringHelper::parsePath($valuePath);
}

if (!empty($valuePathList)) {
array_push($valuePathList, ...$error->getValuePath());
}
$valuePathList = is_int($valuePath)
? [$valuePath, ...$error->getValuePath()]
: [...StringHelper::parsePath($valuePath), ...array_slice($error->getValuePath(), 1)];

$compoundResult->addError($error->getMessage(), $error->getParameters(), $valuePathList);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Rule/NestedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,21 @@ public function hasAttribute(string $attribute): bool
'level1.level2.level3.name' => ['This value must contain at least 5 characters.'],
],
],
'error messages with attributes in nested structure' => [
[
'user' => [
'name' => '',
],
],
new Nested([
'user' => [
'name' => new Required(message: '{attribute} is required.'),
],
]),
[
'user.name' => ['name is required.'],
],
],
];
}

Expand Down

0 comments on commit 010ac44

Please sign in to comment.