-
-
Notifications
You must be signed in to change notification settings - Fork 444
fix(form-core): fix broken sync/async validation logic #1370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(form-core): fix broken sync/async validation logic #1370
Conversation
View your CI Pipeline Execution ↗ for commit 35d3c97.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1370 +/- ##
==========================================
+ Coverage 88.70% 88.83% +0.12%
==========================================
Files 31 31
Lines 1364 1379 +15
Branches 346 347 +1
==========================================
+ Hits 1210 1225 +15
Misses 137 137
Partials 17 17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
It would be great to stop saving the state of fields at the form level. This leads to duplication and divergence of states. For example, how does it do |
Correct, that's why this PR removes the cumulativeErrorsMap which is at the form level and moves the error source to the fieldMeta. This way when the fieldMeta is shifted, moved, deleted, etc... the source remains valid. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call on moving this to the field meta, I like it!
@Balastrong thanks for the review! I believe I've resolved all of the raised comments. Additionally, I created an equivalent function for determining the new error/error source at the field level validator functions (sync/async). This way its consistent for both form level/field level! Additionally added tests for the new function and updated existing tests to ensure we cover the missed case you mentioned. |
Was manually testing #1336 and realized that for that issue the error comes from no optional chain operator being used when checking for the errorMapKey inside the errorMap dynamically: if (field.state.meta.errorMap[errorMapKey] !== newErrorValue) Updated it to: if (field.state.meta.errorMap?.[errorMapKey] !== newErrorValue) Had to add for both: Not sure why TS doesn't infer that the value could be undefined ¯\(ツ)/¯ Edit: Added it to two other spots as well where we access submit error key: this.state.errorMap?.[submitErrKey] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's just one tiny leftover from your tests probably, everything else looks good! :D
Speaking of the optional errorMap that would be an interesting investigation if you want, to validate if we should fix it on a type level (if we accept it might be undefined) or actually on the code to prevent it being undefined at all times. That's for a new PR anyway :)
829e749
to
6571c24
Compare
This commit updates sync and async validation state to shift with FieldMeta Fixes "Cannot read properties of undefined" when removing array field TanStack#1323
d730b22
to
35d3c97
Compare
Updated! Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Previously we were using a cumulative field errors map maintained at the form api level.
This PR removes that data structure in favor of a errorSourceMap data structure that lives within the FieldMeta for each field. This allows shifting/removing/deleting fields to automatically keep the errorSourceMap in sync.
This will fix #1323 and prevent other bugs from being raised around validation not working after other array actions like removing, shifting, swapping, moving, etc...
Async validation & sync validation now is fully synced and thoroughly tested with the new errorSourceMap. This reduces the complexity of using the cumulative errors map significantly and fixes all the issues with validations running on the wrong fields when removing/inserting/moving/shifting array fields.
Please review when you have time!
Thanks!