Skip to content

Commit b2203c8

Browse files
committedNov 29, 2023
fix: apply schema casts when submitting closes #4565
1 parent ec8a4d7 commit b2203c8

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
 

Diff for: ‎.changeset/brave-camels-wonder.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
fix: apply schema casts when submitting closes #4565

Diff for: ‎packages/vee-validate/src/useForm.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ export function useForm<
426426
{ valid: formResult.valid, results: {}, errors: {} } as FormValidationResult<TValues>,
427427
);
428428

429+
if (formResult.values) {
430+
results.values = formResult.values;
431+
}
432+
429433
return results;
430434
},
431435
);
@@ -767,8 +771,8 @@ export function useForm<
767771
*/
768772
function resetForm(resetState?: Partial<FormState<TValues>>, opts?: ResetFormOpts) {
769773
let newValues = deepCopy(resetState?.values ? resetState.values : originalInitialValues.value);
770-
newValues = isTypedSchema(schema) && isCallable(schema.cast) ? schema.cast(newValues) : newValues;
771774
newValues = opts?.force ? newValues : merge(originalInitialValues.value, newValues);
775+
newValues = isTypedSchema(schema) && isCallable(schema.cast) ? schema.cast(newValues) : newValues;
772776
setInitialValues(newValues);
773777
mutateAllPathState(state => {
774778
state.__flags.pendingReset = true;

Diff for: ‎packages/zod/tests/zod.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,53 @@ test('uses zod default values for initial values', async () => {
356356
);
357357
});
358358

359+
test('uses zod transforms values for submitted values', async () => {
360+
const onSubmitSpy = vi.fn();
361+
let onSubmit!: () => void;
362+
let model!: Ref<string | undefined>;
363+
mountWithHoc({
364+
setup() {
365+
const schema = toTypedSchema(
366+
z
367+
.object({
368+
random: z.string().min(3),
369+
})
370+
.transform(() => {
371+
return {
372+
random: 'modified',
373+
};
374+
}),
375+
);
376+
377+
const { handleSubmit, defineField } = useForm({
378+
validationSchema: schema,
379+
});
380+
381+
model = defineField('random')[0];
382+
383+
// submit now
384+
onSubmit = handleSubmit(onSubmitSpy);
385+
386+
return {
387+
schema,
388+
};
389+
},
390+
template: `<div></div>`,
391+
});
392+
393+
await flushPromises();
394+
model.value = 'test';
395+
onSubmit();
396+
await flushPromises();
397+
await expect(onSubmitSpy).toHaveBeenCalledTimes(1);
398+
await expect(onSubmitSpy).toHaveBeenLastCalledWith(
399+
expect.objectContaining({
400+
random: 'modified',
401+
}),
402+
expect.anything(),
403+
);
404+
});
405+
359406
test('reset form should cast the values', async () => {
360407
const valueSpy = vi.fn();
361408
mountWithHoc({

0 commit comments

Comments
 (0)
Please sign in to comment.