Skip to content

Commit 6652a22

Browse files
committed
fix: don't mutate validated meta when silent validation closes #3981 closes #3982
1 parent 99cf2f5 commit 6652a22

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/vee-validate/src/useForm.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,13 @@ export function useForm<TValues extends Record<string, any> = Record<string, any
663663
}
664664

665665
async function validate(opts?: Partial<ValidationOptions>): Promise<FormValidationResult<TValues>> {
666-
mutateAllFields(f => (f.meta.validated = true));
666+
const mode = opts?.mode || 'force';
667+
if (mode === 'force') {
668+
mutateAllFields(f => (f.meta.validated = true));
669+
}
670+
667671
if (formCtx.validateSchema) {
668-
return formCtx.validateSchema(opts?.mode || 'force');
672+
return formCtx.validateSchema(mode);
669673
}
670674

671675
// No schema, each field is responsible to validate itself

packages/vee-validate/tests/useForm.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormContext, useField, useForm } from '@/vee-validate';
1+
import { FieldMeta, FormContext, useField, useForm } from '@/vee-validate';
22
import { mountWithHoc, setValue, flushPromises, runInSetup } from './helpers';
33
import * as yup from 'yup';
44
import { onMounted, Ref } from 'vue';
@@ -620,4 +620,35 @@ describe('useForm()', () => {
620620
})
621621
);
622622
});
623+
624+
// #3981 #3982
625+
test('fields validated meta should not be mutated when silently validating fields', async () => {
626+
let meta!: FieldMeta<any>;
627+
628+
mountWithHoc({
629+
setup() {
630+
const { validate } = useForm({
631+
validationSchema: yup.object({
632+
name: yup.string().required(),
633+
}),
634+
});
635+
636+
const field = useField('name');
637+
meta = field.meta;
638+
639+
onMounted(() => {
640+
validate({ mode: 'silent' });
641+
validate({ mode: 'validated-only' });
642+
});
643+
644+
return {};
645+
},
646+
template: `
647+
<div></div>
648+
`,
649+
});
650+
651+
await flushPromises();
652+
expect(meta.validated).toBe(false);
653+
});
623654
});

0 commit comments

Comments
 (0)