-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Tiny thing, |
||
this.setFieldMeta(field as DeepKeys<TFormData>, (prev) => { | ||
const newErrorMap = prev.errorMap | ||
|
||
if (prev.isDirty) { | ||
for (const [errorKey] of Object.entries(prev.errorMap)) { | ||
newErrorMap[errorKey as ValidationErrorMapKeys] = undefined | ||
} | ||
} | ||
Comment on lines
+753
to
+759
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
|
||
return { | ||
...prev, | ||
errorMap: { | ||
...prev.errorMap, | ||
[errorMapKey]: undefined, | ||
}, | ||
} | ||
}) | ||
} | ||
} | ||
|
||
if (this.state.errorMap[errorMapKey] !== formError) { | ||
|
@@ -875,7 +895,28 @@ export class FormApi< | |
})) | ||
} | ||
} | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) |
||
for (const [field] of Object.entries(this.fieldInfo)) { | ||
this.setFieldMeta(field as DeepKeys<TFormData>, (prev) => { | ||
const newErrorMap = prev.errorMap | ||
|
||
if (prev.isDirty) { | ||
for (const [errorKey] of Object.entries(prev.errorMap)) { | ||
newErrorMap[errorKey as ValidationErrorMapKeys] = undefined | ||
} | ||
} | ||
|
||
return { | ||
...prev, | ||
errorMap: { | ||
...prev.errorMap, | ||
[errorMapKey]: undefined, | ||
}, | ||
} | ||
}) | ||
} | ||
} | ||
|
||
this.store.setState((prev) => ({ | ||
...prev, | ||
errorMap: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,4 +184,55 @@ describe('zod form api', () => { | |
name0Field.setValue('qwer') | ||
expect(name0Field.getMeta().errors).toEqual([]) | ||
}) | ||
|
||
it('should clear errors if previously errors were set and now are fixed', () => { | ||
const form = new FormApi({ | ||
defaultValues: { | ||
password: '', | ||
confirmPassword: '', | ||
}, | ||
validatorAdapter: zodValidator(), | ||
validators: { | ||
onChange: z | ||
.object({ | ||
password: z.string(), | ||
confirmPassword: z.string(), | ||
}) | ||
.refine( | ||
({ password, confirmPassword }) => password === confirmPassword, | ||
{ | ||
message: 'Passwords must match', | ||
path: ['password'], | ||
}, | ||
), | ||
}, | ||
}) | ||
form.mount() | ||
|
||
const field1 = new FieldApi({ | ||
form, | ||
name: 'password', | ||
defaultMeta: { | ||
isTouched: true, | ||
}, | ||
}) | ||
field1.mount() | ||
|
||
const field2 = new FieldApi({ | ||
form, | ||
name: 'confirmPassword', | ||
defaultMeta: { | ||
isTouched: true, | ||
}, | ||
}) | ||
field2.mount() | ||
|
||
field1.setValue('password') | ||
expect(field1.getMeta().errors).toStrictEqual(['Passwords must match']) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. This is always going to be empty, you should check for |
||
expect(form.state.canSubmit).toBe(true) | ||
}) | ||
}) |
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 likeif (Object.values(fieldErrors).length)
should do the trick.