-
-
Notifications
You must be signed in to change notification settings - Fork 579
fix(form-core): Prevent synchronous validators from returning Promises #1987
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 6b927d2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Passing an asynchronous function to a synchronous validator (i.e.: `onBlur`, `onChange`, `onDynamic`, `onSubmit`) is a bit of a foot-gun given that it does not produce any typescript errors, but it also results in the form/field validation function running after the core validation logic. To prevent this, update the types for these validator functions on both `FormApi` and `FieldApi` to prevent passing a function that returns a `Promise`.
5924423 to
2198362
Compare
|
View your CI Pipeline Execution ↗ for commit 6b927d2
☁️ Nx Cloud last updated this comment at |
|
Just pushed an update that should address the lint issue, sorry about that! |
| export type RejectPromiseValidator<T> = T extends (...args: any[]) => infer R | ||
| ? // Check if R is `any` - if so, allow it (common with mocks) | ||
| 0 extends 1 & R | ||
| ? T | ||
| : // Otherwise, reject if it's a Promise | ||
| [R] extends [Promise<any> | PromiseLike<any>] | ||
| ? never | ||
| : T | ||
| : T |
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.
Is this truly needed? Can the return type not be Omit<T, Promise<any> | PromiseLike<any>>?
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.
I'm assuming you mean Exclude rather than Omit, but no that does not work here. I believe it is a limitation of TypeScript, but the type is still treated as unknown it seems even if attempting to approach it from this angle.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1987 +/- ##
==========================================
- Coverage 90.35% 89.46% -0.90%
==========================================
Files 38 48 +10
Lines 1752 1945 +193
Branches 444 488 +44
==========================================
+ Hits 1583 1740 +157
- Misses 149 184 +35
- Partials 20 21 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
🎯 Changes
Passing an asynchronous function to a synchronous validator (i.e.:
onBlur,onChange,onDynamic,onSubmit) is a bit of a foot-gun given that it does not produce any typescript errors, but it also results in the form/field validation function running after the core validation logic.This updates the types for these validator functions on both FormApi and FieldApi to prevent passing a function that returns a Promise.
✅ Checklist
pnpm test:pr.🚀 Release Impact