Skip to content
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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Gambitboy
Copy link

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.

@Gambitboy
Copy link
Author

@Balastrong
When you've got some time to check out what I've done, and if I'm in headed in the right direction here.

Copy link

nx-cloud bot commented Dec 21, 2024

View your CI Pipeline Execution ↗ for commit 36ca799.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ❌ Failed 1m 36s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 28s View ↗

☁️ Nx Cloud last updated this comment at 2024-12-21 16:50:01 UTC

Copy link

pkg-pr-new bot commented Dec 21, 2024

Open in Stackblitz

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1082

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1082

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1082

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1082

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1082

@tanstack/valibot-form-adapter

npm i https://pkg.pr.new/@tanstack/valibot-form-adapter@1082

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1082

@tanstack/yup-form-adapter

npm i https://pkg.pr.new/@tanstack/yup-form-adapter@1082

@tanstack/zod-form-adapter

npm i https://pkg.pr.new/@tanstack/zod-form-adapter@1082

commit: 36ca799

Copy link
Member

@Balastrong Balastrong left a 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 {
Copy link
Member

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.

Comment on lines +753 to +759
const newErrorMap = prev.errorMap

if (prev.isDirty) {
for (const [errorKey] of Object.entries(prev.errorMap)) {
newErrorMap[errorKey as ValidationErrorMapKeys] = undefined
}
}
Copy link
Member

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)) {
Copy link
Member

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 {
Copy link
Member

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([])
Copy link
Member

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

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.

2 participants