Skip to content

[WIP] Fix bug with incorrect errors for nested fields#271

Closed
perrin4869 wants to merge 2 commits into
final-form:mainfrom
perrin4869:nested-field-level-validation-bugfix
Closed

[WIP] Fix bug with incorrect errors for nested fields#271
perrin4869 wants to merge 2 commits into
final-form:mainfrom
perrin4869:nested-field-level-validation-bugfix

Conversation

@perrin4869

Copy link
Copy Markdown

This is a work in progress, but I would appreciate your input.
This PR targets a bug where you put a validator on the root of a nested field. Let's say you want to ensure a range is valid. Assert that range.min < range.max by setting a validator on range. This is required, if for example, you have an array of ranges (as in our case).
The error message gets converted to an object with keys.
This PR introduces a failing test, if you have any preference on how to fix this issue, please tell me. Otherwise I can come up with a solution.
As an aside, focusing on nested fields can cause weird edge-cases too, but that's a task for a separate PR :)
Thanks!

@perrin4869
perrin4869 force-pushed the nested-field-level-validation-bugfix branch from 9ea1b69 to ebd3318 Compare August 27, 2019 16:34
@codecov

codecov Bot commented Aug 29, 2019

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@4476c45). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff            @@
##             main      #271   +/-   ##
========================================
  Coverage        ?   100.00%           
========================================
  Files           ?        12           
  Lines           ?       557           
  Branches        ?       116           
========================================
  Hits            ?       557           
  Misses          ?         0           
  Partials        ?         0           

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@SpOOnman

Copy link
Copy Markdown

I can confirm this bug also affects me. I didn't test your solution but it looks like it can solve this issue.

@erikras-richard-agent erikras-richard-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review by Richard (Team Lead)

WIP fix for incorrect errors with nested fields. Marked as experimental by the author. Will evaluate the approach but given WIP status, this is lower priority for merge consideration.

@erikras-richard-agent

Copy link
Copy Markdown
Contributor

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Feb 13, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Feb 13, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@erikras-richard-agent has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 14 minutes and 56 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Comment @coderabbitai help to get the list of available commands and usage tips.

@erikras-richard-agent

Copy link
Copy Markdown
Contributor

Thank you for identifying this bug and the fix approach, @perrin4869!

The bug you found still exists in the current TypeScript codebase. We'll be porting your fix (typeof current === 'string' check in setIn) to the TS code. Your test case for nested field validation errors was very helpful.

Closing this PR since it targets the old Flow code, but the fix will be applied. 🙏

erikras-dinesh-agent pushed a commit that referenced this pull request Feb 13, 2026
Fixes #271

Problem:
When setIn tries to remove a nested field key from a parent that contains
a string error message (e.g., 'Invalid range' on 'range' when removing
'range.min'), the destructuring operation converts the string to an object
with numeric keys ({ '0': 'I', '1': 'n', ... }).

This breaks nested field validation where a validator on a parent field
(like 'range') returns a string error message, but child fields ('range.min',
'range.max') also exist.

Root Cause:
Line 61 in setIn.ts: `const { [key]: _removed, ...final } = current as any`
When `current` is a string, JavaScript's destructuring coerces it to an
object, treating each character as a numbered property.

Solution:
Added a `typeof current === 'string'` check before the destructure
operation. Strings cannot have custom properties anyway, so we return
the string as-is instead of trying to remove keys from it.

Changes:
- src/structure/setIn.ts:
  - Added string type check before destructuring (lines 61-64)
  - Return string unchanged when trying to remove properties
- src/FinalForm.validating.test.ts:
  - Added test case for nested field-level validation errors
  - Verifies 'Invalid range' stays a string, not converted to object

Impact:
✅ Nested field validators work correctly
✅ String error messages preserved on parent fields
✅ No breaking changes
✅ All existing tests pass
erikras pushed a commit that referenced this pull request Feb 13, 2026
…527)

Fixes #271

Problem:
When setIn tries to remove a nested field key from a parent that contains
a string error message (e.g., 'Invalid range' on 'range' when removing
'range.min'), the destructuring operation converts the string to an object
with numeric keys ({ '0': 'I', '1': 'n', ... }).

This breaks nested field validation where a validator on a parent field
(like 'range') returns a string error message, but child fields ('range.min',
'range.max') also exist.

Root Cause:
Line 61 in setIn.ts: `const { [key]: _removed, ...final } = current as any`
When `current` is a string, JavaScript's destructuring coerces it to an
object, treating each character as a numbered property.

Solution:
Added a `typeof current === 'string'` check before the destructure
operation. Strings cannot have custom properties anyway, so we return
the string as-is instead of trying to remove keys from it.

Changes:
- src/structure/setIn.ts:
  - Added string type check before destructuring (lines 61-64)
  - Return string unchanged when trying to remove properties
- src/FinalForm.validating.test.ts:
  - Added test case for nested field-level validation errors
  - Verifies 'Invalid range' stays a string, not converted to object

Impact:
✅ Nested field validators work correctly
✅ String error messages preserved on parent fields
✅ No breaking changes
✅ All existing tests pass

Co-authored-by: Erik Rasmussen <[email protected]>
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.

3 participants