-
-
Notifications
You must be signed in to change notification settings - Fork 355
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
[WIP] fix: errors not resetting once values are correct for Zod #1082
base: main
Are you sure you want to change the base?
Conversation
@Balastrong |
View your CI Pipeline Execution ↗ for commit 36ca799.
☁️ Nx Cloud last updated this comment at |
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.
You're on the right track but this might be a bit more tricky than I initially thought. I added some notes, let me know what you think.
Thank you!
@@ -747,6 +747,26 @@ export class FormApi< | |||
})) | |||
} | |||
} | |||
} else { |
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.
Here the form validator should clear the field errors only if the validator is one that returns the object version of FormValidationError
.
This means you should go with else if (isFormValidationError(rawError))
.
However, when the form has no errors the adapters are returning undefined
. With this information alone you don't know if it was a form object validator or a simple validator returning a string, as in that case you shouldn't clear fields (the new failing tests you're getting) but only the form error.
The first approach that comes to my mind is to slightly refactor the adapters and make them return something instead of undefined. It could be { fields: {} }
With this information you know that the form validator returned no errors and you should clear the fields.
Note: this might break the initial if (fieldErrors)
but something like if (Object.values(fieldErrors).length)
should do the trick.
const newErrorMap = prev.errorMap | ||
|
||
if (prev.isDirty) { | ||
for (const [errorKey] of Object.entries(prev.errorMap)) { | ||
newErrorMap[errorKey as ValidationErrorMapKeys] = undefined | ||
} | ||
} |
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.
Are those lines still needed or were they left by some other attempts?
@@ -747,6 +747,26 @@ export class FormApi< | |||
})) | |||
} | |||
} | |||
} else { | |||
for (const [field] of Object.entries(this.fieldInfo)) { |
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.
Tiny thing, Object.keys
might be enough here
@@ -875,7 +895,28 @@ export class FormApi< | |||
})) | |||
} | |||
} | |||
} else { |
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.
Comments for sync also apply here, but let's make sync work first, then we'll replicate in the async :)
expect(form.state.canSubmit).toBe(false) | ||
|
||
field2.setValue('password') | ||
expect(field2.getMeta().errors).toStrictEqual([]) |
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.
This is always going to be empty, you should check for field1
errors
This is an attempt to fix the issue raise in #999
I had a similar issue I think is linked to this.
This passes my test I added but it breaks other tests.
I'm not sure if I'm either handling it incorrectly or if fixing this for zod causes other things to break.
Please let me know if i'm heading in the right direction.