Skip to content

Fix: WhenMissing doesn't work in Nested rules. #771

Merged
vjik merged 13 commits into
yiisoft:masterfrom
Enjoyzz:750-issue
Jun 23, 2025
Merged

Fix: WhenMissing doesn't work in Nested rules. #771
vjik merged 13 commits into
yiisoft:masterfrom
Enjoyzz:750-issue

Conversation

@Enjoyzz

@Enjoyzz Enjoyzz commented Jun 1, 2025

Copy link
Copy Markdown
Contributor
Q A
Is bugfix? ✔️
New feature?
Breaks BC?

Fix #750

@codecov

codecov Bot commented Jun 1, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.18%. Comparing base (fe63447) to head (57b8c9f).
Report is 79 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #771      +/-   ##
============================================
+ Coverage     94.40%   96.18%   +1.78%     
- Complexity      953     1037      +84     
============================================
  Files           108      122      +14     
  Lines          3018     3279     +261     
============================================
+ Hits           2849     3154     +305     
+ Misses          169      125      -44     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Enjoyzz

Enjoyzz commented Jun 1, 2025

Copy link
Copy Markdown
Contributor Author

This PR also fixes this issue #565. I can add a test from this issue. or maybe add it as a separate PR?

@samdark samdark requested review from Copilot and vjik June 3, 2025 17:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Fix a bug where the WhenMissing condition was not properly handled in Nested rules.

  • Added several tests in NestedTest.php to validate WhenMissing behavior under different scenarios.
  • Updated NestedHandler.php to pass fallback data when validated values are missing, ensuring correct handling of nested rules.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/Rule/NestedTest.php Added tests covering WhenMissing in nested rules.
src/Rule/NestedHandler.php Modified value validation to address missing rule values.
Comments suppressed due to low confidence (1)

tests/Rule/NestedTest.php:1264

  • [nitpick] The error message 'Value not passed.' might benefit from clearer wording, such as 'Expected value is missing.', to enhance clarity for end users.
                ['value' => ['Value not passed.']],

@Enjoyzz

Enjoyzz commented Jun 5, 2025

Copy link
Copy Markdown
Contributor Author

Doesn't work with integer valuePath, needs to be further improved

@Enjoyzz

Enjoyzz commented Jun 5, 2025

Copy link
Copy Markdown
Contributor Author

Doesn't work with integer valuePath, needs to be further improved

Currently, WhenMissing does not support rules with integer-based paths because the property is always set to null, causing the isPropertyMissing check to always return false. 😥

@yiisoft yiisoft deleted a comment from Copilot AI Jun 5, 2025
Comment thread src/Rule/NestedHandler.php Outdated
@vjik

vjik commented Jun 5, 2025

Copy link
Copy Markdown
Member

Currently, WhenMissing does not support rules with integer-based paths because the property is always set to null, causing the isPropertyMissing check to always return false. 😥

Yes, also rules with integer keys applies to whole value, not to property.

Enjoyzz added 2 commits June 5, 2025 15:11
Replaces null checks with MissingValue instance detection for more reliable validation of optional array paths.
@Enjoyzz Enjoyzz requested a review from vjik June 20, 2025 15:52

@vjik vjik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add line to changelog

Comment thread src/Rule/NestedHandler.php Outdated
$property = end($valuePathList);
$itemResult = $context->validate([$property => $validatedValue], [$property => $rules]);
$itemResult = $context->validate(
ArrayHelper::pathExists($data, $valuePath) ? [$property => $validatedValue] : [],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ArrayHelper::pathExists($data, $valuePath) ? [$property => $validatedValue] : [],
ArrayHelper::keyExists($data, $valuePath) ? [$property => $validatedValue] : [],

$valuePath is already an array of keys, so using keyExists() is a bit more efficient.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes! Only then should check not $valuePath, but $valuePathList. Thank you, that's more correct. 👍

@vjik vjik merged commit d604a89 into yiisoft:master Jun 23, 2025
31 of 32 checks passed
@vjik

vjik commented Jun 23, 2025

Copy link
Copy Markdown
Member

@Enjoyzz thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WhenMissing doesn't work in Nested rules.

3 participants