Skip to content

Commit e9b215a

Browse files
committed
fix: resetForm should cast typed schema values closes #4347
1 parent 40ce7a9 commit e9b215a

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

.changeset/old-peaches-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
fix: resetForm should cast typed schema values closes #4347

packages/vee-validate/src/useForm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,8 @@ export function useForm<
660660
* Resets all fields
661661
*/
662662
function resetForm(resetState?: Partial<FormState<TValues>>) {
663-
const newValues = resetState?.values ? resetState.values : originalInitialValues.value;
663+
let newValues = resetState?.values ? resetState.values : originalInitialValues.value;
664+
newValues = isTypedSchema(schema) && isCallable(schema.cast) ? schema.cast(newValues) : newValues;
664665
setInitialValues(newValues);
665666
mutateAllPathState(state => {
666667
state.validated = false;

packages/zod/tests/zod.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,40 @@ test('uses zod default values for initial values', async () => {
353353
);
354354
});
355355

356+
test('reset form should cast the values', async () => {
357+
const valueSpy = vi.fn();
358+
mountWithHoc({
359+
setup() {
360+
const schema = toTypedSchema(
361+
z.object({
362+
age: z.preprocess(arg => Number(arg), z.number()),
363+
})
364+
);
365+
366+
const { values, resetForm } = useForm({
367+
validationSchema: schema,
368+
});
369+
370+
resetForm({ values: { age: '12' } });
371+
// submit now
372+
valueSpy(values);
373+
374+
return {
375+
schema,
376+
};
377+
},
378+
template: `<div></div>`,
379+
});
380+
381+
await flushPromises();
382+
await expect(valueSpy).toHaveBeenCalledTimes(1);
383+
await expect(valueSpy).toHaveBeenLastCalledWith(
384+
expect.objectContaining({
385+
age: 12,
386+
})
387+
);
388+
});
389+
356390
// #4186
357391
test('default values should not be undefined', async () => {
358392
const initialSpy = vi.fn();

0 commit comments

Comments
 (0)